我想要将图像从原始的RGB级别转换为灰度和deuteranomaly。但是,我每次尝试的时候都会得到一些不能真正显示灰度的较暗的图像。
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至于deuteranomaly,我不知道该怎么做。任何帮助都将不胜感激。
发布于 2019-11-24 05:28:44
你的错误是扩展gray(i,j,1),gray(i,j,2),gray(i,j,3)。
您需要缩放和求和RGB元素: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)结果:

您还可以使用rgb2gray:
%Using MATLAB rgb2gray built in function:
whe = imread('CoWheel.png');
gray = rgb2gray(whe);rgb2gray并不是感知上的准确。
如果你想要一个准确的结果,你需要反转gamma,计算Y( XYZ颜色空间,并应用gamma)。
快捷方式:将RGB转换为LAB,将A和B置零,然后将LAB转换为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图像,第一个灰度结果和感知上的准确结果:

至于Deuteranomaly,还需要更多的研究。
下面的示例可能是错误的,但我认为它的方向是正确的:
根据Wikipedia的说法:
异常三色是一种常见的遗传性色觉缺陷,当三种锥体色素中的一种改变其光谱敏感性时就会发生。原感光是一种轻微的色觉缺陷,红色视网膜感受器的光谱敏感性改变(更接近绿色感受器的反应)导致红绿色调辨别能力差。它是遗传的,与性别相关,在1%的男性中存在。
与其他缺陷相比,在这种情况下,L-锥体存在但功能不全,而在屈光不正的情况下,L-锥体完全缺失。由绿色视网膜感受器的类似变化引起的Deuteranomaly,是迄今为止最常见的色觉缺陷类型,对5%的欧洲男性的红绿色调辨别能力有轻微影响。它是遗传的,并与性别有关。与后视症不同的是,绿色敏感的视锥细胞并没有丢失,而是发生了故障。
我尝试了通过降低M锥体(绿色)响应的强度水平来模拟Deuteranomaly。
我从文件交换网站下载了Colorspace Transformations。
我使用colorspace将RGB转换为LMS颜色空间,以及从LMS转换为RGB。
下面是一个代码示例:
RGB = imread('CoWheel.png');
RGB = im2double(RGB); %Convert from uint8 [0,255] to double range [0, 1].
%Convert from RGB to LMS color space
LMS = colorspace('CAT02 LMS<-RGB', RGB);
%Reduce the M Cone intensity by half
LMS(:, :, 2) = LMS(:, :, 2)*0.5;
%Convert from LMS to RGB color space
deuteranomalyRGB = colorspace('RGB<-CAT02 LMS', LMS);
figure;imshow(deuteranomalyRGB)结果:

https://stackoverflow.com/questions/59009703
复制相似问题