在Matlab中将图像转换为灰度和十进制范数

问题描述 投票:0回答:1

我想将图像从其原始RGB比例转换为灰度和十粒瘤。但是,我尝试的每一次尝试都会给我一些较暗的图像,但并不能真正显示出灰度。这是我正在运行的内容

My attempt  for grayscale

至于申命记,我不知道该怎么做。任何帮助,将不胜感激。

matlab colors rgb photo image-manipulation
1个回答
0
投票

您的错误是缩放whe = imread('CoWheel.png'); whesi = size(whe); red = []; gree = []; blue = []; for i = 1:whesi(1) for j = 1:whesi(2) red(i,j) = whe(i,j,1); gree(i,j) = whe(i,j,2); blue(i,j) = whe(i,j,3); end end gray = whe; for i = 1:whesi(1) for j = 1:whesi(2) gray(i,j,1) = gray(i,j,1)*.2989; gray(i,j,2) = gray(i,j,2)*.5870; gray(i,j,3) = gray(i,j,3)*.1141; end gray(i,j,1)gray(i,j,2)。您需要缩放和求和RGB元素:gray(i,j,3)

这里是更正的代码示例:

gray = R*.2989 + G*.5870 + B*.1141

结果:whe = imread('CoWheel.png'); figure;imshow(whe); whesi = size(whe); %You don'y use red, green and blue in your conversion code... % red = []; % gree = []; % blue = []; % % for i = 1:whesi(1) % for j = 1:whesi(2) % red(i,j) = whe(i,j,1); % gree(i,j) = whe(i,j,2); % blue(i,j) = whe(i,j,3); % end % end %Convert from uint8 to double (for performing the computations in double - not in uint8 integers). whe = double(whe); %gray = whe; %The Grayscale image has only one color channel (not 3 like RGB) gray = zeros(whesi(1), whesi(2)); for i = 1:whesi(1) for j = 1:whesi(2) gray(i,j) = whe(i,j,1)*.2989 + whe(i,j,2)*.5870 + whe(i,j,2)*.1141; end end %Convert from double to uint8 gray = uint8(gray); figure;imshow(gray)


您也可以使用enter image description here

rgb2gray

rgb2gray不是感知准确的。如果需要准确的结果,则需要反转伽玛,计算Y(XYZ颜色空间的Y,然后执行伽玛)。

%Using MATLAB rgb2gray built in function:
whe = imread('CoWheel.png');
gray = rgb2gray(whe);

结果:%Shortcut: convert RGB to LAB, zero A and B, and convert to LAB to RGB RGB = whe; LAB = rgb2lab(RGB); %Convert RGB to LAB LAB(:,:,2) = 0; %Zero A channel LAB(:,:,3) = 0; %Zero B channel gray = lab2rgb(LAB); %The result is Grayscale image in RGB format (where R=G=B). gray = im2uint8(gray); %Convert from range [0, 1] to uint8 [0, 255]. figure;imshow(gray)

这里是RGB图像,第一个灰度结果和可感知的准确结果:enter image description here

关于申命记,它需要更多的研究...

© www.soinside.com 2019 - 2024. All rights reserved.