Removing unwanted object using regionprops

6 次查看(过去 30 天)
I have used function below, how can I change the pixel inside the boundary which the value less than threshold(0.8) to be 0(black)? Means that, I want to remove all objects which value less than 0.80. In my picture there are 7 objects, only two objects that I want it to maintain which has value of 0.96 and 0.84. I have tried add several line of command but failed. here the image that I means: http://www.flickr.com/photos/64698236@N03/6006901788/in/photostream
here the command that i used for regionprops:
[B,L] = bwboundaries(image7,'noholes');
% Display the label matrix and draw each boundary imshow(label2rgb(L, @jet, [.5 .5 .5])) hold on for k = 1:length(B) boundary = B{k}; plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2) end
stats = regionprops(L,'Area','Centroid');
threshold = 0.80;
% loop over the boundaries for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k' boundary = B{k};
% compute a simple estimate of the object's perimeter delta_sq = diff(boundary).^2; perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k' area = stats(k).Area;
% compute the roundness metric metric = 4*pi*area/perimeter^2;
% display the results metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle if metric > threshold centroid = stats(k).Centroid; plot(centroid(1),centroid(2),'ko'); end
if metric < threshold L(i,j)=0; end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',... 'FontSize',14,'FontWeight','bold');
end
Thank you in advance.

回答(2 个)

Image Analyst
Image Analyst 2011-8-12
You don't even need bwboundaries or regionprops to get rid of pixels with a value of 0.8 or less. Just try something like (untested):
mask = image7 > 0.8
mask = cast(mask, class(image7));
outputImage = mask .* image7;
  2 个评论
Fathin
Fathin 2011-8-19
Thank you for helping me..
I am sorry.I have tried that function but it doesn't works.
I am thinking of writing command ;
if metric < threshold
L(i,j) = 0
end
I tried this also, but it doesn't work, maybe i do wrote wrong.
Fathin
Fathin 2011-8-19
I want to remove that after the bwboundaries or regionprops calculate the parameters first.
You can refer to my picture above to get what I mean.

请先登录,再进行评论。


Image Analyst
Image Analyst 2011-8-19
There is nothing in the image that I can see that relates to the arbitrary text labels stuck next to your colored blobs. Not area, not intensity, not location, nothing. If you labeled and pseudocolored your blobs, then you can't do that - you'll have to work with the original grayscale image if the 0.8 is some kind of intensity value. Otherwise if you're dealing with this colored image you'll have to do something real ad hoc, such as getting rid of blobs based on their exact color rather than using the value 0.8 at all. Same thing if you use the labeled image - you'll just have to use the known label numbers of those blobs you want to remove rather than any sort of automatic removal process based on the value 0.8.
  3 个评论
Fathin
Fathin 2011-8-19
this is my command, I rewrite it;
[B,L] = bwboundaries(image7,'noholes');
% Display the label matrix and draw each boundary
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
stats = regionprops(L,'Area','Centroid');
X = 0.80;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% mark objects above the X value with a black circle
if metric > X
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
%this is the part that I added, I have tried the simplest and also the complex one but nothing works. I intend to make all pixel in the object which has value less than X to turn black (originally white)
if metric < X
L(i,j)=0;
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y','FontSize',14,'FontWeight','bold');
end
Harini Ramasamy
Harini Ramasamy 2018-2-16
how to calculate the number of similar metric value within some range for example if i had a many range of roundness value then how to calculte the number of similar range of roundness like from .2 to .8

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by