How do I add a draw rectangle function to an image app?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I used App Designer to create a frame containing an image. Now I want to draw a rectangle inside that image. None of the examples I have seen describe how to do this in an object-oriented manner for Matlab 2022.
My main routine contains the following code:
scan = sky_frame; % draw the background .gif image
value = [20 20 30 50]; % define the rectangle coordinates
scan.drawRect(value); % call the drawRect function in sky_frame to draw the rectangle
The sky_frame class is defined as follows:
classdef sky_frame < matlab.apps.appBase
properties(Access = public)
etc.
end
% callbacks
methods(Access = Public)
function drawrect(value)
rectangle('Position',value);
end
end
When I run this, the .gif image appears, but "rectangle" creates a new figure on the desktop, outside my original sky_frame object. How do I force the code to draw the rectangle inside the sky_frame object at the coordinates specified?
채택된 답변
Eric Delgado
2022년 10월 20일
편집: Eric Delgado
2022년 10월 20일
Hey... you have to show your image in an uiaxes and create a handle to your ROI as property of the app, so you can draw your ROI and change it later. See app attached. Hope it's helpful! :)
% Image button callback
function ImageButtonPushed(app, event)
[file, path] = uigetfile({'*.png'; '*.jpg'; '*.jpeg'});
figure(app.UIFigure)
if ~isempty(file)
try
imshow(fullfile(path, file), 'Parent', app.UIAxes)
catch ME
uialert(app.UIFigure, ME.message, 'error', 'Icon', 'error')
end
end
end
% Rectangle button callback
function RectangleButtonPushed(app, event)
app.roi = drawrectangle(app.UIAxes);
end

댓글 수: 12
I'm not pushing a button. There are no events to latch onto. I just want to draw a rectangle over the image that I loaded. Actually, a whole bunch of rectangles.
Eric Delgado
2022년 10월 20일
편집: Eric Delgado
2022년 10월 20일
Ok. So... just put those "interactions" in the startup of your app. Instead of drawrectangle, use images.roi.Rectangle.
imshow(fullfile(path, file), 'Parent', app.UIAxes)
for ii = 1:N % N rectangles
app.roi(ii) = images.roi.Rectangle(app.UIAxes, 'Position', [xo(ii), yo(ii), xWidth(ii), yWidth(ii)]);
end
The problem for me is that my image is embedded in an App Designer-generated Panel.
app.Panel = uipanel(app.UIFigure);
app.Image = uiimage(app.Panel);
img = "image.gif";
app.Image.ImageSource = img;
app.UIFigure.Visible = 'on';
This part works. But, how do I reference the image externally to stick a rectangle on it? I played with the images.roi.rectangle example from the command line, and I couldn't get it to draw a rectangle either.
Unable to resolve the name 'images.roi.Rectangle'
You can't use uiimage for this. You have to "plot" the image in an axes...
Working with ROI demand Image Processing Toolbox, do you have it?
Shoot... apparently not. Are there any other options for me?
Eric Delgado
2022년 10월 20일
편집: Voss
2024년 10월 30일
[Redacted]! :)
Image Processing Toolbox is super important... you can get a trial (30 days) from "Add-ons >> Get Add-ons". If it's not an option, you can use matrix operations to create your rectangles ROIs.
I = imread('YourImage.png');
fig = figure;
ax1 = axes(fig);
imshow(I, 'Parent', ax1)
% A red rectangle...
x0 = 5; x1 = 15;
y0 = 20; y1 = 32;
I(x0:x1,[y0,y1],1) = 255;
I(x0:x1,[y0,y1],2) = 0;
I(x0:x1,[y0,y1],3) = 0;
I([x0,x1],y0:y1,1) = 255;
I([x0,x1],y0:y1,2) = 0;
I([x0,x1],y0:y1,3) = 0;
imshow(I, 'Parent', ax1)
This might work... It's more like the Python Tk graphics environment that I'm re-hosting this GUI from. Thanks.
One last question: How do I use this logic to draw a simple line from (x1,y1) to (x2,y2)? I will need to increment x and y simultaneously.
Hey @Kurt, it's still not a good approach. You should consider to use Image Processing Toolbox. But...
imgSize = 256;
I = randi(100,imgSize,'uint8');
I(:,:,2) = randi(100,imgSize,'uint8');
I(:,:,3) = randi(100,imgSize,'uint8');
imshow(I)

% A red rectangle...
x0 = 10; x1 = 100;
y0 = 20; y1 = 200;
I1 = I;
I1(x0:x1,[y0,y1],1) = 255;
I1(x0:x1,[y0,y1],2) = 0;
I1(x0:x1,[y0,y1],3) = 0;
I1([x0,x1],y0:y1,1) = 255;
I1([x0,x1],y0:y1,2) = 0;
I1([x0,x1],y0:y1,3) = 0;
imshow(I1)

% A blue surface...
I2 = I;
I2(x0:x1,y0:y1,1) = 0;
I2(x0:x1,y0:y1,2) = 0;
I2(x0:x1,y0:y1,3) = 255;
imshow(I2)

% A red line...
I3 = I;
% y = ax+b
a = (y1-y0)/(x1-x0);
b = y0 - x0*a;
for x = x0:x1
y = a*x+b;
I3(x,y,1) = 255;
I3(x,y,2) = 0;
I3(x,y,3) = 0;
end
imshow(I3)

Agreed. We're working on getting the Image Processing Toolbox, but in the mean time...
Thanks
Great to hear! Now that you are super happy with my support, you accept my answer, ok? :)
You betcha.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 ROI-Based Processing에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
