Ask Your Question
0

How do I remove all but one color in an image (Python)?

asked 2013-05-02 23:44:31 -0600

smhall05 gravatar image

updated 2013-05-03 02:02:33 -0600

NightLife gravatar image

In GIMP I am able to do the following:

Tools -> Selection Tools -> By Color Select (Shift+O)

Click on the single color I want to keep

Select -> Invert (Shift+I)

Edit -> Clear (Delete)

This allows me to remove all but the one color I clicked on. How can I do this in Python with opencv?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-05-03 03:02:15 -0600

updated 2013-05-03 03:03:07 -0600

The idea is to create a mask, where it's 0 everywhere, except if the original color is the same as the reference color. After, you copy the image, or, you can directly use the dst image to put the right color in it. The code below is something like this. I suppose source image is in CV_8UC3, aka BGR color space.

/// With the mask
Vec3b colorRef(255,0,0); // for ''pure'' blue
int i,j;
Mat mask = Mat::zeros( img.rows, img.cols, CV_8UC1 );
for( i = 0; i < nRows; ++i)
{
    for ( j = 0; j < nCols; ++j)
    {
        if( img.at<Vec3b>(i,j) == colorRef )
        {
            mask.at<uchar>(i,j) = 255;
        }
    }
}
Mat dst;
img.copyTo( dst, mask );

/// Without the mask
Vec3b colorRef(255,0,0); // for ''pure'' blue
int i,j;
Mat dst;
img.copyTo( dst );
for( i = 0; i < nRows; ++i)
{
    for ( j = 0; j < nCols; ++j)
    {
        if( img.at<Vec3b>(i,j) != colorRef )
        {
            dst.at<Vec3b>(i,j) = 0;
        }
    }
}

If you want to be flexible for the color, not using a perfect equality, you have to consider the color distance, which is not the best in RGB cube, therefore, you need to change of color space, like YUV or anything else.

[EDIT] I code it in C++, but it's almost the same in Python...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-05-02 23:44:31 -0600

Seen: 9,810 times

Last updated: May 03 '13