I am reading a book for image processing algorithms and for the contrast
algorithm it says that I can either go RGB->HSL or RGB->HSI first and
than apply a contrast technique for grayscale images, on the lightness component.
Then it gives this formula only, not other formulas for color conversion:
L(x,y) = 0.299*R(x,y) + 0.587*G(x,y) + 0.114*B(x,y)
This formula is neither for the L in HSL, neither for the I in HSI and that is
what confuses me.
Thanks
It's the luma value mentioned in the fourth bullet here: http://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
Related
can anybody explain the mathematical background and function for conversion of BGR2GRAY?
Under https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html I found the following for RGB to Gray:
RGB[A] to Gray:Y←0.299⋅R+0.587⋅G+0.114⋅B
Is it the same reversed for BGR? Is it really that simple or is there a more complex method behind:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Since the human vision does not receipt all colors equally, the contributions of the primary colors vary. This depends on the wavelengths of the colors. In the following document on page 7 you can find the formula and also some more explanations: http://poynton.ca/PDFs/ColorFAQ.pdf
BGR has been used for OpenCV since back then when it was established a couple companies used BGR instead of RGB. The standard is nowadays RGB. Nontheless, the formula for the transformation is equivalent to Y=0.299*R + 0.587*G + 0.114*B
Assume we have a photo taken under insufficient lighting condition. The image is darker than usual but still recognizable.
Now we want to make it brighter so it looks like taken under sufficient lighting condition.
Should we convert the image into YUV and tune the Y channel (luminance), or convert to HSL and tune the L channel (brightness)?
The wording seems similar to me, while their formula differs a lot:
Y: 0.299*R + 0.587*G + 0.114*B
L: 0.5*(max + min), while max/min is the max/min value among RGB
EDIT:
More specifically, I am going to use opencv, cv2.cvtColor(), but unsure which input argument better suits my need: BGR2YUV or BGR2HLS
Tuning just Y and L (in YCbCr and HSL) will result in loosing information, like contrast between high pixel values. I will suggest either using some affine transformation on Y or L
255*(Y(x,y) - min(Y))/(max(Y) - min(Y))
or best would be to use histogram equalisation. It will not only give bright image, but with better contrast also, so it is good for visualisation
I have to detect leukocytes cells in an image that contains another blood cells, but the differences can be distinguished through the color of cells, leukocytes have more dense purple color, can be seen in the image below.
What color methode I've to use RGB/HSV ? and why ?!
sample image:
Usually when making decisions like this I just quickly plot the different channels and color spaces and see what I find. It is always better to start with a high quality image than to start with a low one and try to fix it with lots of processing
In this specific case I would use HSV. But unlike most color segmentation I would actually use the Saturation Channel to segment the images. The cells are nearly the same Hue so using the hue channel would be very difficult.
hue, (at full saturation and full brightness) very hard to differentiate cells
saturation huge contrast
Green channel, actually shows a lot of contrast as well (it surprised me)
the red and blue channels are hard to actually distinguish the cells.
Now that we have two candidate representations the saturation or the Green channel, we ask which is easier to work with? Since any HSV work involves us converting the RGB image, we can dismiss it, so the clear choice is to simply use the green channel of the RGB image for segmentation.
edit
since you didn't include a language tag I would like to attach some Matlab code I just wrote. It displays an image in all 4 color spaces so you can quickly make an informed decision on which to use. It mimics matlabs Color Thresholder colorspace selection window
function ViewColorSpaces(rgb_image)
% ViewColorSpaces(rgb_image)
% displays an RGB image in 4 different color spaces. RGB, HSV, YCbCr,CIELab
% each of the 3 channels are shown for each colorspace
% the display mimcs the New matlab color thresholder window
% http://www.mathworks.com/help/images/image-segmentation-using-the-color-thesholder-app.html
hsvim = rgb2hsv(rgb_image);
yuvim = rgb2ycbcr(rgb_image);
%cielab colorspace
cform = makecform('srgb2lab');
cieim = applycform(rgb_image,cform);
figure();
%rgb
subplot(3,4,1);imshow(rgb_image(:,:,1));title(sprintf('RGB Space\n\nred'))
subplot(3,4,5);imshow(rgb_image(:,:,2));title('green')
subplot(3,4,9);imshow(rgb_image(:,:,3));title('blue')
%hsv
subplot(3,4,2);imshow(hsvim(:,:,1));title(sprintf('HSV Space\n\nhue'))
subplot(3,4,6);imshow(hsvim(:,:,2));title('saturation')
subplot(3,4,10);imshow(hsvim(:,:,3));title('brightness')
%ycbcr / yuv
subplot(3,4,3);imshow(yuvim(:,:,1));title(sprintf('YCbCr Space\n\nLuminance'))
subplot(3,4,7);imshow(yuvim(:,:,2));title('blue difference')
subplot(3,4,11);imshow(yuvim(:,:,3));title('red difference')
%CIElab
subplot(3,4,4);imshow(cieim(:,:,1));title(sprintf('CIELab Space\n\nLightness'))
subplot(3,4,8);imshow(cieim(:,:,2));title('green red')
subplot(3,4,12);imshow(cieim(:,:,3));title('yellow blue')
end
you could call it like this
rgbim = imread('http://i.stack.imgur.com/gd62B.jpg');
ViewColorSpaces(rgbim)
and the display is this
in DIP and CV is this always a valid question
But it has no universal answer because each task is unique so use what is better suited for it. To choose correctly you need to know the pros/cons of each so here is some summary:
RGB
this is easy to handle and you can easyly access r,g,b bands. For many cases is better to check just single band instead of whole color or mix the colors to emphasize wanted feature or even dampening unwanted one. It is hard to compare colors in RGB due to intensity encoded into bands directly. To remedy that you can use normalization but that is slow (need per pixel sqrt). You can do arithmetics on RGB colors directly.
Example of task better suited for RGB:
finding horizont in high altitude photo
HSV
is better suited for color recognition because CV algorithms using HSV has very similar visual perception to human perception so if you want to recognize areas of distinct colors HSV is better. The conversion between RGB/HSV takes a bit of time which can be for big resolutions or hi fps apps a problem. For standard DIP/CV tasks is this usually not the case.
Example of task better suited for HSV:
Compare RGB colors
Take a look at:
HSV histogram
to see the distinct color separation in HSV. The segmentation of image based on color is easy on HSV. You can not do arithmetics on HSV colors directly instead need to convert to RGB and back
Can anybody explain to me why the hue value of an HSV image in OpenCV only goes to 180° and not the full 360°?
I have found somewhere that OpenCV uses a 180° cylinder, but I can not really visualize such a cylinder.
Thanks in advance!
J
try to put 360 into a uchar ;)
so, it's just divided by 2 to make it fit..
The ranges that OpenCV manage for HSV format are the following:
For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. Different softwares use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges.
Here is the link to the OpenCV documentation that explains it.
http://docs.opencv.org/3.2.0/df/d9d/tutorial_py_colorspaces.html
According to http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor
For the 8-bit images, H is converted to H/2 to fit to the [0,255] range. So the range of hue in the HSV color space of OpenCV is [0,179]
Is it really so?I think for HSV the ranges are as H[0-179], S[0-255], V[0-255].Please see the link and help me understand if I am missing something. http://docs.opencv.org/trunk/doc/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html
If you need to convert the range of Hue, see the link below.
http://en.literateprograms.org/RGB_to_HSV_color_space_conversion_%28C%29#
I'd like design a chart and set the colors
from a single exemplar. Same way as in Excel's:
Is there some sort of a formula or algorithm to
generate the next shade of color from a given
shade or color?
That looks to me like they just took the same hue (basic color) and turned the brightness up and down. That can be done easily enough with a HSL or HSV transformations. Check Wikipedia for HSL and HSV color spaces to get some understanding of the theory involved.
Basic idea: Computers represent color with a mixture of red intensity, green intensity and blue intensity, called RGB, because that's the way the screen displays color. HSL (Hue, Saturation, Lightness) and HSV (Hue, Saturation, Value) are two alternative models for representing color that are more intuitive and closer to the way human beings tend to think about how colors look.
Hue is the basic color, represented (more or less) as an angle on a color wheel. Saturation is a linear value, from 0 (gray) to 255 (bright, vibrant color). And Lightness/Value represent brightness, from 0 (black) to 100 (white).
The algorithms to transform from RGB -> HSL and HSL -> RGB (or HSV instead of HSL) are pretty straightforward. Try transforming your color to HS*, adjusting the brightness, and transforming back. By taking several different brightness values from low to high, and arranging them as wedges in a pie chart, you can duplicate that picture pretty easily.
Look into the HSV colour space. Using it you can produce different shades or tints starting from a given colour. There is a page with Pascal / Delphi code for conversion between RGB and HSV at efg's Computer Lab.
Roderick , the #mghie links are great to start, additionally try out the Colorlib Delphi Library , wich lets you convert between color models as well as HTML color conversion utilities. is very complete, full source code included and freeware ;).
check the demo application , in this image you can see a blue pallete generated using this library.