how to create gray scale gradient image?

12 次查看(过去 30 天)
how to create gray scale image like the attached file.

回答(2 个)

DGM
DGM 2022-10-12
编辑:DGM 2022-10-12
Observe that the image profile is not piecewise-linear, but rather it's a cosine function.
% observe that the profile is not PWL
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/93802/image.png');
A = imcrop(A,[54 22 88 405]);
A = rgb2gray(A);
A = mean(A,2);
plot(A)
So one way is to simply generate the image using basic tools:
% generate a gradient image (strictly grayscale)
outsize = [200 200]; % [height width]
y = linspace(0,2*pi,outsize(1))';
y = (cos(y)+1)/2;
outpict = repmat(im2uint8(y),[1 outsize(2)]);
imshow(outpict)

Chunru
Chunru 2022-10-12
a = (-5:0.1:5)';
b = exp(-a.^2/2);
x = repmat(b, [1 512]);
imagesc(x); colormap(flip(gray(512)))
  2 个评论
DGM
DGM 2022-10-12
I suppose you're right. It's not like the OP is the one who needs the answer, so other ease curves/profiles could be considered. For perspective:
% the profile from the source image
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/93802/image.png');
A = imcrop(A,[54 18 88 413]); % crop a little wider to preserve the shape
A = rgb2gray(A);
A = mean(A,2);
profile_orig = mat2gray(A);
profile_orig([1:4 412:414]) = 1; % manually eliminate artifacts included in the wider crop region
sizey = numel(profile_orig);
% generate a gradient vector using a cosine curve
y = linspace(0,2*pi,sizey)';
profile_cos = (cos(y)+1)/2;
% generate a gradient vector using a PWL curve
y = linspace(-1,1,sizey)';
profile_pwl = abs(y);
% generate a gradient vector using a gaussian curve
y = linspace(-5,5,sizey)';
profile_gauss = 1-exp(-y.^2/2);
plot(profile_orig); hold on
plot(profile_cos)
plot(profile_pwl)
plot(profile_gauss)
legend({'original','cosine','pwl','gaussian'},'location','southeast')
Note that the original profile is cropped from a lossy-compressed image and some attempt was made to deal with the artifacts. Some inaccuracy should be expected due to that.
A visual comparison may help show the differences/similarities in the profiles.
% show samples of each side-by-side for comparison
clf
gradientsamples = [profile_orig profile_cos profile_pwl profile_gauss];
gradientsample = imresize(gradientsamples,[sizey 600],'nearest');
imshow(gradientsample)
Chunru
Chunru 2022-10-12
@DGM many of the window function could be used.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by