Fill the lines of incomplete triangles and form complete triangle.
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a smoothed image which consists of 10 - 15 incomplete triangles (looks like triangles but few lines are not connected). I want to connect those lines and form a complete triangle and display the image. I have tried using houghlines but I am not able to complete those triangles. I am using MATLAB R2018a. I have attached an example of image I am trying up with this code:
% Apply edge detection to detect edges
edgeImage = edge(binaryImage, 'Canny');
% Apply line detection to detect line segments
lines = houghlines(edgeImage);
% Group line segments into triangles
triangleGroup = groupTriangleLines(lines);
% Display the image with joined lines
figure;
imshow(image);
hold on;
% Draw the joined lines on the image
for i = 1:numel(triangleGroup)
triangle = triangleGroup(i).lines;
for j = 1:size(triangle, 1)
xy = [triangle(j).point1; triangle(j).point2];
plot(xy(:, 1), xy(:, 2), 'LineWidth', 2, 'Color', 'r');
end
end
hold off;
2 commentaires
Réponses (1)
Image Analyst
le 31 Mai 2023
Modifié(e) : Image Analyst
le 31 Mai 2023
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear all;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'images (3).jpeg';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g2 = gcf;
g2.WindowState = "maximized";
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%=======================================================================================
% Segment the image.
[mask, maskedRGBImage] = createMask(rgbImage);
% Display image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis on;
caption = sprintf('Color Segmentation Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Clean mask.
% Fill black holes.
mask = imfill(mask, 'holes');
% Measure the areas so we can see how small is the smallest blob we want to consider.
props = regionprops(mask, 'Area');
allAreas = sort([props.Area])
% Take the blobs larger than 1000 pixels only
mask = bwareafilt(mask, [1000, inf]);
% Try to disconnect blobs that are tokuching by a little bit.
% Do an opening to break off any little tendrils
% diskRadius = 7;
% se = strel('disk', diskRadius, 0);
% mask = imopen(mask, se);
% Some tendrils may get disconnected. Take the large blobs only.
% mask = bwareafilt(mask, [1000, inf]);
% Do a closing to smooth out any little bays into the main objects.
% mask = imclose(mask, se);
% Fill black holes.
% mask = imfill(mask, 'holes');
% Display image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis on;
caption = sprintf('Final Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%--------------------------------------------------------------------------------------------------
% Get the circularity and area.
props = regionprops(mask, 'Circularity', 'Area');
circ = props.Circularity;
%--------------------------------------------------------------------------------------------------
% Plot the borders of all the blobs in the overlay above the original grayscale image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Display image.
subplot(2, 2, 4);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(mask);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is y.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
fontSize = 15;
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
%=====================================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 31-May-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.542;
channel1Max = 0.788;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.109;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.517;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
7 commentaires
Image Analyst
le 1 Juin 2023
I think what we have here is an XY problem.
So let's take a step back and figure out what you really need to measure. Because there are tons of things you could measure, or would like to measure, and we need to reconcile that with what you can measure or need to measure.
Sure you might look at the picture and think, "Oh I need to count the number of triangles" or "I need the perimeters of all triangles, even of the hidden portions" or "I need the area of all triangles, and of the complete triangle even if part of it is covered up" or similar. All those things can be hard to measure with non-perfect images. But what can you measure? You can measure the area, area fraction, and perimeter of what's actually there, ignoring the fact that some triangles may be overlapping or touching. And you may find that what you can measure correlates well with something you're trying to model. For example, let's say your independent variables are temperature and pH, and by varying those, you get different numbers, concentrations, and size distributions of triangles. Well since the actual count of triangles is hard to get you measure area fraction. And you know what, maybe that correlates just as well as the count. So you can optimize temperature and pH with your model of predicted area fraction.
As another example, a lot of times people look at a grainy scene, like the surface of a bowl of oatmeal and say "I need to measure the number of particles there because we believe that correlates with temperature". Well they might think they need the number of particles, but it may turn out that a texture metric correlates also, and that is a lot easier to measure than counting each and every single particle.
Voir également
Catégories
En savoir plus sur Convert Image Type dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!