[Opencv application notes] (color/grayscale) the pixel values of the image are read and saved to the TXT file.

Source: Internet
Author: User

[Opencv application notes] (color/grayscale) the pixel values of the image are read and saved to the TXT file.

Skyseraph Feb 23rd 2012 sztcl

Email: zgzhaobo@gmail.com QQ: 452728574

Bytes -------------------------------------------------------------------------------------------------------------------------------------------------------------

I. wordy:

A hardware expert wants to test the CCD communication between FPGA and PC. He needs to use image data to find the "image processing master" in my former "xiaoao" lab and entrust me to write a small test.ProgramSave the TXT file of the specified image (color/gray) conversion process to the PC. Ta only needs the EXE file,CodeYou don't need this kind of stuff... why? I wrote a script and tested it. OK. For details, see... O (success _ success) O.

Bytes -------------------------------------------------------------------------------------------------------------------------------------------------------------

Ii. Source Code

1 Color Image

① Input: A colored image rgb.bmp

② Output:The three TXT files under the D drive are respectively r.txt0000g.txt0000b.txt

③ Source code:

View code

 1   /*  ========================================================== ========== //
2 Function: Read and save RGB data.
3 Time: 02/23/2012 skyseraph
4 // ================================================ ========== */
5 # Include " Iostream "
6 # Include <fstream>
7 Using Namespace STD;
8
9 # Include " Cv. h "
10 # Include " Highgui. h "
11
12 # Pragma Comment (Lib, "highgui. lib ")
13 # Pragma Comment (Lib, "cv. lib ")
14 # Pragma Comment (Lib, "cvaux. lib ")
15 # Pragma Comment (Lib, "cxcore. lib ")
16
17 Int Main ( Int Argc, Char * Argv [])
18 {
19 /* ①
20 Iplimage * IMG = cvloadimage ("rgb.bmp",-1 );
21 If (IMG = NULL)
22 Return 0;
23 Cvscalar P;
24 Ofstream OUTFILE ("D: \ rgb.txt ");
25 OUTFILE <"image width and height:" width <"*" height <Endl;
26 For (INT I = 0; I height; I ++)
27 {
28 For (Int J = 0; j width; j ++)
29 {
30 P = cvget2d (IMG, I, j );
31 OUTFILE <p. val [0] <"<p. val [1] <"<p. val [2] <"<Endl;
32 }
33 }
34 */
35 /// * ②
36 Iplimage * IMG = cvloadimage ( " Rgb.bmp " ,- 1 );
37 Cvscalar P;
38 Ofstream outfile1 ( " D: \ B .txt " );
39 Ofstream outfile2 ( " D: \ g.txt " );
40 Ofstream outfile3 ( " D: \ r.txt " );
41 Outfile1 <" Image width and height: " width < " * " height <Endl;
42 Outfile1 < " Image B value " <Endl;
43 Outfile2 < " Image width and height: " width < " * " height <Endl;
44 Outfile2 < " Image G value " <Endl;
45 Outfile3 < " Image width and height: " width < " * " height <Endl;
46 Outfile3 < " Image R Value " <Endl;
47 // Cvflip (IMG );
48 For ( Int I = 0 ; I width; I ++)
49 {
50 For ( Int J = 0 ; J height; j ++)
51 {
52 P = cvget2d (IMG, I, j ); // (J, I)
53 Outfile1 <p. Val [ 0 ] < " " ;
54 Outfile2 <p. Val [ 1 ] < " " ;
55 Outfile3 <p. Val [ 2 ] < " " ;
56 }
57 Outfile1 <Endl;
58 Outfile2 <Endl;
59 Outfile3 <Endl;
60 } // */
61
62 Return 0 ;
63 }

④ EXE file: rgb.zip

2. Grayscale Images

① Input:Grayscale image gray.jpg

② Output:Gray.txt

③ Source code:

View code

 1   /*  ========================================================== ========== //
2 Function: Gray reads and saves data.
3 Time: 02/23/2012 skyseraph
4 // ================================================ ========== */
5 # Include " Iostream "
6 # Include <fstream>
7 Using Namespace STD;
8
9 # Include " Cv. h "
10 # Include " Highgui. h "
11
12 # Pragma Comment (Lib, "highgui. lib ")
13 # Pragma Comment (Lib, "cv. lib ")
14 # Pragma Comment (Lib, "cvaux. lib ")
15 # Pragma Comment (Lib, "cxcore. lib ")
16
17
18 Int Main ( Int Argc, Char * Argv [])
19 {
20 Iplimage * IMG = cvloadimage (" Gray.jpg " , 0 );
21 Cvscalar P;
22 Ofstream outfile1 ( " D: \ gray.txt " );
23 Outfile1 < " Image width and height: " width <" * " height <Endl;
24 Outfile1 < " Image pixel value " <Endl;
25 // Cvflip (IMG );
26 For ( Int I = 0 ; I width; I ++)
27 {
28 For ( Int J = 0 ; J height; j ++)
29 {
30 P = cvget2d (IMG, I, j ); // (J, I)
31 Outfile1 <p. Val [ 0 ] <" " ;
32 }
33 Outfile1 <Endl;
34 } // */
35
36 Return 0 ;
37 }

④ EXE file: gray.zip

Bytes -------------------------------------------------------------------------------------------------------------------------------------------------------------

Iii. Effect

Color as an example, source Image

R.txt (Part): gray.zip



Others are similar...

Bytes -------------------------------------------------------------------------------------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.