Realization of color recognition based on Opencv

  • 2020-06-19 11:12:30
  • OfStack

Color model

RGB (red, green, blue) model and HSV (hue, saturation, brightness) are commonly used in digital image processing. RGB is widely used in color monitors and color video cameras. Our usual picture 1 is RGB model. The HSV model is more in line with the way people describe and interpret colors. The color description of HSV is natural and intuitive to people.

HSV model

The parameters for the HSV model are hue (H: hue), saturation (S: saturation), and brightness (V: value). A color space created by A. R. Smith in 1978, also known as the Pyramid model (Hexcone Model).

(1) Hue (H: hue) : Measured by Angle, the value range is 0° ~ 360°. It is calculated counterclockwise from red, 0° in red, 120° in green and 240° in blue. Their complementary colors are 60° yellow, 180° cyan and 300° Magenta.

(2) Saturation (S: saturation) : The value range is 0.0 ~ 1.0. The larger the value is, the more saturated the color is.

(3) Brightness (V: value) : Value range is 0(black) ~ 255(white)

RGB into HSV

Let (r, g, b) be the red, green, and blue coordinates of 1 color, respectively, whose values are real Numbers between 0 and 1. Let max be equivalent to the largest of r, g and b. Let min equal the smallest of these values. To find the value (h, s, v) in HSV space, h ∈ [0, 360) is the hue Angle of the Angle, while s, v ∈ [0,1] are saturation and brightness, the method is as follows:

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

A function can be directly under the OpenCV converts RGB model HSV model, the OpenCV H ∈ [0, 180), S ∈ [0, 255], V ∈ (0, 255). We can know H component basic said one the color of the object, but S and V values can be within the scope of the 1 set, because S represent H represents the combination of color and white, also said the smaller S, The more white the color is, the lighter the color is; V represents the mixing degree of the color and black indicated by H, so the smaller V is, the darker the color is.

Orange 0-22, Yellow 22-38, Green 38-75, Blue 75-130, Violet 130-160, Red 160-179

The specific implementation

(1) Read a picture


cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV);

(2) Equalize the color image


split(imgHSV, hsvSplit);
equalizeHist(hsvSplit[2],hsvSplit[2]);
merge(hsvSplit,imgHSV);

(3) Color detection


inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded);

(4) Open operation and close operation to remove the influence of noise.


Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);
morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);

Code:


#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
 
using namespace cv;
using namespace std;
 
// Entry master function 
int main(int argc, char** argv)
{
 // Turn on the camera and get the image from the camera 
 VideoCapture cap(0);
 if (!cap.isOpened())
 {
 cout << "Cannot open the web cam"<<endl;
 return -1;
 }
 // Create a window 
 namedWindow("Control",CV_WINDOW_AUTOSIZE);
 
 int iLowH = 100;
 int iHighH = 140;
 
 int iLowS = 90;
 int iHighS = 255;
 
 int iLowV = 90;
 int iHighV = 255;
 // Create a progress bar 
 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 Cycled image 
 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);
 
 // To histogram equalization 
 split(imgHSV,hsvSplit);
 equalizeHist(hsvSplit[2],hsvSplit[2]);
 merge(hsvSplit,imgHSV);
 Mat imgThresholded;
 // Determine the range of the color display 
 inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS,iHighV),imgThresholded);
 // Get rid of the noise 
 Mat element = getStructuringElement(MORPH_RECT,Size(5,5));
 morphologyEx(imgThresholded,imgThresholded,MORPH_OPEN,element);
  // Connected connected domain 
 morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);
 imshow("Thresholded Image",imgThresholded);
 imshow("Original",imgOriginal);
 // Waiting time 
 char Key = (char)waitKey(300);
 if (Key==27)
 {
 break;
 }
 }
 return 0;
}

Related articles: