delete isles in binary matrix
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 2 de Jul. de 2023
There are certainly discontinuities but I don't know what you mean by "Isles". Please explain what those are because I haven't the slightest idea. Also, you forgot to attach bscan in a .mat file. Please do so after you read this:
the cyclist
el 2 de Jul. de 2023
A clear, definitive method for a human to identify an "isle" is almost certainly going to be the first step in writing a computer algorithm to do the same, and then be able to exclude it.
Kjell
el 2 de Jul. de 2023
Kjell
el 2 de Jul. de 2023
Matt J
el 3 de Jul. de 2023
What are the criteria for classifying a region as an "isle" to be removed? Is there a certain separation distance and/or length that it should have?
Image Analyst
el 3 de Jul. de 2023
Editada: Image Analyst
el 3 de Jul. de 2023
I still don't know what you ultimately want. Do you just want a list of (x,y) coordinates for the dots in the image that form a line plot, but where there is a discontinuity you want the discontinuous regions replaced with a straight line between the start and end of the discontinuity? It's not clear from all the examples exactly where the discontinuities start and end. I know you circled them, but you'll need a mathematical definition. And explain why those regions need to be interpolated. Why can't you use the actual data as-is?
Kjell
el 4 de Jul. de 2023
But you still haven't told us how you distinguish the abberations from the things you want to keep. In the figure below, why should I not consider the region circled in yellow as the thing I should keep and the regions circled in blue as the ones to be thrown away?

Kjell
el 4 de Jul. de 2023
Matt J
el 4 de Jul. de 2023
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.
Respuestas (1)
prabhat kumar sharma
el 18 de En. de 2024
Editada: prabhat kumar sharma
el 18 de En. de 2024
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!
Categorías
Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









