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 d
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: