Opencv pixel Processing

Source: Internet
Author: User

From: http://blog.csdn.net/hxgqh/archive/2011/02/23/6202001.aspx

You can obtain the pixel value for more flexible image processing. In opencv, you can add a cvscalar struct to the cvget2d () and cvset2d () functions.
In opencv, The cvscalar structure is:
Typedef struct cvscalar
{
Double Val [4];
}
Cvscalar;

Four double-type variables will not be forced type conversion during algorithm processing and reduce the accuracy.
Let's take a look at the definition of the read/write function:

Cvget2d obtains the value of a certain vertex. idx0 = the value of the hight row and idx1 = the value of the width column.
Cvapi (cvscalar) cvget2d (const cvarr * arr, int idx0, int idx1 );

Cvset2d assigns a value to a vertex.
Cvapi (void) cvset2d (cvarr * arr, int idx0, int idx1, cvscalar value );

As shown in the preceding figure, the cvget2d return type and the Value Type in cvset2d are both cvscalar. In this way, it is OK to define a cvscalar variable and then call the function.

Example of pixel reading and writing in opencv:

Int main (INT argc, char ** argv)
{
Iplimage * IMG = cvloadimage (argv [1], 1 );
Cvscalar pixel;
For (INT I = 0; I height; ++ I)
{
For (Int J = 0; j width; ++ J)
{
// Obtain and display the RGB values of pixels. Note that the storage sequence in the memory is BGR.
Pixel = cvget2d (IMG, I, j );
Printf ("B = % F, G = % F, R = % F/T", pixel. val [0], pixel. val [1], pixel. val [2]);
// Modify the value of each point
Pixel. Val [0] = 0;
Pixel. Val [1] = 0;
Pixel. Val [2] = 0;
Cvset2d (IMG, I, j, pixel );
}
}
Cvnamedwindow ("image", 1 );
Cvshowimage ("image", IMG );
Cvwaitkey (0 );
Cvdestroywindow ("image ");
Cvreleaseimage (& IMG );
Return 0;
}

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.