how to change pixel label based on it's neighbors?
5 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone,
I have a label mask contains of (1,2,... Number of labels).
for example:
11112222233333
11122221222233
11122222222233
in some regions I have a wrong labeled pixel for some reason, for instance in region labeled by 2 I have one pixel inside this region labeled by 1. How can I clean these few wrong labeled pixels and give it the value of it's 3x3 neighbours?
Median filter of erosion actually just reorder the kernel but not removing the wrong labelled pixel
I will appreciate any help,
best regards,
4 件のコメント
採用された回答
Image Analyst
2020 年 12 月 17 日
Try this demo. It lets you specify the min allowable hole size, so that you cen keep huge holes, like a donut, but remove small holes like salt and pepper noise:
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 14;
fprintf('Beginning to run %s.m ...\n', mfilename);
labeledImage = [
1,1,1,1,2,2,2,2,2,3,3,3,3,3,
1,1,1,2,2,2,2,1,2,2,2,2,3,3,
1,1,1,2,2,2,2,2,2,2,2,2,3,3,
1,1,1,2,2,2,2,1,2,2,2,2,3,3,
1,1,1,2,2,2,2,1,2,2,2,2,3,3,
1,1,1,2,2,2,2,2,2,2,2,2,3,3]
subplot(3, 2, 5);
imshow(labeledImage, [], 'Colormap', lines(4), 'InitialMagnification', 4800);
title('Original labeled Image', 'FontSize', fontSize);
colorbar
axis('on', 'image');
drawnow;
% Define the min acceptable hole size
% Meaning get rid of holes 1 or smaller,
% and keep holes of size 2 or bigger.
minHoleArea = 2;
maxLabel = max(labeledImage(:));
for k = 1 : maxLabel
% Get a binary image of only this label.
thisLabel = ismember(labeledImage, k);
subplot(3, 2, 1);
imshow(thisLabel, [], 'InitialMagnification', 4800);
caption = sprintf('Label #%d', k);
title(caption, 'FontSize', fontSize);
% Maximize figure window
if k == 1
g = gcf;
g.WindowState = 'maximized';
end
% Invert it so holes become blobs, and and it with a hole-filled thisLabel
% so that we get holes only within this particular label.
filledLabel = imfill(thisLabel, 'holes');
holeMask = ~thisLabel & filledLabel;
subplot(3, 2, 2);
imshow(holeMask, [], 'InitialMagnification', 4800);
caption = sprintf('All Initial Holes in Label #%d', k);
title(caption, 'FontSize', fontSize);
% Now size filter these holes to remove holes smaller
% than the min allowable area.
holeMask = bwareafilt(holeMask, [1, minHoleArea-1]);
subplot(3, 2, 3);
imshow(holeMask, [], 'InitialMagnification', 4800);
caption = sprintf('Holes smaller than %d\nthat will be deleted from Label #%d', minHoleArea, k);
title(caption, 'FontSize', fontSize);
% use holeMask to fill in the label
labeledImage(holeMask) = k;
subplot(3, 2, 6);
imshow(labeledImage, [], 'Colormap', lines(4), 'InitialMagnification', 4800);
caption = sprintf('Labeled Image with\nholes smaller than %d\nnow deleted from Label #%d', minHoleArea, k);
title(caption, 'FontSize', fontSize);
drawnow;
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
break;
end
end
fprintf('Done running %s.m ...\n', mfilename);
2 件のコメント
Image Analyst
2020 年 12 月 18 日
You can try modefilt(). It probably will give a smoother image, if you think that a smoother image is more accurate. It will not give you precise control over the size of the holes you want to keep or exclude like my code will.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!