Ask Your Question

How could i detect a color?

asked 2015-03-14 11:17:26 -0500

Mihnea gravatar image

updated 2015-03-14 11:36:56 -0500


I want to make a program that detects the value o a resistor by color code. I made the program like this

  1. In the center of the frame i draw a rectangle
  2. At the middle of the rectangle i draw a line
  3. I get all the pixel from the line and store them RGB value in a vector
  4. I need to detect the color bars from the resistor

I tried to compare the RGB values of red,yellow etc to the RGB values of my pixles from the frame .

Mat src,dst;

Size matSize=src.size();
Point center(matSize.width/2,matSize.height/2);
Point pt1(0,matSize.height/2);
Point pt2(matSize.width,matSize.height/2);

//points for line of scanning

Point ptScan1(center.x-matSize.width/6,center.y);
Point ptScan2(center.x+matSize.width/6,center.y);

//parcurgere linie de scanare
vector<Point3i> bgr_planes;
int codesview[10][30]   = { { 0, 0, 0 }, // black
        { 139, 69, 19 }, // brown
        { 255, 0, 0 }, // red
        { 255, 128, 0 }, // orange
        { 255, 255, 0 }, // yellow
        { 0, 255, 0 }, // green
        { 0, 0, 255 }, // blue
        { 200, 0, 255 }, // violet
        { 128, 128, 128 }, // gray
        { 255, 255, 255 }       // white

Point3i black(0,0,0);
Point3i brown(139,69,19);
Point3i red(255,0,0);
Point3i orange(255,128,0);
Point3i yellow(255,255,0);
Point3i green(0,255,0);
Point3i blue(0,0,255);
Point3i violet(200,0,255);
Point3i gray(128,128,128);
Point3i white(255,255,255);

for(int i=ptScan1.x;i<ptScan2.x;i++)
    Vec3b intensity =<Vec3b>(center.y, i);
    //printf("%d %d %d\n",intensity.val[0],intensity.val[1],intensity.val[2]);

for (int i=0;i<bgr_planes.size();i++)
    printf("b=%d g=%d r=%d \n",bgr_planes[i].x,bgr_planes[i].y,bgr_planes[i].z);

        if (bgr_planes[i].x==0 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        if (bgr_planes[i].x==139 && bgr_planes[i].y==69 && bgr_planes[i].z==19)
        if (bgr_planes[i].x==255 && bgr_planes[i].y==0 && bgr_planes[i].z==0)
        if (bgr_planes[i].x==255 && bgr_planes[i].y==128 && bgr_planes[i].z==0)
        if (bgr_planes[i].x==200 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        if (bgr_planes[i].x==0 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        if (bgr_planes[i].x==0 && bgr_planes[i ...
edit retag flag offensive close merge delete


i am getting an error in the first line ie Math scr,dst...Plz do help

nishashetty131 gravatar imagenishashetty131 ( 2017-09-21 12:00:47 -0500 )edit

what is the error ?

Mat src,dst;
sturkmen gravatar imagesturkmen ( 2017-09-21 12:03:12 -0500 )edit

Mat src,dst ; getting a error here

nishashetty131 gravatar imagenishashetty131 ( 2017-10-04 11:53:23 -0500 )edit

Mat src,dst; invalid syntax is the error im getting

nishashetty131 gravatar imagenishashetty131 ( 2017-10-04 11:55:03 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2015-03-14 11:44:37 -0500

updated 2015-03-16 08:07:53 -0500

You should have a look at the HSV-colorspace (use cv:cvtColor to convert from RGB to HSV). In this space, only the Hue-Channel contains the information on the color, whereas in RGB you need all three values. HSV is also much more resistent against a change of light than RGB. You should also allow a range of colors and not only compare via ==. cv::inrange would be the keyword here.

As a start, you could convert to HSV, get the Hue-values along your horizontal scan line and plot them as graph.

edit flag offensive delete link more


I know i need to give a interval because i cant get the "pure" red or blue from image. If i get the hue values and plot them as graph i get a histogram. From level of the hue histogram i can identify the color?

Mihnea gravatar imageMihnea ( 2015-03-14 12:10:17 -0500 )edit

That's the idea, yes. You could try to detect the transitions between the colors as steep jumps in the plot, compute the mean hue value for this plateau and use this value to label the ring.

FooBar gravatar imageFooBar ( 2015-03-14 12:40:46 -0500 )edit

answered 2015-03-14 15:47:03 -0500

Mihnea gravatar image

Here is the histogram of hue image description

I have too many pixels of red that aren't from bands of the resistor. I cant say where is the red one. With yellow works. What can i do?

edit flag offensive delete link more


Please ref: link text and link text I cant say where is the red one. With yellow works. What can i do?--->in fact, the color space transfer to another in this color space (HSV or other) ,no any define R,G,B

wuling gravatar imagewuling ( 2015-03-15 10:03:40 -0500 )edit

answered 2015-03-14 18:58:31 -0500

Note : Storing the Hue as a 7-bit number instead of an 8-bit number (OpenCV uses 7 bit)

you might be messing up this part for colors.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-03-14 11:03:41 -0500

Seen: 3,569 times

Last updated: Mar 16 '15