How to draw same size rectangle on different image location

2 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2014-11-22
See attached demo.
  4 个评论
Tania
Tania 2014-11-22
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.
Tania
Tania 2014-11-22
Thank you Image analyst. I have used separate channels to do what you have said. Not perfect but I am happy.

请先登录,再进行评论。

更多回答(2 个)

Youssef  Khmou
Youssef Khmou 2014-11-22
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 个评论
Tania
Tania 2014-11-22
编辑:Tania 2014-11-22
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.
Tania
Tania 2014-11-22
I have figured it out with your one as well. Thank you very much.

请先登录,再进行评论。


Ahmet Cecen
Ahmet Cecen 2014-11-21
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 个评论
Tania
Tania 2014-11-22
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by