필터 지우기
필터 지우기

Draw multiple polygons on figure

조회 수: 10 (최근 30일)
Gee
Gee 2016년 6월 8일
편집: Gee 2016년 6월 8일
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!

답변 (2개)

KSSV
KSSV 2016년 6월 8일
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.
  댓글 수: 1
Gee
Gee 2016년 6월 8일
Sorry, I should have been more clear. I'm trying to draw 4 sided polygons by hand with impoly (see added code above);

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 6월 8일
Put it in a while or for loop.
  댓글 수: 1
Gee
Gee 2016년 6월 8일
편집: Gee 2016년 6월 8일
I tried putting it in a while loop, but it won't let me draw on the figure:
while l <= 4
h(l) = impoly();
api = iptgetapi(h(l));
api.setColor(l);
api.addNewPositionCallback(@(p) title(['Draw ROI for box ' l]));
pos(l) = ceil(getPosition(h(l)))
l = l + 1;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by