You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
making an image with binary mask
3 views (last 30 days)
Show older comments
I have obtain the binary mask through segmentation as attach now I have to apply this mask to the image that is gray image, problem is mask is created using regionprops() command in matlab, so the mask obtain is of different size, point by point multiplication doesn't work, any solution?
The result I'm getting with imresize() can be seen. How do I map it perfectly?
2 Comments
sivaranjani g
on 8 Oct 2017
which coding used to create this mask????
Image Analyst
on 8 Oct 2017
It probably was a color image originally and color segmentation was used to find skin tones. Or perhaps texture segmentation might have been used.
Answers (1)
Image Analyst
on 27 Apr 2015
Why is the mask a different size than the gray scale image? regionprops() does not create masks. Perhaps you created one by taking the bounding box of one of the blobs??? If so, don't do that. You can get a mask of just one particular blob, if that's what you want to do, by using ismember().
22 Comments
likui fikui
on 27 Apr 2015
Edited: likui fikui
on 27 Apr 2015
first apply the thresholding, in order to remove small objects that are not ROI, i have used maximum area to get the largest component and than filled image to get mask
likui fikui
on 27 Apr 2015
Do you wish to see the code?
Image Analyst
on 28 Apr 2015
Sure. By the way, you'd be best off using a uniform dark background, or a really bright uniform one, or one with a lot more or a lot less texture than the hand. Right now you have a background that is complicated to find the hand on. But I can't see why regionprops is giving you a mask at all, much less one of a different size, unless you give the code. Attach with the paper clip icon.
likui fikui
on 29 Apr 2015
Edited: likui fikui
on 29 Apr 2015
here is the code
Image Analyst
on 29 Apr 2015
Which one of the images is 'DSC_0081.jpg'?
likui fikui
on 30 Apr 2015
Image Analyst
on 30 Apr 2015
It looks like you have no consistency or control over the background - different colors, different textures, different amounts of clutter. Is that true, or can you control your background better to make your task easier?
likui fikui
on 1 May 2015
no control over background, it can be any thing or any colour
likui fikui
on 1 May 2015
but the problem is not segmentation, but applying the mask to the image,have obtain really good segmentation results,
Image Analyst
on 1 May 2015
I'm not sure what "apply the mask" means to you. Do you just want to multiply it by the image, like
maskedImage = grayImage .* uint8(mask);
Or zero out the non-mask areas by assignment:
maskedImage = grayImage;
maskedImage(~mask) = 0;
or something else? You have to say, because "apply the mask" is not specific at all. Apply HOW ? It looks like you already applied a mask that is larger than the hand in the first image you posted but I'm not sure where that mask came from. You're going to have to explain more before anyone can help you because I don't want to just start guessing all kinds of wrong scenarios -- that would waste both of our time.
Apra Gupta
on 30 Aug 2021
Hi, Image Analyst, Using color thresholding App I have created a mask as per my requirement, i exported the function and gave my another image as input, but I am not able to see result. If I write imshow(MaskedRGBImage), it shows original image being masked, not the output for given input.
Image Analyst
on 30 Aug 2021
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
where mask is what you got from the Color Thresholder, and rgbImage is your new, different image.
Apra Gupta
on 30 Aug 2021
Edited: Image Analyst
on 30 Aug 2021
This is my code. It is still not working. Can you please help?
RGB = imread('trial visible.png');
imshow (RGB)
hold on
figure()
imshow (maskedRgbImage)
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 30-Aug-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 255.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 101.000;
channel2Max = 210.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 150.000;
channel3Max = 253.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% Create mask based on selected regions of interest on point cloud projection
I = double(I);
[m,n,~] = size(I);
polyBW = false([m,n]);
I = reshape(I,[m*n 3]);
% Project 3D data into 2D projected view from current camera view point within app
J = rotateColorSpace(I);
% Apply polygons drawn on point cloud in app
polyBW = applyPolygons(J,polyBW);
% Combine both masks
BW = sliderBW & polyBW;
% Initialize output masked image based on input image.
maskedRgbImage = bsxfun(@times, RGB, cast(mask, 'like', RGB));
% imshow (maskedRGBImage)
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
function J = rotateColorSpace(I)
% Translate the data to the mean of the current image within app
shiftVec = [137.589180 138.858503 115.202572];
I = I - shiftVec;
I = [I ones(size(I,1),1)]';
% Apply transformation matrix
tMat = [-0.003597 0.001504 0.000000 0.260702;
0.001554 0.003481 0.000000 -0.657293;
0.000000 0.000000 0.003577 8.160254;
0.000000 0.000000 0.000000 1.000000];
J = (tMat*I)';
end
function polyBW = applyPolygons(J,polyBW)
% Define each manually generated ROI
hPoints(1).data = [-0.137389 -0.174272;
-0.048924 -0.193483;
-0.027693 -0.251116;
0.023617 -0.352660;
0.064310 -0.467927;
0.059003 -0.550260;
0.009462 -0.605149;
-0.162159 -0.621615;
-0.270086 -0.522816];
% Iteratively apply each ROI
for ii = 1:length(hPoints)
if size(hPoints(ii).data,1) > 2
in = inpolygon(J(:,1),J(:,2),hPoints(ii).data(:,1),hPoints(ii).data(:,2));
in = reshape(in,size(polyBW));
polyBW = polyBW | in;
end
end
end
Apra Gupta
on 30 Aug 2021
This is my new image
Image Analyst
on 30 Aug 2021
Try the attached.
Apra Gupta
on 31 Aug 2021
Thank you Sir, it worked beautifully.
Apra Gupta
on 7 Sep 2021
Hi, Image Analyst, In the attached figure I want to plot the each pixel intensity seperatly for two bowls. Do I first need to to sepearte the images and find the pixel intensity? And as next step I want to see the variance in the pixel intensity of two bowls. Please help.
Image Analyst
on 7 Sep 2021
I don't know what that means, but yes, you'd have to do each region at a time if that's what you want to do. What does plot pixel intensity mean? You mean like histogram() or surf()? And there are multiple colors in there because the bowls are filled with a material of different color than the bowl itself. Plus, each region will have one set of red values, one set of green values, and one set of blue values which are vastly different so you wouldn't want to "plot" them on the same "plot" (whatever that means). Plus the one bowl has a shadow over it which also changes the color.
What is the use case? Why do you have images like this?
Apra Gupta
on 7 Sep 2021
Yes, I want surf. One of the bowl contains dry soil another contains wet. I want study the variation in color intensity between both the cases. I want to find the variance and standarised it to distingush between dry and wet soil.
Image Analyst
on 7 Sep 2021
Rather than continue to hijack @likui fikui's discussion, can you please start your own question? I'll tell you there how to do it. In the mean time, see the attachment.
Apra Gupta
on 7 Sep 2021
As per your suggestion, I created new Questionhttps://in.mathworks.com/matlabcentral/answers/1448409-want-to-plot-the-surf-where-x-y-axis-represent-pixel-coordinate-and-z-axis-its-intensity-separate-p?s_tid=srchtitle
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)