MATLAB Answers

display several rectangles in color on your image

33 views (last 30 days)
Niki
Niki on 9 Mar 2014
Answered: Image Analyst on 9 Mar 2014
Hi,
I am using the following lines to put a rectangle on my image.
patch([850 850 900 900],[2 3.6 3.6 2],'r','facecolor',[1 .8 .8],...
'edgecolor',[1 .2 .2],
'facealpha',0.7)
There is several problem with it. First it changes the quality of my image second the edge color is black and also not ok third, do you have any alternative way to put several rectangle on a figure

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 9 Mar 2014
See my demo:
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 = 33;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
numRectangles = 20;
% Don't allow patches to blow away image and prior patched.
hold on;
for k = 1 : numRectangles
% Get random coordinates.
x1 = columns * rand(1);
x2 = columns * rand(1);
y1 = rows * rand(1);
y2 = rows * rand(1);
% Get random edge color and face colors.
edgeColor = rand(1,3);
faceColor = rand(1,3);
% Display a rectangular solid patch.
patch([x1,x2,x2,x1], [y1,y1,y2,y2], 'r',...
'facecolor', faceColor,...
'edgecolor', edgeColor,...
'facealpha',0.7, ...
'LineWidth', 3)
% See if they want to draw another one.
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end

  0 Comments

Sign in to comment.

More Answers (1)

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 9 Mar 2014
You can plot it!
plot([x1 x1+a x1+a x1 x1], [y1 y1 y1+b y1+b y1])
where (x1, y1) is the starting point of your rectangle and a and b are the sides of the rectangle.

  1 Comment

Image Analyst
Image Analyst on 9 Mar 2014
I think he wants solid rectangles with transparency - that's why he used patch.

Sign in to comment.

Sign in to answer this question.

Tags


Translated by