display several rectangles in color on your image

조회 수: 5 (최근 30일)
Niki
Niki 2014년 3월 9일
답변: Image Analyst 2014년 3월 9일
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

채택된 답변

Image Analyst
Image Analyst 2014년 3월 9일
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

추가 답변 (1개)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2014년 3월 9일
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
Image Analyst
Image Analyst 2014년 3월 9일
I think he wants solid rectangles with transparency - that's why he used patch.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Display Image에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by