Ask Your Question
1

Simple raw pixel colour changing

asked 2018-01-18 23:59:35 -0500

Santhosh1 gravatar image

updated 2018-01-19 00:00:30 -0500

I have check already in OpenCV forum, this is not duplicate question.

I want to change a colour in an image, say with [B,G,R] value [49,51,31]

I used a loop to change pixel with these intensity values to white [255,255,255]

Here's a loop in python

for row in range(r):
    for col in range(c):
        if np.all(cim[row,col] == [49,51,31]):
            cim[row,col] = [255,255,255]

It takes around 30 seconds for my current test image of resolution [1908,1429] to complete this.

I came across this post

changing all the Red values in a colour image < 255 to 255

img[img[:, :, 2] < 255, 2] = 255 using numpy fancy indexing

I tried out same for the above condition of mine

cim1[cim1[:,:]==[49,51,31]] =[255,255,255]

But I'm getting this error

cim1[cim1[:,:]==[49,51,31]] =[255,255,255]
ValueError: NumPy boolean array indexing assignment cannot assign 3 input values to the 51068 output values where the mask is true

Any solution? Anyone?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-19 00:48:26 -0500

Santhosh1 gravatar image

Found the solution

OpenCV: setting all pixels of specific BGR value to another BGR value

This was the open line solution of numpy fancy indexing with condition

Copied image

Used np.where

cim1 = cim.copy()
cim1[np.where((cim1==[49,51,31]).all(axis=2))]=[255,255,255]

I just took 0.13535053000123298 seconds

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-18 23:59:35 -0500

Seen: 1,469 times

Last updated: Jan 19 '18