Then the image of the previous grayscale processing, the next gray image color recognition, my method is relatively simple, there are other good methods can be put forward, we progress together.
Objective:
Turns the pixel grayscale in all of the grayscale images to white (set to 255), and less than 128 to black (set to 0).
Then count the number of black and white pixels.
Here's the code:
#include <stdio.h> #include <stdlib.h> #include <opencv/cv.h> #include <opencv/
highgui.h> int main (int argc, char* argv[]) {cvscalar pixel;
int i = 0, j = 0;
int NUMW = 0, numb = 0;
if (argc! = 2) {printf ("usage:./test xxx.jpg\n");
return 1; } iplimage* img = Cvloadimage (argv[1], cv_load_image_anycolor|
Cv_load_image_anydepth);
if (NULL = = img) {printf ("Load Image fail!\n");
return 2; }//Access image each pixel point for (i=0; iheight; i++) {for (j=0; jwidth; J + +) {pixel = Cvget2d (img, I, j
);
if (Pixel.val[0] >) {pixel.val[0] = 255;//8 single-channel image stores pixels in pixel.val[0] ++numw;//statistics white point number} else {
Pixel.val[0] = 0;
++numb;//statistics Black Number} cvset2d (IMG, I, J, Pixel);
}} printf ("Image height:%d, width:%d\n", Img->height, Img->width);
printf ("White num:%d, Black num:%d\n", NUMW, Numb);
Cvsaveimage ("Handle.jpg", IMG, 0);
Cvreleaseimage (&IMG);
return 0; }
This processing image is equivalent to the grayscale image color to deepen a level, convenient for subsequent image processing.