opencv颜色识别

匿名 (未验证) 提交于 2019-12-03 00:22:01
#include <imgproc/imgproc.hpp>   #include <opencv2/core/core.hpp>   #include<opencv2/highgui/highgui.hpp> #include <iostream>   #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include<string>  #include<vector>  #define BIGGEST(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))  #define RED 1 #define GREEN 2 #define BLUE 3  //HSV范围  //红色HSV范围 #define iLowH_R  125 #define iHighH_R  155 #define iLowS_R  43 #define iHighS_R  255 #define iLowV_R  46 #define iHighV_R  255 //绿色HSV范围 #define iLowH_G  35 #define iHighH_G  77 #define iLowS_G  43 #define iHighS_G  255 #define iLowV_G  46 #define iHighV_G  255 //蓝色HSV范围 #define iLowH_B  100 #define iHighH_B  124 #define iLowS_B  128 #define iHighS_B  255 #define iLowV_B  102 #define iHighV_B  255  using namespace cv; using namespace std;  Mat WhiteBalance(Mat image); double PixelTotal(Mat mask); int HSV(Mat img);  int main() {  	IplImage* example = cvLoadImage("C:\\Users\\Lenovo\\Desktop\\Picprocess\\DDD.jpg", 1); 	Mat Img, exampleWB; 	int color; 	Img = cvarrToMat(example); 	exampleWB = WhiteBalance(Img); 	color = HSV(exampleWB); 	printf("return value:%d\n",color);  }  Mat WhiteBalance(Mat image) { 	vector<Mat> imageRGB;  	//RGB三通道分离   	split(image, imageRGB);  	//求原始图像的RGB分量的均值   	double R, G, B; 	B = mean(imageRGB[0])[0]; 	G = mean(imageRGB[1])[0]; 	R = mean(imageRGB[2])[0];  	//需要调整的RGB分量的增益   	double KR, KG, KB; 	KB = (R + G + B) / (3 * B); 	KG = (R + G + B) / (3 * G); 	KR = (R + G + B) / (3 * R);  	//调整RGB三个通道各自的值   	imageRGB[0] = imageRGB[0] * KB;//蓝色 	imageRGB[1] = imageRGB[1] * KG;//绿色 	imageRGB[2] = imageRGB[2] * KR;//红色  								   //RGB三通道图像合并   	merge(imageRGB, image); 	waitKey(0); 	return image; }//白平衡子函数  double PixelTotal(Mat mask) { 	IplImage *image; 	image = &IplImage(mask); 	double i, j, pixel = 0; 	CvScalar s; 	for (i = 0; i < image->height; i++) { 		for (j = 0; j < image->width; j++) { 			s = cvGet2D(image, i, j); 			pixel += s.val[0]; 		} 	} 	return pixel; }  int HSV(Mat img) {//Mat imgOutR, Mat imgOutB, Mat imgOutG) { 	Mat imgHSV; 	Mat imgGray;  	cvtColor(img, imgHSV, COLOR_BGR2HSV);//转为HSV   	cvtColor(img, imgGray, COLOR_RGB2GRAY);  	imwrite("C:\\Users\\Lenovo\\Desktop\\Picprocess\\hsv.jpg", imgHSV); 	Mat maskR, maskG, maskB;  	inRange(imgHSV, Scalar(iLowH_G, iLowS_G, iLowV_G), Scalar(iHighH_G, iHighS_G, iHighV_G), maskG); //Threshold the image     	inRange(imgHSV, Scalar(iLowH_R, iLowS_R, iLowV_R), Scalar(iHighH_R, iHighS_R, iHighV_R), maskR); 	inRange(imgHSV, Scalar(iLowH_B, iLowS_B, iLowV_B), Scalar(iHighH_B, iHighS_B, iHighV_B), maskB);  	//开操作 (去除一些噪点)   	Mat element = getStructuringElement(MORPH_RECT, Size(10, 10)); 	morphologyEx(maskG, maskG, MORPH_OPEN, element);//如果二值化后图片干扰部分依然很多,增大下面的size 	morphologyEx(maskR, maskR, MORPH_OPEN, element); 	morphologyEx(maskB, maskB, MORPH_OPEN, element);  	imwrite("C:\\Users\\Lenovo\\Desktop\\Picprocess\\G.jpg", maskG); 	imwrite("C:\\Users\\Lenovo\\Desktop\\Picprocess\\B.jpg", maskB); 	imwrite("C:\\Users\\Lenovo\\Desktop\\Picprocess\\R.jpg", maskR);  	double x_r = 0, x_b = 0, x_g = 0, x_total = 0;  	x_r = PixelTotal(maskR); 	x_g = PixelTotal(maskG); 	x_b = PixelTotal(maskB); 	x_total = BIGGEST(x_b, x_g, x_r);  	if (x_total == x_b) { 		printf("This is blue, pixel %lf\n", x_total); 		return BLUE; 	} 	else if (x_total == x_g) { 		printf("This is green, pixel %lf\n", x_total); 		return GREEN; 	} 	else { 		printf("This is red, pixel %lf\n", x_total); 		return RED; 	} 	waitKey(0);  }//RGB转化为HSV,并将图像二值化,根据需求得到需要的色块处理图像

文章来源: opencv颜色识别
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!