4

Let say I have this rose (Do not care about the background, only the white leaves are important).

enter image description here

I transform it to a grayscale picture: grayscaled=cv2.imread('white_rose.png',cv2.IMREAD_GRAYSCALE)

How can I change every white pixel to a red one under the condition the red color (R=255) will have the same contrast as the white one has ? Meaning I want to see the white leaves in red color but with the same L value of every pixel that in grayscaled ?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Change the color space to HSV and then do the appropriate changes. That way, you will be assured of having a consistent contrast value. – scap3y Mar 18 '15 at 15:25
  • thank you. But what do you mean by *the appropriate changes* ? @scap3y –  Mar 18 '15 at 15:37
  • You can refer to the link [here](http://www.tech-faq.com/hsv.html) for more details on the HSV color space. For example, white would have very low S and high V values. You can change the H value there to the one which corresponds to RED and alter S/V value accordingly. – scap3y Mar 18 '15 at 15:49
  • @scap3y thank you, i will give it a try and let you know –  Mar 18 '15 at 15:51
  • *You can change the H value there to the one which corresponds to RED*: How to know that `H` of a *whitish* pixel corresponds to which `H` of red , please ? (yes, I read that article) @scap3y –  Mar 18 '15 at 15:59
  • Okay, so the way _White_ is interpreted in the HSV space is that it can have any Hue (but the saturation of that Hue is very low) with high brightness (V). Does that make things clearer? I am guessing you haven't had much theoretical background on color spaces so might I suggest reading Computer Vision by Hartley-Zisserman? HTH – scap3y Mar 18 '15 at 16:26
  • I knew before about HSV colorspace even if I am not an expert with it, but I do not know how to get the H value of a color from the corresponding H of an other one. @scap3y because each color is defined in its own angel scope –  Mar 18 '15 at 16:30
  • You need to play around with this on your own. Read the [OpenCV documentation](http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor) for details. To start: go through each pixel value and match H,S and V values for white. Replace that with the H value of Red with a higher S. – scap3y Mar 18 '15 at 16:42

1 Answers1

3

You need to loop over your grey image and create a new coloured image by yourself.

For each pixel, you can replace the R value of your coloured image with the remainder of dividing of 255 and relative grey value:

import cv2
import numpy as np

img = cv2.imread('5585T.jpg')
print type(img)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
new=[[[0,0,255%j] for j in i] for i in img_gray]
dt = np.dtype('f8')
new=np.array(new,dtype=dt)

cv2.imwrite('img.jpg',new)

enter image description here

and with new=[[[255%j,255%j,j] for j in i] for i in img_gray] :

enter image description here

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Thank you. But that way, white pixels will be changed to red pixels. I rather want to change to recolor the leaves in red, but by red, I do not mean (0,0,255) because the red color intensity must be the same as L value in the gray scale level of the image –  Mar 18 '15 at 15:08
  • I upvote for the effort, but I can not accept because I see from the result that it is too red (almost dark) in regions where it is almost too white (this means the red must be not too dark in such regions). Thank you very much for the effort to help me –  Mar 18 '15 at 16:57
  • @Kabyle welcome you can change the value yourself. let me edit! – Mazdak Mar 18 '15 at 16:58
  • @Kabyle and sorry for late answer its because installing OpenCV! ;) – Mazdak Mar 18 '15 at 17:06
  • If we take the leaf in the bottom: in the original image, it is lighter on the right and a little bit darker on the rest. The result is opposite in your second test. I think the right solution is around your idea, however –  Mar 18 '15 at 17:09
  • @Kabyle yes you need to play with the `G,B,R` values, any way i thinks this answer can be accept!;) – Mazdak Mar 18 '15 at 17:11
  • 1
    @Kabyle thanks! as a note if you want to get a pic with more quality you need to create your new values based on all the colors and dont convert your image to gray! – Mazdak Mar 18 '15 at 17:18