Effacer les filtres
Effacer les filtres

How Could we mirror and Freehand ROI Object in Matlab?

1 vue (au cours des 30 derniers jours)
Nikan Fakhari
Nikan Fakhari le 20 Oct 2023
Hi there,
I have created freehand object in matlab and Im able to display it over an Image using the following Code:
ROI_left = images.roi.Freehand(hAx,'Position',ROI_Left.Position);
However I would like to display a mirror (vertical) of this ROI in another image.
Could you please help with this?
Nikan
  2 commentaires
Image Analyst
Image Analyst le 21 Oct 2023
Do you mean
  1. that you want to extract the portion of the image within your hand-drawn ROI and display that cropped portion in another image (i.e., another axes), OR
  2. you want to take the freehand curve you drew, and flip the coordinates vertically and display the flipped curve over a different image in a different axes?
Nikan Fakhari
Nikan Fakhari le 21 Oct 2023
I want to exactly do the second point that you mentioned.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 22 Oct 2023
Try this demo. You can draw some ROI over the image and then it will get the (x,y) coordinates and then flip them vertically about the centroid, and then draw them over a second image. I think that's just what you're asking.
% Demo to have the user freehand draw an irregular shape over a gray scale image.
% Then it flips it about the y centroid and draws it over a new image.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is.
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
h = gcf;
h.WindowState = 'maximized';
h.NumberTitle = 'off';
h.Name = 'Demo by Image Analyst'
% Ask user to draw freehand mask.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand(); % Actual line of code to do the drawing.
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Get coordinates of the boundary of the freehand drawn region.
x = xy(:, 1); % Columns.
y = xy(:, 2); % Rows.
hold on; % Don't blow away the image.
plot(x, y, 'b-', 'LineWidth', 2);
% Close the shape by making the last point the same as the first point.
x(end+1) = x(1);
y(end+1) = y(1);
% Now make it smaller so we can show more images.
subplot(2, 2, 1);
% Plot over original image.
imshow(grayImage, []);
hold on; % Don't blow away the image.
plot(x, y, 'r-', 'LineWidth', 2);
axis on;
drawnow;
title('Original gray scale image', 'FontSize', fontSize);
% Display the freehand mask.
subplot(2, 2, 2);
imshow(binaryImage);
axis on;
title('Binary mask of the region', 'FontSize', fontSize);
caption = sprintf('Area of Mask = %d pixels', nnz(binaryImage));
xlabel(caption, 'FontSize', fontSize);
% Label the binary image and compute the centroid.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(binaryImage, grayImage, 'Area', 'Centroid');
area = measurements.Area
centroid = measurements.Centroid
% Flip y coordinates by shifting to zero and then shifting to the centroid.
yNew = -(y - centroid(2)) + centroid(2);
subplot(2, 2, 3);
% Plot over second image.
imshow(grayImage, []);
hold on; % Don't blow away the image.
plot(x, yNew, 'r-', 'LineWidth', 2);
axis on;
drawnow;
title('Image 2 with ROI flipped vertically', 'FontSize', fontSize);

Plus de réponses (1)

Walter Roberson
Walter Roberson le 21 Oct 2023
ROI_left.Vertices
will be an N x 2 array of coordinates. You can extract, transform as you want, and plot() over the new image.
What I do not know is how to create an ROI object from coordinates or from binary mask.
  3 commentaires
DGM
DGM le 21 Oct 2023
I can't run this on the forum, but it should demonstrate the concept.
inpict = imread('cameraman.tif');
% an image in an axes
ax1 = subplot(1,2,1);
imshow(inpict);
% add new ROI object interactively
ROI1 = drawfreehand(ax1);
% an image of the same size in another axes
ax2 = subplot(1,2,2);
imshow(inpict);
% vertical flip the coordinates about the image midline
newposition = ROI1.Position;
newposition(:,2) = size(inpict,1) - newposition(:,2);
% add flipped ROI object programmatically
ROI2 = images.roi.Freehand(ax2,'position',newposition);
Adapt as needed.
Walter Roberson
Walter Roberson le 21 Oct 2023
Assuming that for the purposes of mirroring, that vertices should be understood as relative to the top left edge of the original image, and that for the new image you want the vertices to be the same distance but from the top right edge, then
verts = ROI_left.Vertices;
newx = size(NewImageArray,2) - verts(:,1);
newy = verts(:,2);
figure(2);
image(NewImageArray);
hold on
plot(newx, newy);
hold off

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by