From: Kris zenitis on
Hallo there!!
I have this code which mark specific rectangles in a graysclae picture

function M = imrect(I,ltx,lty,rbx,rby)
% IMRECT Draw Rectangles on an Gray-level Image
% imrect(I), where I is an Gray-level image matrix
% ltx(i), lty(i): left top (x,y)
% rbx(i), rby(i): right bottom (x,y)
% return M, the image matrix with rectangles drawed.

len = length(ltx);
for i=1:len
for p=ltx(i):rbx(i)
I(lty(i),p) = 0;
I(rby(i),p) = 0;
end
for p=lty(i):rby(i)
I(p,ltx(i)) = 0;
I(p,rbx(i)) = 0;
end
end
M = I;

Firstly I want to use imrect in colourful picture and to have colour rectangular(or secondly just to have colourful rectangles in a grayscale picture). How can I cope with the first question? And it is possible the second?
From: Walter Roberson on
Kris zenitis wrote:

> Firstly I want to use imrect in colourful picture and to have colour
> rectangular

Have you tried just rect() and set the EdgeColor and FaceColor for that
rectangle ?

> (or secondly just to have colourful rectangles in a grayscale
> picture).

If you use rect() it will not form part of the image itself; rect() does
not care what kind of image might happen to be below it.
From: ImageAnalyst on
imrect() is a reserved function name in the Image Processing Toolbox.
If you have, or ever plan on using that toolbox, you should avoid
using the function name imrect for your own custom functions or you
will override the built-in imrect().

It's also not a great idea to use I as a variable name. I know it's
case sensitive (so won't get confused with i, the imaginary number)
but it seems like you're on the slippery slope of using bad variable
and function names so it might be good to start improving on that.
Otherwise you'll soon start doing things like using i for index
variable names - oh wait, you already did that too. Bummer.

Here's a little demo on using imellipse to create outline and solid
masks and to burn them into the image (like you want to do). Adapting
it for imrect is simple and straightforward. By the way, you might
want to note how instructive it is that I use descriptive variable
names - imagine what a confusing mess it would be if all my variables
were 1 or 2 letters.

clc;
clear;
close all;
workspace;
% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

filename = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos
\cell.tif'; % Standard demo image.
originalImage = imread(filename);
subplot(3, 2,1);
imshow(originalImage, []);
title('Original Image');
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
[rows, columns, numberOfColors] = size(originalImage)

% Make a solid ellipse.
rowVector = linspace(1, rows, rows);
colVector = linspace(1, columns, columns);
[x,y] = meshgrid(colVector, rowVector);
size = .5;
semiAxisX = columns / 2;
semiAxisY = rows / 4;
centerX = columns / 2;
centerY = rows / 2;
solidEllipse = ( ((x-centerX)/semiAxisX).^2 + ((y-centerY)/
semiAxisY).^2 <= size);
subplot(3, 2, 3);
imshow(solidEllipse);
title('Solid Ellipse');

% Make just a perimeter of an ellipse.
smallerEllipse = imerode(solidEllipse, ones(3));
perimeterEllipse = logical(solidEllipse - logical(smallerEllipse));
subplot(3, 2, 4);
imshow(perimeterEllipse);
title('Perimeter Ellipse');

% Write the solid ellipse into the image and display it.
originalPlusSEllipse = originalImage;
originalPlusSEllipse(solidEllipse) = 255;
subplot(3, 2, 5);
imshow(originalPlusSEllipse);
title('Solid Ellipse Written into Original Image');

% Write the perimeter ellipse into the image and display it.
originalPlusPEllipse = originalImage;
originalPlusPEllipse(perimeterEllipse) = 255;
subplot(3, 2, 6);
imshow(originalPlusPEllipse);
title('Perimeter Ellipse Written into Original Image');
From: Kris zenitis on
ok thanks a lot for your help. The above fantastic code is to draw elipse i want rectangulars. I ve been confused a little. I must change the elipse to rectangular and after that how i manage with the coordinates?
From: ImageAnalyst on
On May 20, 7:21 pm, "Kris zenitis" <gio.1...(a)hotmai.com> wrote:
> ok thanks a lot for your help. The above fantastic code is to draw elipse i want rectangulars. I ve been confused a little. I must change the elipse to rectangular and after that how i manage with the coordinates?

----------------------------------------------------------------
I'm not even sure what your intent is. Do you want to change your
image by "burning" new values into it? To do that, you'd assign pixel
values like you're doing, but in a vectorized way instead of in
loops. If you want to burn colored lines into the image then you'd
have to convert your grayscale image into a color image with the
cat(3, grayImage, grayImage, grayImage) command, or the
ind2rgb(grayImage) command.

Or do you just want to display a rectangle over your image, say with
the plot() function? This will not change your image pixel values at
all, it will just put a rectangle over your image, in whatever color
you specify.
plot([x1 x2 x2 x1 x1], [y1 y1 y2 y2 y1], 'Color', [1 1 0]);