Ask Your Question
0

Convert all pixels of one color to another in JAVA

asked 2017-06-22 02:40:45 -0600

arqam gravatar image

I have an image and have to convert all the red color to green color. And I am stuck on this part, as I cannot find any function in openCV which specifically does that.

The approach that is coming in my mind is iterating through each pixel of the Matrix and convert the color as and when it comes. But it will be easier if there is some in built method which I cannot find.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-06-22 03:08:56 -0600

berak gravatar image

updated 2017-06-22 03:10:16 -0600

  1. use inRange to make a binary mask from your image. (wherever your image is red, the mask should be white, else black)
  2. Mat.setTo() , using this mask, to update the color, wherever the mask is white.

(and yes, please forget about iterating over pixels in general, especially from java)

edit flag offensive delete link more

Comments

then you invert (the color of) your Mask by:

Core.subtract(new Scalar(255), mask, mask); // mask = 255 - mask;

and again use setTo() with the inverted mask, and another color.

berak gravatar imageberak ( 2017-06-22 05:22:01 -0600 )edit

In the inrange function if we have an image which has various shades of brown then how can we decide the lower bound and upper bound?

arqam gravatar imagearqam ( 2017-06-22 05:50:13 -0600 )edit

maybe convert to HSV first, and look mainly at Hue (careful: [0..180] range in opencv)

berak gravatar imageberak ( 2017-06-22 06:03:17 -0600 )edit

Can you please show by an example on how to select a range of color like shades of brown/black as even in HSV its having 3 parameters and in an image there are many shades of the same color, so how will the range be decided with one being lower and other being higher.

arqam gravatar imagearqam ( 2017-06-22 06:36:57 -0600 )edit

sorry, i do not have java here.

berak gravatar imageberak ( 2017-06-22 06:49:51 -0600 )edit

Right now what I am doing is I took an average color of brown from the image, saw the RGB value and then for lower index I am subtracting 20 and for higher I am adding 20 from all RGB. It does give shape in some form. If my approach is wrong then please inform me.

arqam gravatar imagearqam ( 2017-06-22 07:40:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-06-22 02:40:45 -0600

Seen: 2,119 times

Last updated: Jun 22 '17