Random black and white pixels forming clusters, input proportion of white

4 views (last 30 days)
Hello,
My knowledge of Matlab is very (very) limited but I am trying to find a code for the following: let's say on a 10*10 image, I would like to be able to enter the porportion of black and white pixels, and generate random images (for instance, generate many random images that are 50% black). The thing is it shouldn't only be pixels, but these should form clusters. One idea would be to define a few random points on the image (five on the example below, at random positions of the 10*10 image). These points would be black and the rest of the image white, so to start. Then around the points, progressively decrease the probability of having a black pixel (this is what I tried to represent with circles around the first point). In the end, it would form clusters and we could give the proportion of white (or black) pixels as an input (e.g. generate random images 50% black). Any help for finding such a code is more than welcome.
I hope this makes sense, please don't hesitate to ask for clarifications. Many thanks in advance for your help.

Answers (2)

harjeet singh
harjeet singh on 27 Dec 2015
hello lucy do use this code
clear all
close all
warning off
clc
initial_dots=5;
a=500;
img(1:a,1:a)=1;
img=logical(img);
figure(1)
imshow(img)
drawnow
pos=randint(initial_dots,2,[1 a]);
for i=1:length(pos)
img(pos(i,1),pos(i,2))=0;
end
figure(2)
imshow(img)
drawnow
for i=1:5
for k=1:20
theta=0:k:360;
r=round(pos(i,1) + k*sin(theta));
c=round(pos(i,2) + k*cos(theta));
for j=1:length(r)
if(r(j)>0 && r(j)<a && c(j)>0 && c(j)<a)
img(r(j),c(j))=0;
end
end
end
end
figure(3)
imshow(img)
drawnow
  1 Comment
Image Analyst
Image Analyst on 27 Dec 2015
Good start. What toolbox is randint() in? Can you make it work with the built-in randi() instead? Can you make it work with randomness so that you are not just plopping down a point with 100% certainty? How are you keeping track of the number of points drawn so as to achieve the desired % blackness?

Sign in to comment.


Lucy G.
Lucy G. on 27 Dec 2015
Hi Harjeet, Thank you a lot for your answer. Pardon my ignorance of Matlab, but I am not sure this would give me what I am trying to have. In the end, I hope to have a result similar to the following:
Sorry if I am not very clear. Many thanks in advance for your help.

Community Treasure Hunt

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

Start Hunting!