Masked image displaying wrong intensity - image

I have a 3D image and a mask, both size [256x256x160]. The mask is a binary image of 0 and 255. After applying the mask on the image like so:
masked_image = image.*mask;
I get a masked_image that has totally wrong intensity values. In fact, its values are nowhere to be found on the original image. They are just too big. All I want to achieve is extract the original image's intensities at the location of the mask and calculate the mean value of that ROI. I can see from imshow that the mask is aligned properly on the image. However, applying the mask is unsuccessful and I don't understand why.

Your mask shouldn't be scaled from 0 to 255. It should contain only 0 or 1 to use it the way you want. Any one of these alternatives would work:
masked_image = image.*logical(mask);
% Or...
masked_image = image.*(mask > 0);
% Or...
masked_image = image.*(mask./255);

Converting the mask to 0 and 1 rather than 0 and 255 solved the problem.
The extracted ROIs now have the correct intensity.
Thanks for the clarification.

Related

How to filter out the background in an RGB image(Matlab)? [duplicate]

I have converted my image into a mask and I would now like to obtain what the original colours were given this mask. I have an array called objectPixels that determines which locations belong to the object I am segmenting out. I have tried the code below but I am not obtaining the original colours.
What am I doing wring?
mask = false(size(grayImage));
mask(objectPixels) = true;
%on R channel
tmp = originalImage(:,:,1);
If I understand your question properly, you have a mask of pixels that belong to some objects. You now wish to find an image where any pixels that are labeled true will provide the original colour at these locations while false we skip. I'm going to assume that the output pixels are black if the mask locations are false. This can easily be computed using bsxfun with times as the function. We would essentially replicate the mask for each colour channel in your image, then multiply the mask with the original image.
As such:
out = bsxfun(#times, originalImage, cast(mask, class(originalImage)));
mask is originally a logical array, and in order to multiply both the mask and your original image together, they must be the same type, and that's why cast is used so that we can cast the image to the same type as the original image. We use class to determine the class or type of the original image.
As an example, let's use the onion.png image that's part of MATLAB's system path. I'm going to convert this image to grayscale using rgb2gray then choose an arbitrary threshold of graylevel 100 to give us a mask. Anything greater than 100 will give a mask value of true, while anything else is set to false.
Once I generate this mask, let's figure out what the original colours were based on these mask values. As such:
originalImage = imread('onion.png');
mask = rgb2gray(originalImage) >= 100;
out = bsxfun(#times, originalImage, cast(mask, class(originalImage)));
%// Show the images now
figure;
subplot(1,3,1);
imshow(im);
title('Original Image');
subplot(1,3,2);
imshow(mask);
title('Mask');
subplot(1,3,3);
imshow(out);
title('Output Image');
With the above code, I implement the logic I was talking about, with an additional figure that shows the original image, the mask generated as well as the output image that shows you the original colours of where the mask locations were true.
This is what I get:

thresholding grayscale image without converting binary image

I would like to know how can ı achieve thresholding based on grayscale intensity by not converting it to binary image. As an example, below 50 intensity will be 0 while 50-255 intensity values remain the same(in Matlab).
Check the following...
Read sample image:
I = imread('cameraman.tif');
Set all values below 50 to zero.
I(I < 50) = 0;

count number of non gray pixels in RGB image in matlab

I have a RGB image which has only black and white squares. I want to count number to non gray pixels in this image. I am new to matlab. I want to check the quality of image as it should only contain black and white pixels.Actually I have undistorted this image due that some colored fringes are appeared.I want to know the how many color are introduced to check the quality of the image.
using matlab to get counts of specific pixel values in an image.
Images are RGBA <512x512x4 uint8> when read into matlab (although we can disregard the alpha channel).
Something like this
count = sum(im(:, :, 1) == 255 & im(:, :, 3) == 255 & im(:, :, 3) == 255);
will give you the count of such pixels. Replace sum with find to get the indices of those pixels if you need that.
A pixel is said to be gray if its R,G,B components are all same.
Using this logic
%// checking for equality of R,G,B values
B = any(diff(im,[],3),3); %// selecting only non-gray pixels
count = sum(B(:)); %// Number of non-gray pixels
PS: This answer is tailored from this and this answer.

Converting array into grayscale in matlab

I am trying to plot a set of data in grayscale. However, the image i get seems to be always blue.
I have a set of data, albedo that ranges from [0, 0.068], which is a 1X1 double.
My code is:
for all px,py
albedoMax = 0.0679; albedoMin = 0;
out_im(px,py) = 1/(albedoMax-albedoMin)*(albedo - albedoMin);
imshow(out_im);
drawnow;
end
Basically px,py are the image coordinates that i have to iterate over, and the formula is trying to map the input range of [0, 0.068] to [0 1]. However, by running this code, i notice that the output is always blueish. I was wondering what went wrong.
Thanks for the help.
Can't you make use of the rgb2gray function?
What you are making is one layer of the RGB image.
If you are creating a homogeneous blue image with constant color then the normalization is wrong. But if it is just the matter of being blue instead of being gray then just convert it using :
ImGray = rgb2gray(Im);
Do not forget to distribute the pixels like a grid/mesh, to fill all the image not just a part of it.

binary or contiuous image?

I have an image which I want to use it in MATLAB. But, I am looking for a method by which I be able to automatically find that my image is binary (0 and 1) or continuous. Is there any solution of piece of code?
For starters you cannot formally talk about binary or continuous images. Digital images have a discrete set of values, taken from a finite value set depending on their format and pixel bit-wise representation.
For example a "binary" image would have 2 levels of gray (white and black), represented by 0 or 1 or any other combination of values, e.g. an image of levels 0, 255 is still "binary". A grayscale image for an 8-bit representation (i.e. 8 bits per pixel) will have 2^8 discrete levels of intensity from min 0 (black) to max 255 (white).
Thus you can test for the number of unique levels of gray, i.e. unique values in your input image:
I = imread(image_filename);
if length(unique(I))==2,
flag_binary = true
end
Examples:
I = imread('cameraman.tif');
>> disp(flag_binary)
0
I = imread('circles.png');
>> disp(flag_binary)
1
From your question I'm guessing you are dealing only with images of the logical or double class. The first should be used for real binary images but unfortunately, that's not always the case when using code out in the wild.
It seems to me your problem is to distinguish between a real image of double class (all values between 0 and 1) or a binary image as class double (all values are 0 or 1). The best way to do it is the following which returns true if the image only has the values 1 and 0:
bool = all ((image(:) == 1) + (image(:) == 0));
This is a line from isbw() in Octave image package where you can use isbw (img, "non-logical")
Calculate the histogram using imhist. If there are more than two distinct grayvalues in the histogram your image is not binary.

Resources