필터 지우기
필터 지우기

How to make binary to gray scale

조회 수: 4 (최근 30일)
mohd akmal masud
mohd akmal masud 2024년 5월 20일
답변: Abhishek Kumar Singh 2024년 5월 20일
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?

채택된 답변

Abhishek Kumar Singh
Abhishek Kumar Singh 2024년 5월 20일
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!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by