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 件のコメント
Kurt
2022 年 10 月 20 日
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
Kurt
2022 年 10 月 20 日
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'
Eric Delgado
2022 年 10 月 20 日
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?
Kurt
2022 年 10 月 20 日
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)
Kurt
2022 年 10 月 20 日
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)

Kurt
2022 年 10 月 20 日
Agreed. We're working on getting the Image Processing Toolbox, but in the mean time...
Thanks
Eric Delgado
2022 年 10 月 21 日
Great to hear! Now that you are super happy with my support, you accept my answer, ok? :)
Kurt
2022 年 10 月 21 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
