delete isles in binary matrix

2 次查看(过去 30 天)
Kjell
Kjell 2023-7-2
Hi,
I have a binary matrix, which shows an edge of a bscan. However some of the binary matrices have Isles which messes up my edge Interpolation to close the gaps between the edge for example
Is there a way to delete the isles without loosing the information of the intented edge. Here is my Code:
load ("images.mat");
figure()
imagesc(interpolateEdge(img1));
figure()
imagesc(interpolateEdge(img2));
figure()
imagesc(interpolateEdge(img3));
figure()
imagesc(interpolateEdge(img4));
function [bscanout] = interpolateEdge(bscan)
[rows, columns] = size(bscan);
% delete Isle
bscan = bwareaopen(bscan, 150);
temp = zeros(rows,columns);
for i = 1:columns
for j = 1:rows
if bscan(j,i) == 1
temp(j,i) = 1;
break;
end
end
end
% Delete Isle under the edge
temp(350:end,:) = 0;
% Delete Isle above the the edge
[yThresh xThresh] = find(temp);
if median(yThresh) > 200
temp(1:150,:) = 0;
end
% Interpolate edge Without high peaks
bscanout = temp;
[yValues, xValues] = find(temp);
clear temp;
%if gap is at the start
if xValues(1) ~= 1 && xValues(1) ~= 2
xValues = [1; 2; xValues];
yValues = [yValues(end-4); yValues(end-4); yValues];
elseif xValues(1) ~= 1 && xValues(1) == 2
xValues = [1; xValues];
yValues = [yValues(end-4); yValues];
end
%if gap is at the end
if xValues(end) ~= columns && xValues(end) ~= columns -1
xValues = [xValues; columns - 1; columns];
yValues = [yValues; yValues(3); yValues(3)];
elseif xValues(end) ~= columns && xValues(end) == columns -1
xValues = [xValues; columns];
yValues = [yValues; yValues(3)];
end
%interpolate edge for the missing gap
for i = 1:length(xValues) - 1
if xValues(i) + 1 ~= xValues(i + 1)
edgeData = interp1(xValues(i-1:i + 1),yValues(i-1:i + 1),1:columns,"pchip");
edgeData = round(edgeData);
for j = xValues(i):xValues(i+1)
bscanout(edgeData(j),j) = 1;
end
end
end
end
  10 个评论
Kjell
Kjell 2023-7-4
I am not sure how to do this yet, but I have two ideas.
One is by using the median of the height which can be determined in yThresh and replace everything over it with a certain distance to the median with zeros.
My second idea is by using the slope. If the edge is changing its region, there should be an massive increase where the yellow increase starts and a similar decrease where it ends. Then the bigger region should be kept
Matt J
Matt J 2023-7-4
there should be an massive increase where the yellow increase starts and a similar decrease where it ends
So the defect regions will always be biased upward relative to the true curve? Never downward?
And the defect can never occur at the start of the curve? If it does, there will be no signal jump to indicate where it starts.

请先登录,再进行评论。

回答(1 个)

prabhat kumar sharma
编辑:prabhat kumar sharma 2024-1-18
Hi Kjell,
It understand that your primary concern is distinguishing the main edge from an additional, unwanted edge caused by artifacts within your binary image. To address this issue, you can employ several strategies:
Try Different Edge Detection Filters: There are various edge detection filters available. Experiment with different ones to determine which best suits your needs and effectively highlights the main edge while minimizing the impact of artifacts.
Use Morphological Operations to Remove Small Objects: Morphological operations in MATLAB, such as “bwareaopen", can be used to eliminate small, isolated groups of pixels without affecting the main edge information. This operation removes all connected components with fewer pixels than a specified threshold, effectively filtering out noise.
binaryImage = imread('path_to_your_image.png'); % Load your binary image
threshold = 50; % Define a threshold for the minimum size of objects to retain
cleanedImage = bwareaopen(binaryImage, threshold);
You can refer this documentation for more details on “bwareaopen” : https://mathworks.com/help/releases/R2023a/images/ref/bwareaopen.html
Filter Based on Component Area or Intensity: Considering the images you have provided; I suggest identifying connected components based on their area or intensity values. You can then retain the component with the largest area, which likely represents the main edge, and remove smaller components that are likely artifacts resulting from bright spots in the image.
Here I am providing an approach for finding the largest connected component based on area :
% Load your binary image
binaryImage = imread('path_to_your_image.png');
% Find all connected components in the binary image
cc = bwconncomp(binaryImage);
% Get area properties of each component
stats = regionprops(cc, 'Area');
% Sort the components based on area in descending order and get the indices
[sortedAreas, sortIdx] = sort([stats.Area], 'descend');
% Check if there are at least two components
if numel(sortIdx) >= 2
% Get the indices of the first and second largest components
largestComponentIdx = sortIdx(1);
secondLargestComponentIdx = sortIdx(2);
% Create a new binary image and fill it with the two largest components
mergedComponents = false(size(binaryImage));
mergedComponents(cc.PixelIdxList{largestComponentIdx}) = true;
mergedComponents(cc.PixelIdxList{secondLargestComponentIdx}) = true;
% Optionally, you can perform morphological closing to ensure the components are connected
se = strel('disk', 3); % Adjust the size of the structuring element as needed
mergedComponents = imclose(mergedComponents, se);
else
error('Not enough components found in the image.');
end
% Visualize the result
figure;
subplot(1,2,1);
imshow(binaryImage);
title('Original Binary Image');
subplot(1,2,2);
imshow(mergedComponents);
title('Merged Largest Components');
This results the largest connected component that looks like below image.
This approach should be well-suited for your situation, allowing you to maintain the integrity of the main edge while removing extraneous artifacts.
I hope this helps you achieve the desired outcome!

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by