delete isles in binary matrix
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Matt J
il 4 Lug 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.
Risposte (1)
prabhat kumar sharma
il 18 Gen 2024
Modificato: prabhat kumar sharma
il 18 Gen 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!
0 Commenti
Vedere anche
Categorie
Scopri di più su Computer Vision with Simulink in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!