, we need to find the red scale of the tape measure in the picture, so we need to filter the image, leaving only the red part.
The first idea is to find the RGB values, and then find a portion of the red area to keep it, but it seems difficult to determine the RGB range of the red area, so you want to convert the image into the HSV space.
It is OK to use the Cvcvtcolor function directly in OpenCV.
[CPP]View PlainCopy
- iplimage* HSV = cvcreateimage (cvgetsize (image), 8, 3);
- Cvcvtcolor (IMAGE,HSV,CV_BGR2HSV);
OpenCV h Range is 0~180, the Red H range is probably (0~8) ∪ (160,180), S is saturation, is generally greater than a value, S is too low is gray (reference value s>80), V is brightness, too low is black, too high is white (reference value 220>v >50).
So the next thing to do is to traverse the image, get the h,s,v component of each pixel point of the image, then make a judgment, satisfy the condition on the reservation, not satisfied with the assignment is black.
I use the iplimage in OpenCV to store pictures.
Iplimage gets the pixel points in the following way:
[CPP]View PlainCopy
- Cvscalar S_HSV = cvget2d (HSV, J, I); //Gets the HSV value for the pixel point (I, J), I is the width value, j is the height value
Iplimage assigns values to pixel points in the following way:
[CPP]View PlainCopy
- Cvscalar s;
- CVSET2D (HSV, J, I, s); //Pair (I,J) at the pixel point assignment
Obtain the H,S,V component separately, pay attention to the image conversion time BGR2HSV, so s.val[0] is the value of B or H, s.val[1] is the value of G or S, s.val[2] is the value of R or v.
Because brother like to use Cvmat, so the input has changed to Cvmat, use when Inputimage is want to filter the picture, Outputimage is the output picture, because Outputimage will be in the function of space application and assignment, So when you pass in a parameter, set it to null directly.
Also note that because it is a color image to do experiments, so if the incoming picture is not a 3-channel color picture, then there will be a memory error.
The following is a single-channel way to open a picture or create a picture, and a memory error occurs.
Iplimage *input = cvloadimage (path, 0),
cvmat* M = Cvcreatemat (4,4,CV_32FC1); Or 8UC1, because C1 represents nchannel = 1, which is a single channel
[CPP]View PlainCopy
- void Colorfilter (Cvmat *inputimage, Cvmat *&outputimage)
- {
- int I, J;
- iplimage* image = Cvcreateimage (Cvgetsize (inputimage), 8, 3);
- Cvgetimage (inputimage, image);
- iplimage* HSV = cvcreateimage (cvgetsize (image), 8, 3);
- Cvcvtcolor (IMAGE,HSV,CV_BGR2HSV);
- int width = hsv->width;
- int height = hsv->height;
- For (i = 0; i < height; i++)
- For (j = 0; J < width; j + +)
- {
- Cvscalar S_HSV = cvget2d (HSV, I, J); //Gets the HSV value of the pixel point as (j, i) point
- /*
- The H range of the OpenCV is 0~180, the Red H range is probably (0~8) ∪ (160,180)
- S is the saturation, is generally greater than a value, S is too low is gray (reference value s>80),
- V is brightness, too low is black, too high is white (reference value 220>v>50).
- */
- Cvscalar s;
- if (! ( ((s_hsv.val[0]>0) && (s_hsv.val[0]<8)) | | (s_hsv.val[0]>120) && (s_hsv.val[0]<180 )))
- {
- S.val[0] = 0;
- s.val[1]=0;
- s.val[2]=0;
- CVSET2D (HSV, I, J, s);
- }
- }
- Outputimage = Cvcreatemat (Hsv->height, Hsv->width, CV_8UC3);
- Cvconvert (HSV, outputimage);
- Cvnamedwindow ("filter");
- Cvshowimage ("filter", HSV);
- Waitkey (0);
- Cvreleaseimage (&HSV);
- }
There is one more thing to note about the function, H component I get is (0,8), (120,180), S and V components do not filter, if follow the comments section of the filter results are not very good.
Results
OpenCV color filter leaves only the red areas in the slices