I tried doing this:
foreground_mask = uint8(new_label == 1);
segmented_foreground = imdata_orig .* repmat(foreground_mask, [1,1,3]);
and got a messed up original image, instead of the properly applied mask.
imdata_orig
is uint8 (which is why I make the logical -> uint8 casting).
What is wrong here? What are other ways to do this?

1 commentaire

Walter Roberson
Walter Roberson le 1 Juin 2013
In what respect is it messed up? Are some values left non-zero when they should not be, or some made zero when they should not be, or does the result come out completely different than the mask ? Perhaps you could post some samples?

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 1 Juin 2013

1 vote

Try Sean de Wolski's method:
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
where mask is a map of foreground and background like a logical image or a double image, that perhaps you got from color classification or something.

5 commentaires

Qazi  Arbab Ahmed
Qazi Arbab Ahmed le 19 Déc 2016
It does work for me last day. But now it displays "Undefined function or variable 'mask'.
Error in test_framereading (line 27) maskedRgbImage = bsxfun(@times, im, cast(mask,class(im)));"
Jan
Jan le 19 Déc 2016
@Qazi: "mask" is the logical array and you have to define this matrix by your own.
Jan
Jan le 19 Déc 2016
@Image Analyst: I thought several times to create a C-Mex function for the fast application of a logical mask to a multi-dim array. But I always stopped this, because I've been convinced, that the Image Processing Toolbox must contain such a function already, because the task is so common.
Qazi  Arbab Ahmed
Qazi Arbab Ahmed le 19 Déc 2016
Thank You Simon. It worked when I defined it as bw. my binary Image. Thanks Alot, Now I want to extract temperature values from my ROI in a thermal video, Would you have any Idea?
Image Analyst
Image Analyst le 28 Mar 2023
@Qazi Arbab Ahmed you'd have to extract one frame at a time and then convert the RGB image (if it's been pseudocolored) into a temperature image. See attached demos. Adapt as needed.

Connectez-vous pour commenter.

Plus de réponses (2)

Sam
Sam le 16 Juin 2014

2 votes

I had a similar problem. Try segmenting the channels first and applying the mask to each channel.
something from a similar project:
r = im1(:,:,1);
g = im1(:,:,2);
b = im1(:,:,3);
r(testgray<threshold) = 0;
g(testgray<threshold) = 0;
b(testgray<threshold) = 0;
im1(:,:,1) = r;
im1(:,:,2) = g;
im1(:,:,3) = b;
testgray was a n*n matrix with the same width and height of im1, containing distance information from a 3D reconstruction, any values in the background (below the threshold) are set to 0, similar to masking, allowing only foreground image data.

2 commentaires

Here is how I used the code above to solve the problem, where "I" is the rgb image.
imageSize = size(I);
ci = [200, 200, 100]; % center and radius of circle ([c_row, c_col, r])
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
r = I(:,:,1);
g = I(:,:,2);
b = I(:,:,3);
r = r.*mask;
g = g.*mask;
b = b.*mask;
I(:,:,1) = r;
I(:,:,2) = g;
I(:,:,3) = b;
imshow(I)
Image Analyst
Image Analyst le 5 Mai 2016
Note: This is for the special case of a circular mask only.

Connectez-vous pour commenter.

Eman
Eman le 28 Mar 2023
Modifié(e) : Image Analyst le 28 Mar 2023

0 votes

Create a mask that extract the head of the cameraman that's located at the following locations : (100,40) (136,40) (100,80) (136,80) using AND logical operations.
Note: you can use
[x,y]= ginput(4)
to get the coordinate of the cameraman head using the mouse cursor.

1 commentaire

Image Analyst
Image Analyst le 28 Mar 2023
@Eman this does not look like an answer to @Antonio's original post. This looks like your homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own. But you can look at my attached demo. If you have any questions, then let's not send @Antonio more emails about your question, let's have you start your own separate thread.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by