![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/314994/image.png)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Plot a circle of Gaussian varying intensity
20 views (last 30 days)
Show older comments
I want to plot a circle who's intensity varies as a Gaussian distribution, ie. to be maximum at the centre and tail off as the radius is increased. I have developed this code in which the intensity can vary according to the radius, but I am unsure on how to follow.
imagelength=100;
middle=(imagelength+1)/2;
radius=20;
dimmerRadius=15;
brightness=256;
dimmerBrightness=150;
image=ones(imagelength);
for i=1:imagelength
x=-imagelength+middle+i; %change y coordinate by adding term
for j=1:imagelength
y=-imagelength+middle+j; %change x coordinate by adding term
if(x^2+y^2<=radius^2) %equation of circle
image(i,j)=brightness;
end
if(x^2+y^2<=dimmerRadius^2)
image(i,j)=dimmerBrightness;
end
end
end
imagesc(image)
axis square
axis off
colormap gray
caxis([1 256])
Many thanks.
Accepted Answer
Image Analyst
on 12 Jun 2020
See my attached demo. There are lots of parameters to control exactly the way it looks so adapt them to get what you want.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/314994/image.png)
25 Comments
Image Analyst
on 20 Jun 2020
Sorry, no.
reham elnabawy
on 21 Jun 2020
Edited: reham elnabawy
on 21 Jun 2020
Image Analyst: I tried your code which is the Gaussian circles demo, however, I would like to ask you a question which is: I want to set the center of the circle each time I draw the circle, meaning that I am interested in the part of the code of "smallCircleRadius" but I want to delete the background black image meaning that I want just the circle without the black background so how could I do that? Also, as I stated above, I would like to draw the circle at a certain specified center where the center has an x and y coordinates so how could I do that in your code?
Image Analyst
on 22 Jun 2020
Yes you can. Just edit out anything having to do with "big circle mask".
reham elnabawy
on 22 Jun 2020
Edited: reham elnabawy
on 22 Jun 2020
Image Analyst: Yes I know that theoretically, but which lines of your code should I change to get what I want please? I just want to remove the black background behind the small circle and to be able to set the center of the circle.
Image Analyst
on 22 Jun 2020
OK, I deleted the lines of code for you:
% Demo to place multiple small circles of Gaussian profile
% Clean up
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Initialize some parameters.
numberOfSmallCircles = 50; % Number of small circles
smallCircleOutsideValue = 0;
smallCircleInsideValue = 0.8;
smallCircleRadius = 31; % small circle radius
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
% Initialize an image to hold one single small circle.
sigma = smallCircleRadius * .4;
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
% Normalize to 0-1.
singleCircleImage=singleCircleImage/max(max(singleCircleImage));
% Display it in the upper right plot.
subplot(1, 2, 1);
imshow(singleCircleImage, []);
title('Single Small Circle (scaled to fit)', 'FontSize', fontSize);
axis('on', 'image');
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));
if x1 < 1
x1 = 1;
end
if y1 < 1
y1 = 1;
end
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
if x2 > bigImageWidth
x2 = bigImageWidth;
end
if y2 > bigImageHeight
y2 = bigImageHeight;
end
% 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, []);
title('Many Small Overlapping Circles', 'FontSize', fontSize);
axis('on', 'image');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/320494/image.png)
reham elnabawy
on 22 Jun 2020
Edited: reham elnabawy
on 22 Jun 2020
Image Analyst: Firstly, thank you. Secondly, in your code, where do you assign the center of the circle? Also, I want to get rid of the black background image that the small circle is drawn on so how could I do that? Note: I already have an image that is of black background so I just need the circle only.
Image Analyst
on 22 Jun 2020
I don't get the center of the circle. We don't need it and won't use it. Each circle is a little square matrix that we need to paste onto the canvass so what we need is the starting and stopping rows and starting and stopping columns. I get the starting row and columns with these lines:
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
The small Gaussian image is singleCircleImage. If you don't want to drop/paste it onto a big black canvass like I did, you're perfectly welcome to past it onto some other canvass. If you don't want to paste it (assign it), you can ADD it to the canvass image. That way you'll still see some of the underlying canvass image.
reham elnabawy
on 23 Jun 2020
Image Analyst:
Where in your code do you drop/paste the singleCircleImage on your big black canvas?
As I have an image that is initially black and by means of imshow and hold on, I fill the circle to be drawn on top of this image and give the fill command the rgb color which is white. However, I need the circle to not be as if it is bitmapped however, I want the circle to be having luminance effect not just white color so that's why I just need to apply gaussian effect to the circle instead of coloring it so how could I do that please?
Image Analyst
on 23 Jun 2020
RIght here is where I drop the single circle onto the canvass that has all the other circles on it:
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
manySmallCircles is the full-sized canvass, and singleCircleImage is the small image containing one single circle. Not sure what you mean by "bitmapped" because all images are essentially bitmapped. They are all discrete matrices of pixel values, discrete both spatially (rows and columns) and in intensity (gray levels). Note that my "circles" are not solid, one-intensity circles, they have a Gaussian profile. Zoom in and look closely. Look at the big image of the single circle - you can see there that it has a "gaussian effect to the circle" as you call it. So why do you say I'm "coloring" it instead of adding a Gaussian-profiled hump onto the image?
If you want color, there are several ways to do that depending on if you wanted replacement or addition. In my demo I'm doing addition. I think you might be able to make the single circle color and then just add a third dimension to the addition like this (untested):
% Add in one small RGB circle to the existing big RGB image.
manySmallCircles(y1:y2, x1:x2, :) = manySmallCircles(y1:y2, x1:x2, :) + singleCircleImage;
reham elnabawy
on 23 Jun 2020
Image Analyst: I mean by "bitmapped" that the circle that I draw in my own code is colored and it is not shown as Gaussian profile so I need my circle to be not just colored with intensity 255 but to be having gaussian luminance effect. So how could I do that please?
Image Analyst
on 24 Jun 2020
Didn't I do that? How do my spots not have the Gaussian luminance effect???
reham elnabawy
on 24 Jun 2020
Edited: reham elnabawy
on 24 Jun 2020
Image Analyst:
Yes you did that but as you can see, the circle drawn in your code is a set of pixels grouped together that is drawn based on the number of rows and the number of columns however, what I need to do is, that I need to draw the circle that has gaussian effect just the same view as yours before maximizing the figure so that there will be no pixelization shown. Or in other words, I need the circle to be a solid circle that has gaussian luminance effect instead of this pixelization effect so is there a way to do that?
Note: you can refer to the attached image named "reference image" to know the circle view that I need to reach.
Also, please refer to the second image named "bitmappedVsGaussian" where the left image is the image that I already reached in my own code however, I would like to reach the right hand side image
Image Analyst
on 24 Jun 2020
reham, that looks just like what I'm doing. I'm not doing what you call bitmapped - making solid discs of uniform gray level. It seems the only difference is you're putting your Gaussians on a regular hex grid and varying the Gaussian sigma. So when you do that to my code you get this:
% Demo to place multiple small circles of Gaussian profile
% Clean up
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Initialize some parameters.
numberOfSmallCircles = 50; % Number of small circles
smallCircleOutsideValue = 0; % Background color.
smallCircleRadius = 15; % small circle radius
bigImageWidth = 470;
bigImageHeight = 349; % square area 0f 500*500
% fileName = 'reference image.png';
% grayImage = imread(fileName);
% [rows, columns, numColors] = size(grayImage)
% imshow(grayImage);
% impixelinfo;
xStart = 32;
xSpacing = 29;
yStart = 26;
ySpacing = 25;
x = xStart : xSpacing : bigImageWidth
y = yStart : ySpacing : bigImageHeight
[x, y] = meshgrid(x, y);
evenRows = 2 : 2 : size(x, 1);
x(evenRows, :) = x(evenRows, :) + 16;
x = x(:);
y = y(:);
hold on;
plot(x(:), y(:), 'r.', 'MarkerSize', 15);
% Initialize an image to hold one single small circle.
sigma = smallCircleRadius * .4;
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
% Normalize to 0-1.
% singleCircleImage = singleCircleImage / max(singleCircleImage(:));
% Display it in the upper right plot.
subplot(2, 1, 1);
imshow(singleCircleImage, []);
title('Single Small Circle', 'FontSize', fontSize);
axis('on', 'image');
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 = x;
smallUpperLeftY = y;
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : length(smallUpperLeftX)
% Find the square in the big image where we're going to add a small circle.
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
if x1 < 1
x1 = 1;
end
if y1 < 1
y1 = 1;
end
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
if x2 > bigImageWidth
continue;
end
if y2 > bigImageHeight
continue;
end
% Initialize an image to hold one single small circle.
sigma = smallCircleRadius * 0.5 * rand(1);
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
% Normalize to 0-1.
singleCircleImage = singleCircleImage / max(singleCircleImage(:));
fprintf('Dropping Gaussian at upper left = (%d, %d).\n', x1, y1);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
% Display it in the lower left plot. This will slow it down though.
% subplot(2, 1, 2);
% imshow(manySmallCircles, []);
% title('Many Small Gaussians', 'FontSize', fontSize);
% axis('on', 'image');
% drawnow;
end
% Make outside the circles the outside color.
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
% Display it in the lower left plot.
subplot(2, 1, 2);
imshow(manySmallCircles, []);
title('Many Small Gaussians', 'FontSize', fontSize);
axis('on', 'image');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/321433/image.png)
Now what you said you want is this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/321436/image.png)
Tell me that they're much different. If so, then I'm sure a smart engineer like you will be able to modify my code, like modifying the amplitude as well (inside the loop of course) or whatever.
reham elnabawy
on 25 Jun 2020
Image Analyst:
Fristly, thank you for your help. Secondly, I think there is a misunderstand. I didn't talk about your code from the beginning, as I am talking about my code that I did before seeing your code where my code includes btmapped not gaussian circles. And while I was searching for how to perform gaussian luminance profile on my drawn circles, I found your code so I asked you the above questions. So off course, this means that it is obvious that you are applying the gaussian profile to the circle and I was trying to know how you did that and that's it :)
Last but not least, indeed your output image in your last comment is the same as what I attached to you. So there is no need to tell me "smart engineer" as if you are making fun of me, as I think you misunderstand me from the beginning. However, I would like to thank you again for your help and I will try your code and get back to you if I have a problem.
reham elnabawy
on 26 Jun 2020
Edited: reham elnabawy
on 26 Jun 2020
Image Analyst:
I have a question please.
In your last code that you sent to me, you are doing the sigma in terms of a random value however, what I need to do is, that I don't want to get random circles lightened but I need the following:
There is an input image that is for example a flower and I would like to apply your code to this image so that the more brightened circles are the circles located at the foreground positions.
Note: please see the two attached images where the first image is the input image and the second image is the desired output image.
For more clarification, please also find attached another set of images named "input and output 2" where the left image is the input image and the right image is the output image.
Please note that I need the code to be generic so this means that the code should work on any input image.
Thank you in advance.
Image Analyst
on 26 Jun 2020
Try having sigma be a factor times the gray level at the center location. First read in your gray scale image, before the loop, then in the loop:
xMid = round(mean([x1,x2]));
yMid = round(mean([y1,y2]));
grayLevel = grayImage(yMid, xMid); % This changes depending on where in the image we are.
sigma = someFactor * grayLevel;
Figure out what someFactor needs to be. But it will probably be something like 0.01 or something. It will be the same for the whole program. someFactor does not vary location by location.
reham elnabawy
on 27 Jun 2020
Edited: reham elnabawy
on 27 Jun 2020
Image Analyst:
I tried to add the lines of codes you wrote to your code but there was an error which is that the sigma is of type uint8 and it should be double so, I typecasted it to be double. However, after that error was fixed by adding double typecast, I found another which is index exceeds matrix dimensions from the following line:
grayLevel = grayImage(yMid, xMid);
So how to fix this error please?
Please find below the code after adding the image which is the grayscale flower image before the loop:
numberOfSmallCircles = 50; % Number of small circles
smallCircleOutsideValue = 0; % Background color.
smallCircleRadius = 15; % small circle radius
bigImageWidth = 470;
bigImageHeight = 349;
xStart = 32;
xSpacing = 29;
yStart = 26;
ySpacing = 25;
x = xStart : xSpacing : bigImageWidth
y = yStart : ySpacing : bigImageHeight
[x, y] = meshgrid(x, y)
evenRows = 2 : 2 : size(x, 1);
x(evenRows, :) = x(evenRows, :) + 16;
x = x(:);
y = y(:);
sigma = smallCircleRadius * .4;
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
subplot(2, 1, 1);
imshow(singleCircleImage,[]);
title('Single Small Circle', 'FontSize', fontSize);
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
smallUpperLeftX = x;
smallUpperLeftY = y;
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
inputImage = imread('flower.jpg');
grayImage=rgb2gray(inputImage);
% Place the small circles one by one.
for k = 1 : length(smallUpperLeftX)
% Find the square in the big image where we're going to add a small circle.
x1 = smallUpperLeftX(k);
y1 = smallUpperLeftY(k);
x2 = x1 + singleWidth - 1;
y2 = y1 + singleHeight - 1;
if x2 > bigImageWidth
continue;
end
if y2 > bigImageHeight
continue;
end
xMid = round(mean([x1,x2]));
yMid = round(mean([y1,y2]));
grayLevel = grayImage(yMid, xMid); % This changes depending on where in the image we are.
sigma = double(0.01 * grayLevel);
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
% Normalize to 0-1.
singleCircleImage = singleCircleImage / max(singleCircleImage(:));
fprintf('Dropping Gaussian at upper left = (%d, %d).\n', x1, y1);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Display it in the lower left plot.
subplot(2, 1, 2);
imshow(manySmallCircles);
title('Many Small Gaussians', 'FontSize', fontSize);
Please fix this error for me. Also, I need another thing which is that I would like the spacing between every two consecutive gaussian circles to be a variable so that the user can easily change this variable to the desired distance between each two consecutive circles so what could be added to the code to achieve this?
Also, how to make the size of the circles as a variable so that the user will be able to enter the desired circle size? How to achieve this in the code?
Image Analyst
on 27 Jun 2020
Evidently your code requires flower.jpg, which you forgot to attach. I'll check back later.
If it doesn't like something because it's uint8, cast it to double:
sigma = 0.01 * double(grayLevel);
By the way, sorry my attempt at paying a compliment and providing encouragement by referring to you as a smart engineer fell flat.
To change the spacing, you change the spacing by adding a random value to the spacing so it won't be a constant anymore:
xStart = 32;
xSpacing = 29;
yStart = 26;
ySpacing = 25;
x = xStart : xSpacing : bigImageWidth
y = yStart : ySpacing : bigImageHeight
% Add a jitter to x and y by adding a random number.
jitterFraction = 0.10; % Percent of initial spacing you want
x = x + jitterFraction * xSpacing * rand(1, length(x));
y = y + jitterFraction * ySpacing * rand(1, length(y));
% Don't let x go below 1
x = max(x, 1);
y = max(y, 1);
x = min(x, bigImageWidth);
y = min(y, bigImageHeight);
reham elnabawy
on 27 Jun 2020
Edited: reham elnabawy
on 27 Jun 2020
Image Analyst: flower.jpg is already attached in the comment before the last one which is named "input.png" so just rename it. For the error, please try the code I wrote to you in the last comment as I already did typecast however, there is new error which is index exceeds matrix dimensions so please fix it.
Image Analyst
on 27 Jun 2020
Here, try this. It's almost done but I've already spent too much time on it and I need to leave for for you to do so you'll feel some pride of ownership.
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
inputImage = imread('flower.jpg');
grayImage = rgb2gray(inputImage);
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
smallCircleRadius = 15; % small circle radius
bigImageWidth = size(grayImage, 2);
bigImageHeight = size(grayImage, 1);
xStart = 1;
xSpacing = round(bigImageWidth / 10);
yStart = 1;
ySpacing = round(bigImageHeight / 10);
x = xStart : xSpacing : bigImageWidth
y = yStart : ySpacing : bigImageHeight
[x, y] = meshgrid(x, y)
evenRows = 2 : 2 : size(x, 1);
x(evenRows, :) = x(evenRows, :) + 16;
x = x(:);
y = y(:);
% Add a jitter to x and y by adding a random number.
jitterFraction = 1.0; % Percent of initial spacing you want
x = round(x + jitterFraction * xSpacing * rand(length(x), 1));
y = round(y + jitterFraction * ySpacing * rand(length(y), 1));
% Don't let x go below 1
x = max(x, 1);
y = max(y, 1);
x = min(x, bigImageWidth);
y = min(y, bigImageHeight);
sigma = smallCircleRadius * .4;
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
subplot(2, 2, 2);
imshow(singleCircleImage,[]);
title('Single Small Gaussian', 'FontSize', fontSize);
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
smallUpperLeftX = x(:);
smallUpperLeftY = y(:);
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : length(smallUpperLeftX)
% Find the square in the big image where we're going to add a small circle.
x1 = smallUpperLeftX(k);
y1 = smallUpperLeftY(k);
x2 = x1 + singleWidth - 1;
y2 = y1 + singleHeight - 1;
if x2 > bigImageWidth
continue;
end
if y2 > bigImageHeight
continue;
end
xMid = round(mean([x1,x2]));
yMid = round(mean([y1,y2]));
grayLevel = grayImage(yMid, xMid); % This changes depending on where in the image we are.
if grayLevel == 0
grayLevel = 1;
end
sigma = 0.02 * double(grayLevel);
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
% Normalize to 0-1.
singleCircleImage = singleCircleImage / max(singleCircleImage(:));
fprintf('Dropping Gaussian %4d at upper left = (%d, %d) with sigma = %f.\n', k, x1, y1, sigma);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Plot the upper lefts
subplot(2, 2, 3);
plot(smallUpperLeftX + singleWidth/2, smallUpperLeftY + singleHeight/2, 'r.');
grid on;
axis ij;
title('Gaussian Points', 'FontSize', fontSize);
% Display it in the lower left plot.
subplot(2, 2, 4);
imshow(manySmallCircles, []);
title('Many Small Gaussians', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/323008/image.png)
reham elnabawy
on 28 Jun 2020
Image Analyst:
Firstly, thank you for your time and for your help. Secondly, I tried your last code and tried to get the shape of the flower in gaussian format just as the "output.png" image attached before, but I found that the shape of the output is no more hexagonal grid and it is as if sampling is amplied or a filter is applied to the flower where it is almost the same as the flower image but in blurred format. I tried to change the jitterfactor and someFactor but this doesn't yield to the desired output. Also, it is obvious that the shape of small circles turned to be square-like shape not a circle which is also not desired. Please find below the code after adding some updates to it. You can go to a script and write the following lines to call the function below:
I=imread('flower.jpg');
GaussianHexagonalGrid(I,0.5,0.5);
Where d and s are the distance between each two circles and the size of each circle respectively.
I will be really glad if you helped in this and I already can't thank you enough for your time to help me in the previous comments but please help me in this final thing and thank you for your kindness.
function [] = GaussianHexagonalGrid(I,d,s)
%% Read input image
fontSize=12;
grayImage = rgb2gray(I);
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
%smallCircleRadius = 15; % small circle radius
bigImageWidth = size(grayImage, 2);
bigImageHeight = size(grayImage, 1);
xStart = 1;
%xSpacing = round(bigImageWidth / 10);
xSpacing = 10*d;
yStart = 1;
%ySpacing = round(bigImageHeight / 10);
ySpacing = 10*d;
x = xStart : xSpacing : bigImageWidth;
y = yStart : ySpacing : bigImageHeight;
[x, y] = meshgrid(x, y);
x = x(:);
y = y(:);
% Add a jitter to x and y by adding a random number.
jitterFraction = 1.0; % Percent of initial spacing you want
x = round(x + jitterFraction * xSpacing * rand(length(x), 1));
y = round(y + jitterFraction * ySpacing * rand(length(y), 1));
x = max(x, 1);%to make sure that x will not be below one
y = max(y, 1);%to make sure that y will not be below one
x = min(x, bigImageWidth);%to make sure that the current x will not exceed the bigImageWidth
y = min(y, bigImageHeight);%to make sure that the current y will not exceed the bigImageWidth
%% Hexagonal grid
evenRows = 2 : 2 : size(x, 1);
x(evenRows, :) = x(evenRows, :) + 16;
smallCircleRadius = 30*s;
sigma = smallCircleRadius * .4;
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
subplot(2, 2, 2);
imshow(singleCircleImage,[]);
title('Single Small Gaussian', 'FontSize', fontSize);
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
smallUpperLeftX = x(:);
smallUpperLeftY = y(:);
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : length(smallUpperLeftX)
% Find the square in the big image where we're going to add a small circle.
x1 = smallUpperLeftX(k);
y1 = smallUpperLeftY(k);
x2 = x1 + singleWidth - 1;
y2 = y1 + singleHeight - 1;
if x2 > bigImageWidth
continue;
end
if y2 > bigImageHeight
continue;
end
xMid = round(mean([x1,x2]));
yMid = round(mean([y1,y2]));
grayLevel = grayImage(yMid, xMid); % This changes depending on where in the image we are.
if grayLevel == 0
grayLevel = 1;
end
sigma = 0.02 * double(grayLevel);
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
% Normalize to 0-1.
singleCircleImage = singleCircleImage / max(singleCircleImage(:));
fprintf('Dropping Gaussian %4d at upper left = (%d, %d) with sigma = %f.\n', k, x1, y1, sigma);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Plot the upper lefts
subplot(2, 2, 3);
plot(smallUpperLeftX + singleWidth/2, smallUpperLeftY + singleHeight/2, 'r.');
grid on;
axis ij;
title('Gaussian Points', 'FontSize', fontSize);
% Display it in the lower left plot.
subplot(2, 2, 4);
imshow(manySmallCircles, []);
title('Many Small Gaussians', 'FontSize', fontSize);
end
Image Analyst
on 28 Jun 2020
To go back to a hex grid, remove the jitter. So remove these lines:
% Add a jitter to x and y by adding a random number.
jitterFraction = 0.10; % Percent of initial spacing you want
x = x + jitterFraction * xSpacing * rand(1, length(x));
y = y + jitterFraction * ySpacing * rand(1, length(y));
reham elnabawy
on 28 Jun 2020
Image Analyst:
Firstly, thank you for your kind response. Secondly, I removed the lines you told me to remove and I got back the hexagonal grid shape. However, the shape I obtained, as shown in the attached image(obtainedOutput.jpg), the flower pixels appear faded inside eachothers so how to get it not faded like the image in "desiredOutput.jpg" attached?
Image Analyst
on 30 Jun 2020
Looks like you need to make sure your sigmas occur in a tighter range. A bigger sigma will flatten out, almost like it's adding a constant to the image.
reham elnabawy
on 2 Jul 2020
Image Analyst:
ok. I got your point. I minimized the sigma value so that the blurriness is removed. However, I can see that the circle shape is dissapeared meaning that, all the circles are drawn now as squares as shown in the attached figure. So how could I reconstruct the circle shape ?
More Answers (1)
Image Analyst
on 2 Jul 2020
I don't see squares, except if you look at 45 degrees, but that's what you get when you use a hex grid. I think you should use a higher resolution image so you don't have such severe quantization artifacts.
4 Comments
reham elnabawy
on 3 Jul 2020
Edited: reham elnabawy
on 3 Jul 2020
Image Analyst: I marked some squares on the image to be more clearer to you so please see the below image. I just need the hexagonal grid to be all circles not squares please.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326279/IMG_20200703_170726.png)
Image Analyst
on 3 Jul 2020
Again, that's because you don't have enough pixel resolution. How can you expect to get a nice smooth round Gaussian when your image there is only 2 pixels by 2 pixels??? You can't. Use imresize() to blow your image up by a factor of 10 or so and try again.
reham elnabawy
on 7 Jan 2021
Image Analyst: I need your assistnace in something please. I have a certain MATLAB code that I would like to run on a certain device. I need to know the execution time for my MATLAB code on this device. Please note that I don't have the device. However, I know the hardware specs for this device that I want to run my MATLAB code on. Please note that this device is not a computer, it is a video processing unit. I would like to ask you if there is any online simulation that I could give it my code and the hardware specs as inputs and the simulation will output for me the execution time for the code if it runs on that device?
I am looking forward to your response and thank you in advance.
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)