Effacer les filtres
Effacer les filtres

how to create gray scale gradient image?

12 vues (au cours des 30 derniers jours)
Mubashir Ali
Mubashir Ali le 8 Nov 2017
Commenté : Chunru le 12 Oct 2022
how to create gray scale image like the attached file.

Réponses (2)

DGM
DGM le 12 Oct 2022
Modifié(e) : DGM le 12 Oct 2022
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 le 12 Oct 2022
a = (-5:0.1:5)';
b = exp(-a.^2/2);
x = repmat(b, [1 512]);
imagesc(x); colormap(flip(gray(512)))
  2 commentaires
DGM
DGM le 12 Oct 2022
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 le 12 Oct 2022
@DGM many of the window function could be used.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by