How can I deform the boundary of a 2D shape with two layers in order to achieve a smooth transition between pixels, and generate different shapes in a randomized process?

6 visualizzazioni (ultimi 30 giorni)
I want to generate a 2D shape with two layers. To accomplish this, I have created a 2D matrix.
img = 10 * ones(100,100);
img(1:10,:) = 5;
Currently, the boundary of the layers is a straight line, but I intend to deform the boundary to achieve a shape for example similar to the one shown in the picture.
My current approach involves generating random integer numbers to select specific pixels and then using a loop to modify the values of those pixels. However, this approach presents two challenges.
Firstly, the random numbers can lead to significant fluctuations in the value changes between adjacent pixels. I aim to achieve a smooth transition instead.
Secondly, I intend to repeat this process 1000 times, each time obtaining a different shape. I am trying to figure out how to introduce randomness into this process.
Any guidance on addressing these issues would be greatly appreciated.

Risposte (1)

Udit06
Udit06 il 23 Ago 2023
Hi,
You can approach the problem as follows:
Step 1: Create a line using sum of m sine and n cosine functions. An example using one sine function and one cosine function is shown below.
% Define image size
width = 1000;
height = 1000;
% Create a white background image
image = ones(height, width, 3);
% Define the height of the superposition from the bottom
h = 200;
% Generate x-coordinates for the image
x = 1:width;
% Calculate the y-coordinates for the sine and cosine functions
scale = 300; % Scaling factor for the functions
y_sine = h + scale * sin(x / scale); % Scaled sine function
y_cosine = h + scale * cos(x / scale); % Scaled cosine function
y= y_sine + 0.6*y_cosine;
% Set the color of the superposition to black
color = [0, 0, 0];
% Draw the superposition on the image
for i = 1:width
image(round(y(i)), i, :) = color;
end
% Display the image
imshow(image);
Step 2: Now color each pixel according to which region (upper or bottom) they belong to.
% Define image size
width = 1000;
height = 1000;
% Create a white background image
image = ones(height, width, 3);
% Define the height of the superposition from the bottom
h = 200;
% Generate x-coordinates for the image
x = 1:width;
% Calculate the y-coordinates for the sine and cosine functions
scale = 300; % Scaling factor for the functions
y_sine = h + scale * sin(x / scale); % Scaled sine function
y_cosine = h + scale * cos(x / scale); % Scaled cosine function
y = y_sine + 0.6 * y_cosine;
% Set the color of the superposition to black
color_above = [0, 0, 1]; % Blue color for above the line
color_below = [1, 0, 0]; % Red color for below the line
% Draw the superposition on the image
for i = 1:width
for j = 1:height
if j <= round(y(i))
image(j, i, :) = color_below; % Set color below the line
else
image(j, i, :) = color_above; % Set color above the line
end
end
end
% Display the image
imshow(image);
To introduce randomness, you can tweak the following:
  1. Number of sine and cosine function
  2. Variable h in the above code to adjust the height from top and bottom.
  3. The frequency of sine and cosine functions
  4. Scaling factor
I hope this helps.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by