Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How could i detect a color?

Hello!

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;

dst=imread("D:\\image2.bmp",CV_LOAD_IMAGE_COLOR);
src=dst.clone();
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);
line(src,pt1,pt2,Scalar(0,0,255),1,8,0);
rectangle(src,Point(center.x-matSize.width/6,center.y-matSize.height/4),Point(center.x+matSize.width/6,center.y+matSize.height/4),Scalar(0,0,255),1,8,0);
circle(src,center,2,Scalar(255,255,0),1,8,0);


//points for line of scanning

Point ptScan1(center.x-matSize.width/6,center.y);
Point ptScan2(center.x+matSize.width/6,center.y);
circle(src,ptScan1,2,Scalar(255,255,0),1,8,0);
circle(src,ptScan2,2,Scalar(255,255,0),1,8,0);

//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 = dst.at<Vec3b>(center.y, i);
    bgr_planes.push_back(Point3f(intensity.val[0],intensity.val[1],intensity.val[2]));
    //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)
        {printf("blue\n");}
        else
        if (bgr_planes[i].x==139 && bgr_planes[i].y==69 && bgr_planes[i].z==19)
        {printf("brown\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==0 && bgr_planes[i].z==0)
        {printf("red\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==128 && bgr_planes[i].z==0)
        {printf("orange\n");}
        else
        if (bgr_planes[i].x==200 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        {printf("violet\n");}
        else
        if (bgr_planes[i].x==0 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        {printf("blue\n");}
        else
        if (bgr_planes[i].x==0 && bgr_planes[i].y==255 && bgr_planes[i].z==0)
        {printf("green\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==255 && bgr_planes[i].z==0)
        {printf("yellow\n");}
        else
        if (bgr_planes[i].x==128 && bgr_planes[i].y==128 && bgr_planes[i].z==128)
        {printf("gray\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==255 && bgr_planes[i].z==255)
        {printf("white\n");}
        else
        {printf("\n");}
}

namedWindow("output",CV_WINDOW_AUTOSIZE);
imshow("output",src);
waitKey();
return 0;

How could i get approximate to detect the color bars?

image description

How could i detect a color?

Hello!

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;

dst=imread("D:\\image2.bmp",CV_LOAD_IMAGE_COLOR);
src=dst.clone();
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);
line(src,pt1,pt2,Scalar(0,0,255),1,8,0);
rectangle(src,Point(center.x-matSize.width/6,center.y-matSize.height/4),Point(center.x+matSize.width/6,center.y+matSize.height/4),Scalar(0,0,255),1,8,0);
circle(src,center,2,Scalar(255,255,0),1,8,0);


//points for line of scanning

Point ptScan1(center.x-matSize.width/6,center.y);
Point ptScan2(center.x+matSize.width/6,center.y);
circle(src,ptScan1,2,Scalar(255,255,0),1,8,0);
circle(src,ptScan2,2,Scalar(255,255,0),1,8,0);

//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 = dst.at<Vec3b>(center.y, i);
    bgr_planes.push_back(Point3f(intensity.val[0],intensity.val[1],intensity.val[2]));
    //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)
        {printf("blue\n");}
        else
        if (bgr_planes[i].x==139 && bgr_planes[i].y==69 && bgr_planes[i].z==19)
        {printf("brown\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==0 && bgr_planes[i].z==0)
        {printf("red\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==128 && bgr_planes[i].z==0)
        {printf("orange\n");}
        else
        if (bgr_planes[i].x==200 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        {printf("violet\n");}
        else
        if (bgr_planes[i].x==0 && bgr_planes[i].y==0 && bgr_planes[i].z==255)
        {printf("blue\n");}
        else
        if (bgr_planes[i].x==0 && bgr_planes[i].y==255 && bgr_planes[i].z==0)
        {printf("green\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==255 && bgr_planes[i].z==0)
        {printf("yellow\n");}
        else
        if (bgr_planes[i].x==128 && bgr_planes[i].y==128 && bgr_planes[i].z==128)
        {printf("gray\n");}
        else
        if (bgr_planes[i].x==255 && bgr_planes[i].y==255 && bgr_planes[i].z==255)
        {printf("white\n");}
        else
        {printf("\n");}
}

namedWindow("output",CV_WINDOW_AUTOSIZE);
imshow("output",src);
waitKey();
return 0;

How could i get approximate to detect the color bars?

image description