채택된 답변

Image Analyst
Image Analyst 2022년 3월 23일
편집: Image Analyst 2022년 3월 23일

2 개 추천

That's done with discretize(). I'm sure there is example code in the help like there is for all functions.

댓글 수: 4

akevg akevg
akevg akevg 2022년 3월 23일
ı have this codes its working but how can ı print side by side, like above image?
ı try but ı couldnt. can you help?
img_color = imread('peppers.png');
img_gray = rgb2gray(img_color);
figure(1); imshow(img_gray);
nLevels = 4; % change this to the desired level 2 or more
maxLevel = 256;
threshGap = maxLevel / nLevels;
thresholds = zeros(1, nLevels);
thresholds(nLevels) = maxLevel-1;
thresholds(1) = floor(threshGap);
for i = 2:nLevels-1
thresholds(i) = floor(thresholds(i-1) + threshGap);
end
grayGap = maxLevel / (nLevels - 1);
grayLevels = zeros(1, nLevels);
grayLevels(nLevels) = maxLevel - 1;
for i = 2:nLevels-1
grayLevels(i) = floor(grayLevels(i-1) + grayGap);
end
new_img = img_gray;
[rows, cols] = size(img_gray);
for i = nLevels:-1:1
%new_img(img_gray < thresholds(i)) = grayLevels(i); % you can use this line of
% code and remove the next double nested loops
for x=1:rows
for y =1:cols
if img_gray(x, y) < thresholds(i)
new_img(x, y) = grayLevels(i);
end
end
end
end
figure(2); imshow(new_img);
Wow, I didn't say to do all that. I said to use discretize:
grayImage = double(imread('cameraman.tif'));
n = [256, 32, 16, 8, 4, 2];
for k = 1 : length(n)
numLevels = n(k);
posterizedImage = discretize(grayImage, numLevels);
subplot(2, 3, k);
imshow(posterizedImage, []);
caption = sprintf('%d gray levels', numLevels);
title(caption);
end
Tilkesh
Tilkesh 2023년 1월 30일
Thanks
Tilkesh
Tilkesh 2023년 1월 30일
% One simpple example using above code.
% Define the x and y coordinate range
x = linspace(-5,5,100);
y = linspace(-5,5,100);
% Create a meshgrid of the x and y coordinates
[X,Y] = meshgrid(x,y);
% Define the grating period and duty cycle
period = 10;
duty_cycle = 0.5;
% Calculate the phase of the grating
phase = exp(i*2*pi*(X./period));
% grayImage = double(imread('cameraman.tif'));
grayImage =angle(phase);
n=1:1:8
% n=2.^nn
% n = [256, 32, 16, 8, 4, 2];
for k = 1 : length(n)
numLevels = n(k);
posterizedImage = discretize(grayImage, numLevels);
figure(1); subplot(3, 3, 1);
imagesc(angle(phase));colormap(gray);axis off;axis tight;axis square; title('Original');
subplot(3, 3, k+1);
imshow(posterizedImage, []);
caption = sprintf('%d gray levels', numLevels);
title(caption);
figure(2); subplot(3, 3, 1);
plot(angle(phase(50,:)));title('Original');
subplot(3, 3, k+1);
plot(posterizedImage(50,:))
caption = sprintf('%d gray levels', numLevels);
title(caption);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

질문:

2022년 3월 22일

댓글:

2023년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by