Draw multiple polygons on figure
10 views (last 30 days)
Show older comments
Hi all,
I'm relatively new to matlab and am currently trying to use impoly to draw multiple 4-sided polygons on a figure and then use getposition to get each individual position. I have this working for one polygon, but I'm not sure how to do it for multiple polygons on the one figure.
vidObj = VideoReader('final1.mp4');
vidHeight = vidObj.Height;
vidWidth = vidObj.Width;
s = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),...
'colormap',[]);
k = 1;
while hasFrame(vidObj)
s(k).cdata = readFrame(vidObj);
k = k+1;
end
% select ROI
figure('Name','Please Click to Select ROI');
imshow(s(1).cdata)
h = impoly();
h2 = impoly();
api = iptgetapi(h);
api.setColor('green');
api.addNewPositionCallback(@(p) title('Select Region of Interest'));
api.addNewPositionCallback(@(p) title('Select 2nd Box'));
pos = ceil(getPosition(h))
pos2 = ceil(getPosition(h2))
patch(pos,pos2,'red')
x = pos;
y = pos2;
c = [0; 1];
FigHandle = figure('Position', [100, 100, vidWidth, vidHeight])
patch(x,y,c);
pos = ceil(getPosition(h));
% make binary mask
masked = poly2mask(pos(:,1),pos(:,2),vidHeight,vidWidth);
minX = min(pos(:,1));
maxX = max(pos(:,1));
minY = min(pos(:,2));
maxY = max(pos(:,2));
figure('Name','Region of Interest');
roi_frame = s(1).cdata(minY:maxY,minX:maxX,:);
roi_frame(:,:,1) = (roi_frame(:,:,1) .* uint8(masked(minY:maxY,minX:maxX)));
roi_frame(:,:,2) = roi_frame(:,:,2) .* uint8(masked(minY:maxY,minX:maxX));
roi_frame(:,:,3) = roi_frame(:,:,3) .* uint8(masked(minY:maxY,minX:maxX));
imshow(roi_frame)
% write out a temporary video file of the ROI for use in various algo's
% add text to video and write out
v = VideoWriter('roi_video.avi');
open(v);
for k = 1:length(s)
% frameOut = s(k).cdata(pos(2):pos(2)+pos(4),pos(1):pos(1)+pos(3),:);
roi_frame = s(k).cdata(minY:maxY,minX:maxX,:);
roi_frame(:,:,1) = (roi_frame(:,:,1) .* uint8(masked(minY:maxY,minX:maxX)));
roi_frame(:,:,2) = roi_frame(:,:,2) .* uint8(masked(minY:maxY,minX:maxX));
roi_frame(:,:,3) = roi_frame(:,:,3) .* uint8(masked(minY:maxY,minX:maxX));
writeVideo(v,roi_frame);
end
close(v);
Any help would be great!
0 Comments
Answers (2)
KSSV
on 8 Jun 2016
Are you looking for something like this?
x = linspace(0,1,10) ;
y = linspace(0,1,10) ;
[X,Y] = meshgrid(x,y) ;
surf(X,Y) ;
view([0 90])
If not explain your needs further.
See Also
Categories
Find more on Read, Write, and Modify Image 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!