フィルターのクリア

Moving ROIs within images and across images

15 ビュー (過去 30 日間)
Nandini Chatterjee
Nandini Chatterjee 2020 年 11 月 11 日
コメント済み: Auwal 2023 年 10 月 16 日
I have a code where I read in two images and display it side-by-side. I draw a region of interest within the first image, and the region is automatically shown on the second image. When I move the rectangle to another region within the first image, only that rectangle moves (not the second image's rectangle). How can I fix this so that when I move the first rectangle, the second move moves to the same position as well.
What I have now
% Read images
I = imread('cameraman.tif');
I2 = imread('cameraman.tif');
% plot image 1 & draw a rectangular ROI on images
subplot(1,2,1); imshow(I);
roi = drawrectangle('LineWidth',2,'Color','white');
subplot(1,2,2); imshow(I2);
roi2 = drawrectangle(gca,'Position',roi.Position);
% set up listeners for ROI moving events
addlistener(roi,'MovingROI',@allevents);
addlistener(roi,'ROIMoved',@allevents);
% allevents callback function displays the previous position & the current position of the ROI
function allevents(~,evt)
evname = evt.EventName;
switch(evname)
case{'MovingROI'}
disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moving current position: ' mat2str(evt.CurrentPosition)]);
case{'ROIMoved'}
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 11 日
編集済み: Ameer Hamza 2020 年 11 月 11 日
Currently, your event lister function is just priting the values. You need to set the position too. Try following code
% Read images
I = imread('cameraman.tif');
I2 = imread('cameraman.tif');
% plot image 1 & draw a rectangular ROI on images
subplot(1,2,1); imshow(I);
roi = drawrectangle('LineWidth',2,'Color','white');
subplot(1,2,2); imshow(I2);
roi2 = drawrectangle(gca,'Position',roi.Position);
% set up listeners for ROI moving events
addlistener(roi,'MovingROI',@(r1,evt) allevents(r1,evt,roi2));
addlistener(roi,'ROIMoved',@(r1,evt) allevents(r1,evt,roi2));
% allevents callback function displays the previous position & the current position of the ROI
function allevents(roi1,evt,roi2)
evname = evt.EventName;
roi2.Position = roi1.Position;
switch(evname)
case{'MovingROI'}
disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moving current position: ' mat2str(evt.CurrentPosition)]);
case{'ROIMoved'}
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end
Or a simpler solution using linkprop()
% Read images
I = imread('cameraman.tif');
I2 = imread('cameraman.tif');
% plot image 1 & draw a rectangular ROI on images
subplot(1,2,1); imshow(I);
roi = drawrectangle('LineWidth',2,'Color','white');
subplot(1,2,2); imshow(I2);
roi2 = drawrectangle(gca,'Position',roi.Position);
linkprop([roi, roi2], 'Position')
  15 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 11 月 21 日
Try this definition of event callback function
function allevents(roi,~,I,I2,I3,ax1,ax2,ax3)
rect = roi.Position;
ax1.Children.CData = imcrop(I, rect); axis(ax1, 'auto');
ax2.Children.CData = imcrop(I2, rect); axis(ax2, 'auto');
ax3.Children.CData = imcrop(I3, rect); axis(ax3, 'auto');
end
Nandini Chatterjee
Nandini Chatterjee 2020 年 11 月 21 日
Thank you so much! It works!

サインインしてコメントする。

その他の回答 (1 件)

Auwal
Auwal 2023 年 10 月 16 日
Thanks everyone that contributed.
please may i know if it is possible to get the pixel values in the defined roi and calculate the mean value.
Thanks in advace
  2 件のコメント
Image Analyst
Image Analyst 2023 年 10 月 16 日
Yes
croppedImage = imcrop(originalImage, roi.Position); % Get values in the rectangle.
meanValue = mean2(croppedImage)
Auwal
Auwal 2023 年 10 月 16 日
Thanks so much Image Analyst. It works fine.
However, I am not sure why is not working with circular roi "drawcircle"
Many thanks

サインインしてコメントする。

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by