Image processing-YUV to Rgb - matlab

Why does one convert from YUV to RGB , what is the advantage in image processing using matlab of doing such a conversion. I know the answer partially that Y is the light component which gets eliminated in RGB format? what is the basis of such conversions?

I'll tell you what you could have easily found on the internet:
YUV was introduced when colour tvs came up. there should be minimum interference with existing monochrome tvs. so they added color information uv to the luminance signal y.
Due to the way digital colour images are captured (using red, green or blue pass-filtered pixels) the native colour space for digital images is RGB.
Modern displays also use red, green and blue pixels.
For printing you will find YMCK colour space as printing.
Nowadays RGB is the default colour space in digital image processing as we usually process the raw image information. you won't find many algorithms that can handle YUV images directly.

Related

Matlab : ROI substraction

I'm learning about statistical feature of an image.A quote that I'm reading is
For the first method which is statistical features of texture, after
the image is loaded, it is converted to gray scale image. Then the
background is subtracted from the original image. This is done by
subtract the any blue intensity pixels for the image. Finally, the ROI
is obtained by finding the pixels which are not zero value.
The implementation :
% PREPROCESSING segments the Region of Interest (ROI) for
% statistical features extraction.
% Convert RGB image to grayscale image
g=rgb2gray(I);
% Obtain blue layer from original image
b=I(:,:,3);
% Subtract blue background from grayscale image
r=g-b;
% Find the ROI by finding non-zero pixels.
x=find(r~=0);
f=g(x);
My interpretation :
The purpose of substracting the blue channel here is related to the fact that the ROI is non blue background? Like :
But in the real world imaging like for example an object but surrounded with more than one colors? What is the best way to extract ROI in that case?
like for example (assuming only 2 colors on all parts of the bird which are green and black, & geometri shaped is ignored):
what would I do in that case? Also the picture will be transformed to gray scale right? while there's a black part of the ROI (bird) itself.
I mean in the bird case how can I extract only green & black parts? and remove the rest colors (which are considered as background ) of it?
Background removal in an image is a large and potentielly complicated subject in a general case but what I understand is that you want to take advantage of a color information that you already have about your background (correct me if I'm wrong).
If you know the colour to remove, you can for instance:
switch from RGB to Lab color space (Wiki link).
after converting your image, compute the Euclidean from the background color (say orange), to all the pixels in your image
define a threshold under which the pixels are background
In other words, if coordinates of a pixel in Lab are close to orange coordinates in Lab, this pixel is background. The advantage of using Lab is that Euclidean distance between points relates to human perception of colours.
I think this should work, please give it a shot or let me know if I misunderstood the question.

Create a satellite true color image using Matlab

I'm trying to create a true color RBG image from satellite data using matlab, but I don't know how to do it.
The false color RGB image is simple, just employing the right channels for the red, green and blue you can make it
RGB(:,:,1)=(ref16)'; %red - reflectance 1.6mic
RGB(:,:,2)=(ref06)'; %green - reflectance 600nm
RGB(:,:,3)=(ref05)'; %blue - reflectance 500nm
image(RGB)
In this case I'm using reflectances from the satellite channels which range from 0 to 1, so I don't need to modify the original data
But I'm having so much trouble when I try to plot true color images.
According to literature, the following profile should yield good RGB images from MERIS Level-1b data products (the data I'm using). The linear-combinations for the red, green and blue components are based on the colour matching functions of the CIE 1931 color space.
RGB(:,:,1)=log(1.0+0.35*radiance_2+0.60*radiance_5+radiance_6+0.13*radiance_7)'
RGB(:,:,2)=log(1.0+0.21*radiance_3+0.50*radiance_4+radiance_5+0.38*radiance_6)'
RGB(:,:,3)=log(1.0+0.21*radiance_1+1.75*radiance_2+0.47*radiance_3+0.16*radiance_4)'
Radiance are real values going from 0 to 400 (with the scale factor applied), so I guess that I have to normalize the RGB array (0-1 or 0-255) to create the image.
But doing the normalization myself or just using im2uint8 doesn't produce the right image.
It's likely that I'm doing everything wrong because I'm not familiar with colour profiles. Is there a way in matlab to create the image using directly the CIE rgb combination (the one I think I'm getting from the above formulas)?
Is anyone out there familiar with images using matlab and satellite data?
Thanks!

black&white to color image in Matlab

I am doing simple image processing in matlab. I turned my original image (jpg) into black and white image with the function im2bw and I did some modifications on this image. Do you know if it is possible to turn again this image to the original colors?
Grayscaling an image is a one-way function. Without the original data, you have no way of determining the hue of the color used before it was converted to grayscale, and so there is actually loss of data in the conversion.

Block artifact in converting RGB 2 HSV

I would like to convert an image from RGB space to HSV in MATLAB and use the Hue.
However, when I use 'rgb2hsv' or some other codes that I found in the internet the Hue component has block artifacts. An example of the original image and the block artifact version are shown below.
Original
Hue
I was able to reproduce your error. For those of you who are reading and want to reproduce this image on your own end, you can do this:
im = imread('http://i.stack.imgur.com/Lw8rj.jpg');
im2 = rgb2hsv(im);
imshow(im2(:,:,1));
This code will produce the output image that the OP has shown us.
You are directly using the Hue and showing the result. You should note that Hue does not have the same interpretation as grayscale intensity as per the RGB colour space.
You should probably refer to the definition of the Hue. The Hue basically refers to how humans perceive the colour to be, or the dominant colour that is interpreted by the human visual system. This is the angle that is made along the circular opening in the HSV cone. The RGB colour space can be represented as all of its colours being confined into a cube. It is a 3D space where each axis denotes the amount of each primary colour (red, green, blue) that contributes to the colour pixel in question. Converting a pixel into HSV, also known as Hue-Saturation-Value, converts the RGB colour space into a cone. The cone can be parameterized by the distance from the origin of the cone and moving upwards (value), the distance from the centre of the cone moving outwards (saturation), and the angle around the circular opening of the cone (hue).
This is what the HSV cone looks like:
Source: Wikipedia
The mapping between the angle of the Hue to the dominant / perceived colour is shown below:
Source: Wikipedia
As you can see, each angle denotes what the dominant colour would be. In MATLAB, this is scaled between [0,1]. As such, you are not visualizing the Hue properly. You are using the Hue channel to directly display this result as a grayscale image.
However, if you do a scan of the values within this image, and multiply each result by 360, then refer to the Hue colour table that I have shown above, this will give you a representation of what the dominant colours at these pixel locations would be.
The moral of this story is that you can't simply use the Hue and visualize that result. Converting to HSV can certainly be used as a pre-processing step, but you should do some more processing in this domain before anything fruitful is to happen. Looking at it directly as an image is pretty useless, as you have seen in your output image. What you can do is use a colour map that derives a relationship between hue and colour like in the Hue lookup map that I showed you, and you can then colourize your image but that's really only used as an observational tool.
Edit: July 23, 2014
As a bonus, what we can do is display the Hue as an initial grayscale image, then apply an appropriate colour map to the image so we can actually visualize what each dominant colour at each location looks like. Fortunately, there is a built-in HSV colour map that is pretty much the same as the colour lookup map that I showed above. All you would have to do is do colormap hsv right after you show the Hue channel. We can show the original image and this colourized image side-by-side by doing:
im = imread('http://i.stack.imgur.com/Lw8rj.jpg');
im2 = rgb2hsv(im);
subplot(1,2,1);
imshow(im); title('Original Image');
subplot(1,2,2);
imshow(im2(:,:,1)); title('Hue channel - Colour coded');
colormap hsv;
This is what the figure looks like:
The figure may be a bit confusing. It is labelling the sky as being blue as the dominant colour. Although this is confusing, this makes actual sense. On a clear day, the sky is blue, but the reason why the sky appears gray in this photo is probably due to the contributions in saturation and value. Saturation refers to how "pure" the colour is. As an example, true red (RGB = [255,0,0]), means that the saturation is 100%. Value refers to the intensity of the colour. Basically, it refers to how dark or how light the colour is. As such, the saturation and value would most likely play a part here which would make the colour appear gray. The few bits of colour that we see in the image is what we expect how we perceive the colours to be. For example, the red along the side of the jet carrier is perceived as red, and the green helmet is perceived to be green. The lower body of the jet carrier is (apparently) perceived to be red as well. This I can't really explain to you, but the saturation and value are contributing to the mix so that the overall output colour is about a gray or so.
The blockiness that you see in the image is most likely due to JPEG quantization. JPEG works great in that we don't perceive any discontinuities in smooth regions of the image, but the way the image is encoded is that it reconstructs it this way... in a method that will greatly reduce the size it takes to save the image, but allow it to be as visually appealing as if you were to look at the RAW image.
The moral of the story here is that you can certainly use Hue as part of your processing chain, but it is not the entire picture. You will probably need to use saturation or value (or even both) to help you discern between the colours.

how to detect colour from an image matlab?

we are doing a mat lab based robotics project.which actually sorts objects based on its color so we need an algorithm to detect specific color from the image captured from a camera using mat lab.
it will be a great help if some one can help me with it.its the video of the project
In response to Amro's answer:
The five squares above all have the same Hue value in HSV space. Selecting by Hue is helpful, but you'll want to impose some constraints on Saturation and value as well.
HSV allows you to describe color in a more human-meaningful way, but you still need to look at all three values.
As a starting point, I would use the rgb space and the euclidian norm to detect if a pixel has a given color. Typically, you have 3 values for a pixel: [red green blue]. You also have also 3 values defining a target color: [255 0 0] for red. Compute the euclidian norm between those two vectors, and apply a decision threshold to classify the color of your pixel.
Eventually, you want to get rid of the luminance factor (i.e is it a bright red or a dark red?). You can switch to HSV space and use the same norm on the H value. Or you can use [red/green blue/green] vectors. Before that, apply a low pass filter to the images because divisions (also present in the hsv2rgb transform) tend to increase noise.
You probably want to convert to the HSV colorspace, and detect colors based on the Hue values. MATLAB offers the RGB2HSV function.
Here is an example submission on File Exchange that illustrate color detection based on hue.
For obtaining a single color mask, first of all convert the rgb image gray using rgb2gray. Also extract the desired color plane from the rgb image ,(eg for obtaining red plain give rgb_img(:,:,1)). Subtract the given plane from the gray image........