How to draw same size rectangle on different image location

Hi,
I have selected random points using the below code
m = imread('a.jpg');
figure, imshow(m);
random_num = 50;
l = randi(numel(m), 1, random_num);
m(l) = 255;
figure, imshow(m);
Now I want to draw circle/rectangle for each random points. circle/rectangle for all points will be of equal size.
Can anyone assist me how to do that please.
Added an image for your information.

 Accepted Answer

See attached demo.

4 Comments

Small circles fitted inside the big circle. I want to draw only small circles in my color image instead fitting the small circles in the big one. I am trying to figure out your code but can not get through it how to use my image to draw the small circles only.
Well that was just my canned demo. I expected you to adapt it. Here, I adapted it some for you. You still have to do it for every color channel if you have a color image, and apply it to an existing photo instead of a blank canvass if that's what you need.
% M-file to place multiple small circles in an image.
% Clean up
close all;
clc;
fontSize = 15;
% Initialize some parameters.
numberOfSmallCircles = 25; % Number of small circles
smallCircleOutsideValue = 0.2;
smallCircleInsideValue = 0.8;
smallCircleRadius = 25; % small circle radius
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
% Initialize an image to hold one single small circle.
smallCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
[x, y] = meshgrid(1:smallCircleRadius*2, 1:smallCircleRadius*2);
singleCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
singleCircleImage((x - smallCircleRadius).^2 + (y - smallCircleRadius).^2 <= smallCircleRadius.^2) = smallCircleInsideValue;
% Display it in the upper right plot.
subplot(1,2, 1);
imshow(singleCircleImage, []);
axis on;
title('Single Small Circle (scaled to fit)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
% Get random coordinates in the big image where
% we will place the upper left corner of the small circle.
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : numberOfSmallCircles
% Find the square in the big image where we're going to add a small circle.
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Make outside the circles the outside color.
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
% Display it in the lower left plot.
subplot(1,2, 2);
imshow(manySmallCircles);
axis on;
title('Many Small Overlapping Circles', 'FontSize', fontSize);
I am pretty close to it from your code. But one issue I am having is I do not want to fill the circle and need to see the center of each circle.
I am trying to do this:
% M-file to place multiple small circles in an image.
% Clean up
close all;
clc;
fontSize = 15;
% Initialize some parameters.
numberOfSmallCircles = 25; % Number of small circles
smallCircleOutsideValue = 0.2;
smallCircleInsideValue = 0.8;
smallCircleRadius = 25; % small circle radius
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
smallCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
[x, y] = meshgrid(1:smallCircleRadius*2, 1:smallCircleRadius*2);
singleCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
singleCircleImage((x - smallCircleRadius).^2 + (y - smallCircleRadius).^2 <= smallCircleRadius.^2) = smallCircleInsideValue;
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
% Get random coordinates in the big image where
% we will place the upper left corner of the small circle.
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : numberOfSmallCircles
% Find the square in the big image where we're going to add a small circle.
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
linearIndices = randi(numel(manySmallCircles), 1, k);
% Set those lcoations to be white.
manySmallCircles(linearIndices) = 1;
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
figure, imshow(manySmallCircles);
When I have used
linearIndices = randi(numel(manySmallCircles), 1, k);
% Set those lcoations to be white.
manySmallCircles(linearIndices) = 1;
this to see if it the circles are drawn based on the selected random points it seems it is giving me different points and circles are in different points.
Not sure what I am doing wrong.
If I use them outside the loop it is not working as well because it is obvious that it will select different points.
Thank you Image analyst. I have used separate channels to do what you have said. Not perfect but I am happy.

Sign in to comment.

More Answers (2)

Try to adjust this protocol to your problem :
L=0.5;
N=100;
for n=1:N
p=randn;
p2=randn;
r=p+L;
r2=p2+L;
x=[p r r p p];
y=[p2 p2 r2 r2 p2]; plot(x,y,'k')
hold on;
end
title(' Random positions of same size rectangles')

2 Comments

Trying to figure out your code how to use it in my color image not in a white blank image window but can not figure it out.
I have figured it out with your one as well. Thank you very much.

Sign in to comment.

This is a filter operation. I am 95% sure you cancan get away with using imdilate with a circle or square structral element of your liking. Basically make your randomcpoints 1 and everything else 0, and dilate that image.

4 Comments

Sorry I did not get the answer. What I want is to draw the circles/rectangles considering the random points. Filter will not let me to draw the circles. is it? May be I am wrong. Could you give a demo code as you have mentioned that make randompoints as 1 and rests are 0.
The reason I need to draw is I want to calculate the local maxima on those rectangle regions.
Maybe I am not understanding you. Do you basically want to draw circles at random locations? Like the image you attached?
Yes you are right.
I have a color image. in that image I have selected some random points. I want to draw circles in that image using the random point location.
Isn't that what I showed in my answer?

Sign in to comment.

Asked:

on 21 Nov 2014

Commented:

on 22 Nov 2014

Community Treasure Hunt

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

Start Hunting!