How to make binary to gray scale

3 Ansichten (letzte 30 Tage)
mohd akmal masud
mohd akmal masud am 20 Mai 2024
Dear All,
I created the image. My coding as below.
Z = zeros(99); % create square matrix of zeroes
origin = [round((size(Z,2)-1)/2+1) round((size(Z,1)-1)/2+1)]; % "center" of the matrix
radius = round(sqrt(numel(Z)/(2*pi))); % radius for a circle that fills half the area of the matrix
[xx,yy] = meshgrid((1:size(Z,2))-origin(1),(1:size(Z,1))-origin(2)); % create x and y grid
Z(sqrt(xx.^2 + yy.^2) <= radius) = 1; % set points inside the radius equal to one
imshow(Z); % show the "image"
NOW my pixel number just 1 and 0 (binary).
Do you know how to make the pixel number is gray scale. I mean is the gradient pixel number from center to edge is decending?

Akzeptierte Antwort

Abhishek Kumar Singh
Abhishek Kumar Singh am 20 Mai 2024
Hi,
To create a grayscale gradient from the center to the edge of your circle, you'll adjust the pixel values based on their distance from the center. Instead of setting the inside of the circle to a uniform value (like 1 for inside and 0 for outside), you'll calculate a gradient value that decreases from the center to the edge.
You can refer to the following code snippet to achieve a grayscale gradient effect:
Z = zeros(99); % create square matrix of zeroes
origin = [round((size(Z,2)-1)/2+1), round((size(Z,1)-1)/2+1)]; % "center" of the matrix
radius = round(sqrt(numel(Z)/(2*pi))); % radius for a circle that fills half the area of the matrix
[xx,yy] = meshgrid((1:size(Z,2))-origin(1),(1:size(Z,1))-origin(2)); % create x and y grid
distanceFromCenter = sqrt(xx.^2 + yy.^2); % calculate distance from center for each point
% Instead of setting inside to 1, calculate gradient value
for i = 1:size(Z,1)
for j = 1:size(Z,2)
if distanceFromCenter(i,j) <= radius
Z(i, j) = 1 - (distanceFromCenter(i,j) / radius); % normalize and invert to get gradient
end
end
end
imshow(Z);
In above code, each pixel's value inside the circle is calculated based on its distance from the center, normalized by the radius. This means pixels at the center of the circle will have a value close to 1 (lighter) and decrease linearly to 0 (darker) as you move towards the edge of the circle, creating a nice gradient effect.
Hope it helps!

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by