Find the position matrix of boundary points in an image

2 views (last 30 days)
How do I implement the function in the image: find the coordinates of the four points of ABCD and express them in a matrix

Answers (2)

KSSV
KSSV on 27 Apr 2022
You can do it manually using getpts, ginput. Read about these functions. If you want to do it automatically, you can get the white pixel indices using logical comaprisons and get their indices. From these you can find the corners.
  7 Comments
KSSV
KSSV on 27 Apr 2022
Also you can pick first row and last column from Ithres and pick first and last non-zero to get A, B, C and D.
文辉 沈
文辉 沈 on 28 Apr 2022
I can extract the coordinates in 1, but there is no intersection between the crack and the boundary in 2, how to deal with this (perhaps, I have a problem in the preprocessing stage, the crack after processing is incomplete)

Sign in to comment.


Image Analyst
Image Analyst on 28 Apr 2022
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = '00001.jpg';
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
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgbImage(:, :, 3);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
% Display histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Draw a red line at the threshold.
threshold = 122;
xline(threshold, 'Color', 'r', 'LineWidth', 2)
%--------------------------------------------------------------------------------------------------------
% Binarize the image to get a mask.
mask = grayImage < threshold;
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1); % Take largest blob only
% Find first and last columns of the first/top row
A = find(mask(1,:), 1, 'first')
B = find(mask(1,:), 1, 'last')
% Find first and last columns of the last/bottom row
C = find(mask(end,:), 1, 'first')
D = find(mask(end,:), 1, 'last')
% Display mask image.
subplot(2, 2, 3);
imshow(mask);
hold on;
impixelinfo;
axis('on', 'image');
drawnow;
caption = sprintf('Mask. A = %d, B = %d, C = %d, D = %d', A, B, C, D);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% 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.
% 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
subplot(2, 2, 4); % Switch back to the first image.
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% 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);
title(caption, 'FontSize', fontSize);
% axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
  5 Comments
文辉 沈
文辉 沈 on 28 Apr 2022
The white border in the image appears when I save it in the matlab image frame. The result of the operation is that there is no white border. This does not need to be considered.
Image Analyst
Image Analyst on 28 Apr 2022
If you need to save an image don't use save() or saveas() which will give you the surrounding background. Use imwrite().

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!