OpenCV识别指定颜色(黑、灰、白、红、橙、黄、绿、青、蓝、紫)

根据HSV色域的特点,进行识别,关于HSV色域,请看这篇博文

https://blog.csdn.net/qq78442761/article/details/82805610

 

素材如下:

运行截图如下:

源码如下:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>

#include <iostream>
#include <vector>
using namespace std;
using namespace cv;

void main(){

	Mat matSrc=imread("purple.png",IMREAD_UNCHANGED);
	Mat matHsv;
	cvtColor(matSrc,matHsv,COLOR_BGR2HSV);

	vector<int> colorVec;
	colorVec.push_back(matHsv.at<uchar>(0,0));
	colorVec.push_back(matHsv.at<uchar>(0,1));
	colorVec.push_back(matHsv.at<uchar>(0,2));

	if((colorVec[0]>=0&&colorVec[0]<=180)
		&&(colorVec[1]>=0&&colorVec[1]<=255)
		&&(colorVec[2]>=0&&colorVec[2]<=46)){

			cout<<"黑"<<endl;
	}
	else if((colorVec[0]>=0&&colorVec[0]<=180)
		&&(colorVec[1]>=0&&colorVec[1]<=43)
		&&(colorVec[2]>=46&&colorVec[2]<=220)){

			cout<<"灰"<<endl;
	}
	else if((colorVec[0]>=0&&colorVec[0]<=180)
		&&(colorVec[1]>=0&&colorVec[1]<=30)
		&&(colorVec[2]>=221&&colorVec[2]<=255)){

			cout<<"白"<<endl;
	}
	else if(((colorVec[0]>=0&&colorVec[0]<=10)||(colorVec[0]>=156&&colorVec[0]<=180))
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"红"<<endl;

	}
	else if((colorVec[0]>=11&&colorVec[0]<=25)
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"橙"<<endl;
	}
	else if((colorVec[0]>=26&&colorVec[0]<=34)
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"黄"<<endl;
	}
	else if((colorVec[0]>=35&&colorVec[0]<=77)
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"绿"<<endl;
	}
	else if((colorVec[0]>=78&&colorVec[0]<=99)
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"青"<<endl;
	}
	else if((colorVec[0]>=100&&colorVec[0]<=124)
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"蓝"<<endl;
	}
	else if((colorVec[0]>=125&&colorVec[0]<=155)
		&&(colorVec[1]>=43&&colorVec[1]<=255)
		&&(colorVec[2]>=46&&colorVec[2]<=255)){

			cout<<"紫"<<endl;
	}
	else{

		cout<<"未知"<<endl;
	}

	getchar();
}

下面将利用Qt与Python进行动态屏幕颜色识别!

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页