Calculate and crop image based on coordinates

6 views (last 30 days)
dan kin
dan kin on 12 Sep 2014
Edited: Image Analyst on 1 Oct 2014
Hi,
How can I calculate using Matlab the coordinates marked with an X only if I have the red point coordinates as I can crop the image according to them?
Thanks
Rotate version of the image:
  4 Comments
Joseph Cheng
Joseph Cheng on 12 Sep 2014
depends on what you have. are you working before or after the rotation? in the rotated image then you can see you can build the corners based on the red points. So they you have the corners being x1=x4=xa y1 = yb=y2.. and so on.
dan kin
dan kin on 12 Sep 2014
I can rotate them but that require the pre-processing. Is the correct code for the rotate version is: I2=imcorp(I,[X1 Y1 X3 X3])?

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 12 Sep 2014
Edited: Image Analyst on 12 Sep 2014
There is no unique box that goes through all the points. For example, here is another box I drew that is just as valid as yours:
My red box goes through all 4 red spots just like your dashed blue box does.
What you might want to do is to binarize the image into black/white, true/false, 1/0. Then call bwlabel and regionprops to get the orientation. Then call imrotate() to rotate it by the negative of that angle.
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 = 22;
% 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 color demo image.
folder = 'D:\Temporary stuff';
baseFileName = 'plug3.png';
% 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]);
grayImage = rgb2gray(rgbImage);
subplot(2, 2, 2);
imshow(grayImage);
title('Gray Scale Image', 'FontSize', fontSize);
binaryImage = grayImage < 250;
binaryImage = bwareaopen(binaryImage, 1000); % Get rid of small blobs.
subplot(2, 2, 3);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Orientation');
rotatedImage = imrotate(rgbImage, -measurements(1).Orientation);
subplot(2, 2, 4);
imshow(rotatedImage);
title('Rotated Image', 'FontSize', fontSize);
  1 Comment
Image Analyst
Image Analyst on 24 Sep 2014
Edited: Image Analyst on 1 Oct 2014
Dan, what's the story? Are you still alive? Did my code do the rotation like you want?

Sign in to comment.


Joseph Cheng
Joseph Cheng on 12 Sep 2014
the documentation on imcrop (correct function name) should tell you if it is correct, but i think it looks to be correct.
I would try this instead, use the original image and use the outer most points to find a temporary set of "cropped" the image corners. Then use the corners to find the image rotation angle to get the red points to be inline with cropped image edge then use matlab to rotate and crop.

Community Treasure Hunt

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

Start Hunting!