彩色模型
数字图像处理中常用的采用模型是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
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-22 Yellow 22- 38 Green 38-75 Blue 75-130 Violet 130-160 Red 160-179
OpenCV实现
首先我们读取一张图片或从视频读取一帧图像,用下面的函数转为HSV模型。
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV);
然后我们对彩色图像做直方图均衡化
split(imgHSV, hsvSplit); equalizeHist(hsvSplit[2],hsvSplit[2]); merge(hsvSplit,imgHSV);
接着就是进行颜色检测,我们用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);
通过上面的函数我们就可以得到目标颜色的二值图像,接着我们先对二值图像进行开操作,删除一些零零星星的噪点,再使用闭操作,连接一些连通域,也就是删除一些目标区域的白色的洞。
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5)); morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element); morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);
整个代码实现
#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); if ( !cap.isOpened() ) { cout << "Cannot open the web cam" << endl; return -1; } namedWindow("Control" , CV_WINDOW_AUTOSIZE); int iLowH = 100; int iHighH = 140; int iLowS = 90; int iHighS = 255; int iLowV = 90; int iHighV = 255; cvCreateTrackbar("LowH" , "Control" , &iLowH, 179); cvCreateTrackbar("HighH" , "Control" , &iHighH, 179); cvCreateTrackbar("LowS" , "Control" , &iLowS, 255); cvCreateTrackbar("HighS" , "Control" , &iHighS, 255); cvCreateTrackbar("LowV" , "Control" , &iLowV, 255); cvCreateTrackbar("HighV" , "Control" , &iHighV, 255); while ( true ) { Mat imgOriginal; bool bSuccess = cap.read(imgOriginal); if (!bSuccess) { cout << "Cannot read a frame from video stream" << endl; break ; } Mat imgHSV; vector<Mat> hsvSplit; cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); split(imgHSV, hsvSplit); equalizeHist(hsvSplit[2],hsvSplit[2]); merge(hsvSplit,imgHSV); Mat imgThresholded; inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); Mat element = getStructuringElement(MORPH_RECT, Size(5, 5)); morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element); morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element); imshow("Thresholded Image" , imgThresholded); imshow("Original" , imgOriginal); char key = ( char ) waitKey(300); if (key == 27) break ; } return 0; }
实验结果图:
颜色识别的应用
经典的颜色识别的经典应用就是车牌定位了,因为中国的车牌无非就是蓝色和黄色,还有就是交通标志定位也是个应用。比如下面两张图片,有很明显的颜色区分。