how to mark pixel as zero

Hi all
I have a segmented image i want to process only the larger segment. so i want to make the remaining pixels as zero. Please help me how i can do it.

2 comentarios

Image Analyst
Image Analyst el 24 de Jul. de 2012
You don't need to make the remaining pixels zero in order to measure the largest "segment".
M@lik Ali
M@lik Ali el 24 de Jul. de 2012
Thanks again. yes but actually i want to do some more processing on the largest segment like to extract features to display that image. that why i think i need it, what you suggest ..

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 23 de Jul. de 2012

0 votos

Use regionprops to get the areas then get them all in an array so you can determine the largest.
measurements = regionprops(labeledImage, 'Area');
allAreas = [measurements.Area];
[sortedAreas sortIndexes] = sort(allAreas);
% Then mask your image or create a new one from the largest blob.

3 comentarios

M@lik Ali
M@lik Ali el 24 de Jul. de 2012
Thanks Image Analyst, in this way i cant get the largest region, now i want to mark all pixels as zero except largest region. please help me how i can mark the pixel as zero.
Image Analyst
Image Analyst el 24 de Jul. de 2012
Editada: Image Analyst el 24 de Jul. de 2012
You can get the largest region. It's sortIndexes(1). Just extract the label for the largest image with ismember(). Then turn it into a binary image and use that to "mark all pixels as zero except largest region" in your original image.
biggestLabel = ismember(labeledImage, sortIndexes(1));
binaryImage = biggestLabel > 0;
% If you want the original image masked:
maskedImage = grayImage;
maskedImage(~binaryImage) = 0;
If this code doesn't work for you, let me know tomorrow and maybe I can adapt my blobs demo (in my File Exchange) to find the largest blob. But like I said in the comment, you don't need to zero anything out to get the measurements of the largest blob, so I don't know why you think you do, unless you just want to display it for curiosity's sake.
Image Analyst
Image Analyst el 24 de Jul. de 2012
Muhammad, you can color your labels like this, instead of all that code you gave in your comment below:
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
imshow(coloredLabels);

Iniciar sesión para comentar.

Más respuestas (1)

jagadeeshwar
jagadeeshwar el 23 de Jul. de 2012

0 votos

hai imran just crop the image which segment u want it can be enlarge after crop operation

4 comentarios

M@lik Ali
M@lik Ali el 23 de Jul. de 2012
actually in my case, i divide the image into blocks then perform segmentation by kmeans. now i what i want is to get the largest region of the image. so i want to mark the unwanted pixels as zero. therefore kindly help me how i can make the many pixels as zero. so that when i can perform the processing on my image only my wanted region where pixel value will not be zero.
Image Analyst
Image Analyst el 24 de Jul. de 2012
Not necessarily true. With a crop, you could have a portion of another blob included in the cropped image, so you could have more than one blob in the cropped image. Besides, he'd first need to know what the largest blob is before he could even crop it, but that's his main problem - finding the largest blob. He doesn't know which blob it is unless he sorts it like I showed him how. Plus there's no need to do any enlarging.
M@lik Ali
M@lik Ali el 24 de Jul. de 2012
Ok thanks, my goal is to extract the features of the largest regions, that why i was thinking that once i have a complete image having some pixels zero values. then its easy to further process.
M@lik Ali
M@lik Ali el 24 de Jul. de 2012
One more thing can i do like this
as i have a piece of code like this for c = 1 : size(ca, 2) for r = 1 : size(ca, 1) jj=ca{r,c}; indx=indx+1;
if labels(indx)==1
colorMap(r:c, 1) = 1;
colorMap(r:c, 2) = 0;
colorMap(r:c, 3) = 0;
elseif labels(indx)==2
colorMap(r:c, 1) = 1;
colorMap(r:c, 2) = 1;
colorMap(r:c, 3) =0;
elseif labels(indx)==3
colorMap(r:c, 1) = .5;
colorMap(r:c, 2) = .5;
colorMap(r:c, 3) = .5;
elseif labels(indx)==4
colorMap(r:c, 1) = 0;
colorMap(r:c, 2) = 1;
colorMap(r:c, 3) = 0;
elseif labels(indx)==5
colorMap(r:c, 1) = 1;
colorMap(r:c, 2) = 0;
colorMap(r:c, 3) = 1;
elseif labels(indx)==6
ddd=ddd+1;
colorMap(r:c, 1) = 127/255;
colorMap(r:c, 2) =1;
colorMap(r:c, 3) = 212/255;
elseif labels(indx)==7
% elseif data_idxs(indx)==7
colorMap(r:c, 1) = 1;
colorMap(r:c, 2) = 0;
colorMap(r:c, 3) = 0;
else
colorMap(r:c, 1) = 1;
colorMap(r:c, 2) =0;
colorMap(r:c, 3) = 1;
end
end
end
using this code i color the different regions. now can i make the pixel values zero as in this case i put the different color for the pixel, so same way i want to put some pixel as zero means when
elseif labels(indx)==6 value (r:c)=0 hoe i can do like this.
i am able to do this my job will be easy.

Iniciar sesión para comentar.

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by