Remove scratches from binary image

5 views (last 30 days)
Adrian Reuther
Adrian Reuther on 16 Feb 2020
Hi everybody,
right now I am struggling with a problem. I got a binary image which contains unwanted scratches. In the uploaded image you can see which objects need to be removed (red rectangular). The following code already works, but it also removes everything else inside the rectangular. Since I want to keep the other objects, the code needs to be modified. My question is: How can the code be modified, to only erase the scratches and not the other objects?
Subtract=Vergleich-Referenz;
binary=imbinarize(Subtract,0.14);
se=strel('disk',4);
close=imclose(binary,se);
close=imfill(close,'holes');
stats=regionprops('table',close,'BoundingBox','MajorAxisLength','MinorAxisLength','SubarrayIdx','Area','Perimeter','Centroid');
stats.LenWdRatio = stats.MajorAxisLength ./ stats.MinorAxisLength;
thresh = 3;
stats.isGreater = stats.LenWdRatio < thresh;
objRemoveIdx=find(~stats.isGreater);
for i = find(~stats.isGreater).'
close(stats.SubarrayIdx{i,1},stats.SubarrayIdx{i,2}) = false;
end
se=strel('disk',3);
open=imopen(close,se);
filter=bwareafilt(open,[10 1000]);
stats.circularity=4*pi*stats.Area./stats.Perimeter.^2;
thresh=0.5;
stats.isBigger = stats.circularity >thresh;
for i = find(~stats.isBigger).'
filter(stats.SubarrayIdx{i,1},stats.SubarrayIdx{i,2}) = false;
end
invert=imcomplement(filter);
I would be very thankful for an answer.
Greetings!

Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!