首页 > 未分类 > OpenCV识别指定颜色(黑、灰、白、红、橙、黄、绿、青、蓝、紫)
2020
04-21

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进行动态屏幕颜色识别!

————————————————

版权声明:本文为CSDN博主「IT1995」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq78442761/article/details/83056346


本文》有 0 条评论

留下一个回复