Seven. Using OpenCV for Image color recognition

Source: Internet
Author: User
Tags printf

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.

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.