Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changes Brightness in color replacement

I am working on replacing certain color in image by User's selected color. I am using OpenCV for color replacement.

Here in short I have described from where I took help and what I got.

  1. How to change a particular color in an image? I have followed the step or taken basic idea from answer of above link. In correct answer of that link that guy told you only need to change hue for colour replacement.

  2. after that I run into the issue similar like color replacement in image for iphone application (i.e. It's good code for color replacement for those who are completely beginners)
    from that issue I got the idea that I also need to change "Saturation" also.


Now I am running into issues like

"When my source image is too light(i.e. with high brightness) and I am replacing colour with some dark colour then colours looks light in replaced image instead of dark due to that it seems like Replaced colour does not match with colour using that we done replacement"

This happens because I am not considering the brightness in replacement. Here I am stuck what is the formula or idea to change brightness?

Suppose I am replacing the brightness of image with brightness of destination colour then It would look like flat replacemnt and image will lose it's actual shadow or edges.

Edit:
When I am considering the brightness of source(i.e. the pixel to be processed) in replacment then I am facing one issue. let me explain as per scenario of my application.

for example I am changing the colour of car(like whiteAngl explain) after that I am erasing few portion of the newly coloured car. Again I am doing recolour on erased portion but now what happended is colour done after erase and colour before erase doesn't match because both time I am getting different lightness because both time my pixel of to be processed is changed and due to that lightness of colour changed in output. How to overcome this issue

Any help will be appreciated

Coloured ImageErased Few portionRecoloured

like image 682
Iducool Avatar asked Mar 31 '13 17:03

Iducool


1 Answers

Without seeing the code you have tried, it's not easy to guess what you have done wrong. To show you with a concrete example how this is done let's change the ugly blue color of this car:

enter image description here

This short python script shows how we can change the color using the HSV color space:

import cv2
orig = cv2.imread("original.jpg")
hsv = cv2.cvtColor(orig, cv2.COLOR_BGR2HSV)
hsv[:,:,0] += 100
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite('changed.jpg', bgr)

and you get: enter image description here

On wikipedia you see the hue is between 0 to 360 degrees but for the values in OpenCV see the documentation. You see I added 100 to hue of every pixel in the image. I guess you want to change the color of a portion of your image, but probably you get the idea from the above script.

Here is how to get the requested dark red car. First we get the red one: red card

The dark red one that I tried to keep the metallic feeling in it: dark red car

As I said, the equation you use to shift the light of the color depends on the material you want to have for the object. Here I came up with a quick and dirty equation to keep the metallic material of the car. This script produces the above dark red car image from the first light blue car image:

import cv2
orig = cv2.imread("original.jpg")
hls = cv2.cvtColor(orig, cv2.COLOR_BGR2HLS)
hls[:,:,0] += 80 # change color from blue to red, hue
for i in range(1,50): # 50 times reduce lightness 
 # select indices where lightness is greater than 0 (black) and less than very bright 
 # 220-i*2 is there to reduce lightness of bright pixel fewer number of times (than 50 times), 
 # so in the first iteration we don't reduce lightness of pixels which have lightness >= 200, in the second iteration we don't touch pixels with lightness >= 198 and so on
 ind = (hls[:,:,1] > 0) & (hls[:,:,1] < (220-i*2))
 # from the lightness of the selected pixels we subtract 1, using trick true=1 false=0
 # so the selected pixels get darker
 hls[:,:,1] -= ind
bgr = cv2.cvtColor(hls, cv2.COLOR_HLS2BGR)
cv2.imwrite('changed.jpg', bgr)
like image 137
fireant Avatar answered Nov 16 '22 22:11

fireant