MATLAB Answers

aaditya
0

getting coordinates from a picture

Asked by aaditya
on 2 Oct 2014
Latest activity Commented on by polo Chris on 12 Nov 2019 at 6:06
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!!

  0 Comments

Sign in to comment.

1 Answer

Answer by Image Analyst
on 2 Oct 2014
Edited by Image Analyst
on 16 Jul 2019
 Accepted Answer

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.

  12 Comments

wick
on 20 Jul 2019
Sir I want this kind of output for my project.I want to extract X and Y values along with the curve boundry and save it as a separate excel sheet . I want to get those values automatically without clicking on the curve boundy. Sir I tried what you have said and I placed the "boundaries = bwboundaries(binaryImage);" code at the very end and it didnt work.I want to do this for any kind of curve graphs.I attached the image related to the above picture. Sir please give me a solution..
Sir at the end of the code (the code you have given) I placed those coded lines and I tried to save the output as a separate file, But it won't work.
% 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);
boundaries = bwboundaries(binaryImage);
xlswrite([fullFileName '_raw.xls'],boundaries,'Sheet1','A1');
I'm not really sure what you want or why my code can't be adapted to what you want. So I've done it two ways for you.
  1. One finds only the y value (line number) of the top of the hump, along with the x value (column) where it occurs. These are stored in xTop and yTop and are shown in blue in the lower left.
  2. The other method finds the coordinates of the complete outline of the hemispherical blob, and those are stored in xOutline and yOutline and drawn in red over the binary image in the lower right.
Hopefully one of them is what you want. Note: nowhere in my code am I asking the user or the programmer to specify any point on any image. If this doesn't answer your question, improve your question and plot it as a new question.
0000 Screenshot.png
polo Chris on 12 Nov 2019 at 6:06
Hello,Thanks for your code,but i have a question about this,when i run this the .xls file doesn't any data, i don't how to solve it.

Sign in to comment.