How can i extract part of a matrix when the part i'm extracting is slanted?

4 views (last 30 days)
I have an image and i used the impoly() command do draw a slanted rectangle. I have the positions of the vertex of the rectangle and i would like to copy the intensities on those indexes into another matrix. My problem is that the matrix i want to extract is slanted. This is the relevant part of my code:
filename = uigetfile; %get the file name
obj = VideoReader(filename);
thisfig = figure();
for k = 1 : nFrames
this_frame = read(obj, k);
thisax = axes('Parent', thisfig);
image(this_frame, 'Parent', thisax);
if k==nFrames
title(thisax, sprintf('Frame #%d', k));
if k==1
result=input('How many rectangles would you like to draw? ');
pos=zeros(4,2,10); %declaring a reasonable size for post
for i=1:result
pos(:,:,i) = getPosition(handle);
Image Analyst
Image Analyst on 1 Jun 2023
You can use the floating point numbers and use poly2mask to get a binary mask of the rotated part. Then you can use that to either mask the image (blacken outside of the rectangle mask) or to extract all the pixel values in a 1-D list.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 29 Sep 2013
Perhaps you should just use IMROTATE to rotate your entire image, so that the rectangle is no longer slanted. Then extract a normal box.
Matt J
Matt J on 29 Sep 2013
You said you know the vertices of your rectangle. The angle can be computed from the vertices.

Sign in to comment.

More Answers (2)

Matt J
Matt J on 15 Sep 2013
Edited: Matt J on 15 Sep 2013
I have the positions of the vertex of the rectangle and i would like to copy the intensities on those indexes into another matrix.
I assume you really mean that you want the image values enclosed by the polygon. The vertex coordinates can fall in between pixel locations and so are not indexable.
To get the enclosed values, you can do
regionValues=this_frame( createMask(handle) )
where "handle" is your handle to the impoly object.
Matt J
Matt J on 15 Sep 2013
Edited: Matt J on 15 Sep 2013
I need to extract the values inside my polygon/rectangle into a new matrix so i will be able to calculate things like the mean of the columns and etc.
Since your data has a slanted geometry, it's not clear what you mean by "mean of the columns". A column is a notion that applies to rectangular arrays. My vague impression (you have to be clearer!) is that you want to take means etc... across the columns of your original matrix, but ignoring data outside the polygon. If so, and if you have the Statistics Toolbox, then you could fill the region outside the polygon with NaNs and use NANMEAN, NANSTD, etc...
If you don't have nanmean, nanstd, etc... you can re-invent them by doing things like

Sign in to comment.

Image Analyst
Image Analyst on 15 Sep 2013
See my well commented demo below. You should be easily able to adapt it from copying and pasting rectangles to polygons using impoly() and poly2mask(). If you can't let me know.
% Lets user drag out a box on an image, then define where they want to paste it.
% Then it pastes the drawn region onto the original image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% 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.
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'eight.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram, just for fun.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Ask user to draw a box.
subplot(2, 2, 1);
promptMessage = sprintf('Drag out a box that you want to copy,\nor click Cancel to quit.');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Find the coordinates of the box.
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
axis manual
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units
% Display the cropped image.
croppedImage = grayImage(y1:y2,x1:x2);
subplot(2, 2, 3);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image
[rows2 columns2] = size(croppedImage)
promptMessage = sprintf('Click on the upper left point where you want to paste it,\nor click Cancel to quit.');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
[x, y] = ginput(1)
% Determine the pasting boundaries.
r1 = int32(y);
c1 = int32(x);
r2 = r1 + rows2 - 1;
r2 = min([r2 rows]);
c2 = c1 + columns2 - 1;
c2 = min([c2, columns]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
grayImage(r1:r2, c1:c2) = croppedImage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(2, 2, 4);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
Image Analyst
Image Analyst on 8 Nov 2014
Andrea, I recommend you start a new question after reading this. Post your image(s) and explain whatever information that you know about the image or the geometry about how it was taken. In the meantime, check out the camera calibration capability of the Computer Vision System Toolbox which I think can help you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!