OpenCV颜色识别

来源:互联网 发布:口袋妖怪能力数据 编辑:程序博客网 时间:2024/04/28 02:44
转:http://blog.csdn.net/zwhlxl/article/details/46381353

彩色模型

数字图像处理中常用的采用模型是RGB(红,绿,蓝)模型和HSV(色调,饱和度,亮度),RGB广泛应用于彩色监视器和彩色视频摄像机,我们平时的图片一般都是RGB模型。而HSV模型更符合人描述和解释颜色的方式,HSV的彩色描述对人来说是自然且非常直观的。

HSV模型

HSV模型中颜色的参数分别是:色调(H:hue),饱和度(S:saturation),亮度(V:value)。由A. R. Smith在1978年创建的一种颜色空间, 也称六角锥体模型(Hexcone Model)。

  • 色调(H:hue):用角度度量,取值范围为0°~360°,从红色开始按逆时针方向计算,红色为0°,绿色为120°,蓝色为240°。它们的补色是:黄色为60°,青色为180°,品红为300°;
  • 饱和度(S:saturation):取值范围为0.0~1.0,值越大,颜色越饱和。
  • 亮度(V:value):取值范围为0(黑色)~255(白色)。

RGB转HSV

设 (r, g, b) 分别是一个颜色的红、绿和蓝坐标,它们的值是在 0 到 1 之间的实数。设 max 等价于 r, g 和 b 中的最大者。设 min 等于这些值中的最小者。要找到在 HSV 空间中的 (h, s, v) 值,这里的 h ∈ [0, 360)是角度的色相角,而 s, v ∈ [0,1] 是饱和度和亮度,计算为:

max=max(R,G,B) min=min(R,G,B) if R = max, H = (G-B)/(max-min) if G = max, H = 2 + (B-R)/(max-min) if B = max, H = 4 + (R-G)/(max-min) H = H * 60 if H < 0, H = H + 360 V=max(R,G,B) S=(max-min)/max
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

OpenCV下有个函数可以直接将RGB模型转换为HSV模型,注意的是opencv中H∈ [0, 180), S ∈ [0, 255], V ∈ [0, 255]。我们知道H分量基本能表示一个物体的颜色,但是S和V的取值也要在一定范围内,因为S代表的是H所表示的那个颜色和白色的混合程度,也就说S越小,颜色越发白,也就是越浅;V代表的是H所表示的那个颜色和黑色的混合程度,也就说V越小,颜色越发黑。经过实验,识别蓝色的取值是 H在100到140,S和V都在90到255之间。一些基本的颜色H的取值可以如下设置:

Orange  0-22Yellow 22- 38Green 38-75Blue 75-130Violet 130-160Red 160-179
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

OpenCV实现

首先我们读取一张图片或从视频读取一帧图像,用下面的函数转为HSV模型。

cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV);
  • 1
  • 1

然后我们对彩色图像做直方图均衡化

 //因为我们读取的是彩色图,直方图均衡化需要在HSV空间做   split(imgHSV, hsvSplit);   equalizeHist(hsvSplit[2],hsvSplit[2]);   merge(hsvSplit,imgHSV);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

接着就是进行颜色检测,我们用void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst);函数进行颜色检测,这个函数的作用就是检测src图像的每一个像素是不是在lowerb和upperb之间,如果是,这个像素就设置为255,并保存在dst图像中,否则为0。

inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
  • 1
  • 1

通过上面的函数我们就可以得到目标颜色的二值图像,接着我们先对二值图像进行开操作,删除一些零零星星的噪点,再使用闭操作,连接一些连通域,也就是删除一些目标区域的白色的洞。

//开操作 (去除一些噪点)   Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));   morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);   //闭操作 (连接一些连通域)   morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

整个代码实现

#include <iostream>#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"using namespace cv;using namespace std; int main( int argc, char** argv ) {    VideoCapture cap(0); //capture the video from web cam    if ( !cap.isOpened() )  // if not success, exit program    {         cout << "Cannot open the web cam" << endl;         return -1;    }  namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"  int iLowH = 100;  int iHighH = 140;  int iLowS = 90;   int iHighS = 255;  int iLowV = 90;  int iHighV = 255;  //Create trackbars in "Control" window  cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)  cvCreateTrackbar("HighH", "Control", &iHighH, 179);  cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)  cvCreateTrackbar("HighS", "Control", &iHighS, 255);  cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)  cvCreateTrackbar("HighV", "Control", &iHighV, 255);    while (true)    {        Mat imgOriginal;        bool bSuccess = cap.read(imgOriginal); // read a new frame from video         if (!bSuccess) //if not success, break loop        {             cout << "Cannot read a frame from video stream" << endl;             break;        }   Mat imgHSV;   vector<Mat> hsvSplit;   cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV   //因为我们读取的是彩色图,直方图均衡化需要在HSV空间做   split(imgHSV, hsvSplit);   equalizeHist(hsvSplit[2],hsvSplit[2]);   merge(hsvSplit,imgHSV);   Mat imgThresholded;   inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image   //开操作 (去除一些噪点)   Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));   morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);   //闭操作 (连接一些连通域)   morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);   imshow("Thresholded Image", imgThresholded); //show the thresholded image   imshow("Original", imgOriginal); //show the original image   char key = (char) waitKey(300);   if(key == 27)         break;    }   return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

实验结果图:
这里写图片描述

颜色识别的应用

经典的颜色识别的经典应用就是车牌定位了,因为中国的车牌无非就是蓝色和黄色,还有就是交通标志定位也是个应用。比如下面两张图片,有很明显的颜色区分。
这里写图片描述
这里写图片描述

原创粉丝点击