Drawing a rectangle on top of an image
이전 댓글 표시
Say I have a rectangle with some dimensions. If I want to draw a rectangle over an image using for instance `plot`, if I start as follows:
hold on
imshow(img)
Then, regarding `plot` and my rectangle. How would they fit in this process?
Thanks.
답변 (1개)
Image Analyst
2013년 12월 19일
imshow(img);
hold on;
% Then, from the help:
rectangle('Position',[0.59,0.35,3.75,1.37],...
'Curvature',[0.8,0.4],...
'LineWidth',2,'LineStyle','--')
댓글 수: 4
med-sweng
2013년 12월 19일
Image Analyst
2013년 12월 19일
You probably didn't adjust your position values properly.
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
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);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
hold on;
axis on;
% Then, from the help:
rectangle('Position',[95,35,40,50],...
'Curvature',[0.8,0.4],...
'EdgeColor', 'r',...
'LineWidth', 3,...
'LineStyle','-')
Katia-Renae Purnell
2022년 2월 25일
I see you calculate the size of the image, but I don't see where it is used. How do you calculate the offset so that the rectangle is actually drawn over the image?
DGM
2022년 2월 26일
As to how you find the correct offset, that certainly might be a function of the image height and width as you suspect. The problem with writing a generalized example is that there are multiple ways to find an offset for an arbitrary location in space. In some case, you might want to draw a rectangle over some text to censor it. In some example, you might want to draw a rectangle around a peak value. In the above example, the rectangle is drawn around a face. Programmatic solutions to these three tasks would be significantly different and some are individually involved enough that it's arguable that they're beyond the scope of a simple rectangle() demo.
I don't know for certain how Image Analyst found the face, but I imagine that if I needed to draw a rectangle over one such image, I would probably just use a datatip or impixelinfo() to fetch an approximate set of coordinates.
If your're implicitly asking how you can programmatically get the offsets to place a rectangle() object in a certain spot on various images, you'll need to start with a good description of what your images look like and what your goals are. Depending on the scope of the problem, it may be best to open a new question so as to get more eyes on it.
카테고리
도움말 센터 및 File 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!