Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

matlab - Given a mask, how do I get obtain the original colors of an image?

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...