Get pixel values from image: Matlab - image

I have a simple green fluorescent image. I want to find the total number of pixels that are above a specific value using MATLAB. I don't know where the pixel values are stored in an image.
Here is the green fluorescent image. I want to know which percentage of the pixels have value of more than a specific threshold. For example in this image, if the pixel value in the cells are around X, then I want to find the total number of pixels that are above X.

If you read a colored image using imread, you get a 3D matrix in which the first two indices are the image coordinates; (row, columns); and the last index represents the color channels. For the typical use case of an RGB image, the color channels are:
1 = red
2 = green
3 = blue.
Other possibilities are grayscale, CMYK and indexed images. Please check the official documentation for more information.

Related

Finding out number of pixels in white area in binary image as well as number of pixels of ROI in original image in MATLAB

I have segmented image in which my region of interest (ROI) was white color cotton. Now I want to compare the number of pixels in segmented area i.e. total number of pixels in white blob in binary image with actual number of pixels of ROI in actual image. How I can do that. Following figure can clear the point.
As we can see from original image, my ROI was white color cotton circled in red boundry. When I segmented this image I got binary image as shown. As we can noticed there are some missing areas in binary image as compare to original area. So, I want to count the number of pixels in original image of ROI and number of pixels of white blob in binary image. So that I can calculate difference in actual pixels of ROI and actual segmented number of pixels.
Thank You.
If you wish to not draw the boundaries yourself, you can try this. It might not be as precise as you need, but you might get close to the actual value by tweaking with the thresholding values I used (100 for all 3 channels in this case).
Assume I is your original image. First create the binary mask by thresholding with the RGB values. Then remove all the small objects that don't have at least a 2000 pixel area. Then sum up the pixels of that object.
IT = I(:,:,1) > 100;
IT(I(:,:,2) < 100) = 0;
IT(I(:,:,3) < 100) = 0;
IT = bwareaopen(IT, 2000);
sum(IT(:) > 0)
21380
Resulting image:

Why Red, Green, Blue channels of image separetely are grayscaled (Matlab)?

Matlab stores the image as a 3-dimensional array. The first two dimensions correspond to the numbers on the axis of the above picture. Each pixel is represented by three entries in the third dimension of the image. Each of the three layers represents the intensity of red, green, and blue in the array of pixels. We can extract out the independent red-green-blue components of the image by:
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
For example original image is:
If you display red green and blue channels you get these grayscaled images:
If you concatenate one of these channels with two black matrices (zero matrices) you get colored image. Let us concatenate each channel with black image matrices for remaining channels:
blackImage = uint8(zeros(rows, columns));
newRedChannel = cat(3, redChannel, blackImage, blackImage);
newGreenChannel = cat(3, blackImage, greenChannel, blackImage);
newBlueChannel = cat(3, blackImage, blackImage, blueChannel);
It outputs the following images:
Why does it work this way? Why individual channels for each color have to be concatenated with zero matrices (black images) for remaining channels in order for it be be colored when displaying? And why individual channels of colors are actually just grayscaled images if displayed individually?
In MATLAB, greyscale images are represented as 2D matrices. Colour images as 3D matrices. A red image is still a colour image hence it still has to be a 3D image. Since is it a purely red image and thus has no other colours, the green and blue channels should be empty hence matrices of zeros.
Note also that when we say greyscale, we are really referring to an indexed image. So you can make this a colour image by applying a colourmap. If you were to apply a colourmap that ranges from black to red, then your 2D matrix would display as your red image above. The question is, how would MATLAB know what colourmap to apply? Yes, this does take up less space than a colour image. But it increases the complexity of your code.
On the other, ask yourself what you would expect an image to look like if you set two of the colour channels to zero? The only logical answer is exactly the separate colour channel images you have created above.
If you like, try an rephrase your question to "how else could MATLAB have implemented this?". In other words, if your red channel was a 2D image, how would MATLAB know it is a red channel and not a green channel or a greyscale image? Challenge yourself to think of a more practical way to represent these data. I think this exercise will convince you of the merit's of MATLAB's design choice in this matter.
Also it is worth noting that many file formats, e.g. .bmp, work the same way.

Resizing an image to fit around an isolated object in MATLAB

I am working with RGB images that contain a single object against a monochrome background.
My goal is to isolate the object in the image and resize the image to contain only the object.
I have successfully been able to detect the object by converting the image to a binary image using an appropriate threshold. Then, in order to isolate the object in the original RGB image I use the binary image as a mask with the original RGB image.
maskedImage = bsxfun(#times,originalimage, cast(binaryimage,class(originalimage)));
This leaves me with a image only containing the object surrounded by a black background. This is due to the fact that the binary image mask I used contained the object in white pixels and the background in black pixels and since possess intensity values of 0 the masking process converted all pixels that didn't belong to the object to black pixels. I've attached an example below.
I would now like to draw a bounding box around the object and resize the image to the size of the bounding box, so that I can get rid as much of the surrounding black pixels as possible. Is there any way of doing this? Any help would be appreciated.
Given the segmented image, you want to crop out all of the black pixels and provide the closest bounding box that fully encapsulates the object. That's very simple.
You already have a binary mask that determines what is an object and what's background. You simply need to find the minimum spanning bounding box. You can find the top-left and bottom right corner by obtaining all of the pixel locations that are non-zero in the mask, and finding the minimum and maximum row and column coordinates. You'd then just use these to crop out the segmented image.
As such:
%// Find all non-zero locations in the mask
[row,col] = find(binaryImage);
%// Find the top left corner of the mask
topLeftRow = min(row);
topLeftCol = min(col);
%// Find the bottom right corner of the mask
bottomRightRow = max(row);
bottomRightCol = max(col);
%// Extract the object
extracted = maskedImage(topLeftRow:bottomRightRow, topLeftCol:bottomRightCol, :);
The words of the day are Bounding boxes !
If you want the minimum-area rectangle to crop only the nonzero values, you want the bounding box of your region, then set your phasers to stun and you're all set !
See this Matlab help forum question for more implementation details in Matlab.

Matlab: How can I find the number of black pixels in a color image?

I wish to find the number of black pixels in a color image using MATLAB, how can I do it?
Thanks.
Use nnz like this -
nnz(all(im==0,3))
where im is your image data.
Alternatives - sum(reshape(all(im==0,3),[],1)) and sum(sum(all(im==0,3))).
The assumption here is that black pixels are triplets (0,0,0).
Instead if you define black pixels as pixels that have values in the interval [0 th] for the same pixel location across all channels, use this -
nnz(all(im<=th,3))

White binary image in Matlab

in Matlab,
if I do:
output = false(5, 5);
imshow(output);
it will show me a black square instead of a white binary square image. Is there any reason to this? How can I output a white binary square?
The reason is that false is mapped to 0, and true is mapped to 1.
Also, when showing images, higher number is shown by higher intensity. White has more intensity than black.
Another way to think about it, is that usually you have 256 values - 0-255. 0 is totally black and 255 is totally white. Now, imagine that you do a quantization to two colors. It is now obvious that 0 should be black.
In order to show white square, use
output = true(5,5)
You could use imcomplement
imshow(imcomplement(false(5, 5)))
or modify the default color mapping (quoting from imshow's documentation)
imshow(X,map)
displays the indexed image X with the colormap map. A color map matrix may have any number of rows, but it must have exactly 3 columns. Each row is interpreted as a color, with the first element specifying the intensity of red light, the second green, and the third blue. Color intensity can be specified on the interval 0.0 to 1.0.
You could also change the figure's colormap to customize how MATLAB maps values to colors:
BW = [false,true;true,false];
imshow(BW)
set(gcf, 'Colormap',[1,1,1;0,0,0])

Resources