getting coordinates from a picture

201 views (last 30 days)
Hi! I have an image that I would like to get the coordinates from. Its a 2D image. I need to perform some operation with the image. I need to get the coordinates of some points I choose and get them saved. I am aware of the data cursor, but since I have a huge number of points, manual operation will be of no way.
lets say, I have this basic structure of a thermal fin. I need say 1000 points from its boundary.Is there any way in matlab, I can import the image, and save the coordinates of the boundary, distributed evenly over.
I hope my question is clear.
thanks in advance!!

Accepted Answer

Image Analyst
Image Analyst on 2 Oct 2014
Edited: Image Analyst on 16 Jul 2019
Well, you could use image analysis:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 36;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\aaditya\Documents\Temporary';
baseFileName = 'part1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Extract the individual red, green, and blue color channels.
% redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
% blueChannel = rgbImage(:, :, 3);
% Get the binaryImage
binaryImage = greenChannel < 200;
% Display the original color image.
subplot(2, 2, 2);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
% Find the baseline
verticalProfile = sum(binaryImage, 2);
lastLine = find(verticalProfile, 1, 'last')
% Scan across columns finding where the top of the hump is
for col = 1 : columns
yy = lastLine - find(binaryImage(:, col), 1, 'first');
if isempty(yy)
y(col) = 0;
else
y(col) = yy;
end
end
subplot(2, 2, 3);
plot(1 : columns, y, 'b-', 'LineWidth', 3);
grid on;
title('Y vs. X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
Of course you'll get better results if you use a PNG image rather than that crummy JPG which has all kinds of bad compression artifacts.
  15 Comments
Image Analyst
Image Analyst on 20 Dec 2020
Xavier, I'm not sure what happened to the code. Either I forgot to attach it or one of the other moderators accidentally deleted the attachment. So I had to recreate it, but it was very simple. Here it is:
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 = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'curve.png';
% Get the full filename, with path prepended.
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
grayImage = 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(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get a mask that is the entire image.
% mask = true(size(grayImage));
% Alternative : get a mask that is the thresholded part of the image.
threshold = 195;
binaryImage = grayImage ~= threshold;
subplot(2, 2, 2);
imshow(binaryImage, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Find the top boundary
yTop = zeros(1, columns);
for col = 1 : columns
top = find(binaryImage(:, col), 1, 'first');
if ~isempty(top)
yTop(col) = top;
end
end
subplot(2, 2, 3);
x = 1 : columns;
plot(x, yTop, 'b-', 'LineWidth', 3);
grid on;
title('Y vs. X', 'FontSize', fontSize);
xlabel('Xtop', 'FontSize', fontSize);
ylabel('Ytop', 'FontSize', fontSize);
% 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.
subplot(2, 2, 4);
imshow(grayImage); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(binaryImage);
% 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 x.
plot(x, y, 'r-', 'LineWidth', 3); % Plot boundary in red.
end
hold off;
title('Binary Image with Full Boundary', 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
Image Analyst
Image Analyst on 14 Jun 2021
@Erkan Karaoglu, let's not start a new discussion in @aaditya's 7 year old thread. Please start your own thread and attach the original grayscale thermal images rather than the pseudocolored ones. Also take the picture with the scene perpendicular to the optic axis, not an an oblique angle (bad for a number of reasons).

Sign in to comment.

More Answers (1)

Venkatkumar Muneeswaran
Venkatkumar Muneeswaran on 15 Jun 2023
Edited: Venkatkumar Muneeswaran on 15 Jun 2023
Could you pleae let me know how do i get the data from this plot?
  3 Comments
Venkatkumar Muneeswaran
Venkatkumar Muneeswaran on 15 Jun 2023
As of now I am using Similar to that, but Is ther any otherway without picking the points in the image and directly recognize the line/dashed line plot in the image?
Image Analyst
Image Analyst on 15 Jun 2023
Edited: Image Analyst on 15 Jun 2023
Yes, but it will be complicated. Probably too complicated for you to program up. You'd have to segment the image to find the curved line, and the axes (box) then find the limits of the axes to spatially calibrate it. Not super trivial. Why don't you just use imfreehand to trace the curve? Demo attached.

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!