用matlab做图像的灰度变换,指令I=rgb2gray(A)执行的时候总是报错,用的是matlab2009a

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用matlab做图像的灰度变换,指令I=rgb2gray(A)执行的时候总是报错,用的是matlab2009a相关的知识,希望对你有一定的参考价值。

程序是A=imread('D:\Cameraman.bmp');
I=rgb2gray(A);
subplot(2,2,1);
imshow(I);
title('原图');
subplot(2,2,2);
imhist(I);
J=imadjust(I,[],[0.3 0.7],1);
subplot(2,2,3);
imshow(J);
subplot(2,2,4);imhist(J);
报错??? Error using ==> rgb2gray>parse_inputs at 82
MAP must be a m x 3 array.

Error in ==> rgb2gray at 35
X = parse_inputs(varargin:);

Error in ==> xu1 at 2
I=rgb2gray(A);

这个问题我刚好也遇到过,不一定是楼上说的这种情况,比如说把matlab画的一条曲线以bmp格式保存下来,然后imread之后A也是二维的。
我的理解是这样的,如果图像中颜色种类较少,bmp读取出来会把颜色以索引形式存下来(而不是RGB分量形式)。使用 [A, map] = imread(...),A不同位置的数值在map里对应的颜色分量才是该位置的真实颜色。
对于这样的A,做灰度变换只要对map做变换就行了,如matlab里的例子:
[X,map] = imread('trees.tif');
gmap = rgb2gray(map);
figure, imshow(X,map), figure, imshow(X,gmap);
参考技术A 程序是没有问题的,问题出在你的图片'D:\Cameraman.bmp',rgb2gray(X)的意思是把一幅由R G B 三个通道表征的图像 通过一个比例公式转换为 仅有一个通道的图像 即灰度图像,由出错的提示“??? Error using ==> rgb2gray>parse_inputs at 82
MAP must be a m x 3 array.
”显然你读入的图像并不是R G B 三通道标准的图像
大概你用的是个灰度图片,可以不用调用rgb2gray()这个函数,直接进行后续处理。

数字图像处理:图像的灰度变换(Matlab实现)

(1)线性变换:
通过建立灰度映射来调整源图像的灰度。

k>1增强图像的对比度;k=1调节图像亮度,通过改变d值达到调节亮度目的;0

i = imread(‘theatre.jpg‘);
i = im2double(rgb2gray(i));
[m,n]=size(i);
%增加对比度
Fa = 1.25; Fb = 0;
O = Fa.*i + Fb/255;
figure(1), subplot(221), imshow(O);
title(‘Fa = 1.25, Fb = 0, contrast increasing‘);
figure(2),subplot(221), [H,x]=imhist(O, 64);
stem(x, (H/m/n), ‘.‘);
title(‘Fa = 1.25, Fb = 0, contrast increasing‘);
%减小对比度
Fa =0.5; Fb = 0;
O = Fa.*i + Fb/255;
figure(1), subplot(222),imshow(O);
title(‘Fa = 0.5, Fb = 0, contrast decreasing‘);
figure(2), subplot(222), [H,x] = imhist(O, 64);
stem(x, (H/m/n), ‘.‘);
title(‘Fa = 0.5, Fb = 0, contrast decreasing‘);
%线性亮度增加
Fa = 0.5; Fb = 50;
O = Fa.*i + Fb/255;
figure(1), subplot(223), imshow(O);
title(‘Fa = 0.5, Fb = 50, brightness control‘);
figure(2), subplot(223), [H,x]=imhist(O,64);
stem(x, (H/m/n), ‘.‘);
title(‘Fa = 0.5, Fb = 50, brightness control‘);
%反相显示
Fa = -1; Fb = 255;
O = Fa.*i + Fb/255;
figure(1), subplot(224), imshow(O);
title(‘Fa = -1, Fb = 255, reversal processing‘);
figure(2), subplot(224),[H,x]=imhist(O, 64);
stem(x, (H/m/n), ‘.‘);
title(‘Fa = -1, Fb = 255, reversal processing‘);

(2)对数变换:
增强低灰度,减弱高灰度值。

i = imread(‘theatre.jpg‘);


i = rgb2gray(i);
i = double(i);

out1 = log(1+i)/0.065;
out2 = log(1+i)/0.035;
out1(find(out1>255)) = 255;
out2(find(out2>255)) = 255;
out1 = uint8(out1);
out2 = uint8(out2);

(3)幂次变换:
次数小于1时,增强低灰度,减弱高灰度;次数大于1时增强高灰度,减弱低灰度。

i = rgb2gray(imread(‘theatre.jpg‘));
i = double(i);
y1 = 255*(i/255).^2.5;
y2 = 255*(i/255).^0.4;
y1 = uint8(y1);
y2 = uint8(y2);

(4) 指数变换:
增强高灰度,减弱低灰度。

i = imread(‘theatre.jpg‘);
i = rgb2gray(i);
i = double(i);

y1 = 1.5.^(i*0.070)-1;
y2 = 1.5.^(i*0.050)-1;
y1(find(y1>255)) = 255;
y2(find(y2>255)) = 255;
y1 = uint8(y1);
y2 = uint8(y2);

(5)灰度拉伸:
有时图像灰度集中在某小块区域,需要改变图像对比度。

i = imread(‘theatre.jpg‘);
i = rgb2gray(i);
L = imadjust(i,[ ],[50/255;150/255]);
J = imadjust(L,[50/255;150/255 ],[20/255;230/255]);

(6)灰度均衡:
i = rgb2gray(imread(‘theatre.jpg‘));
LC = imadjust(i,[ ],[50/255;150/255]);
HE1 = histeq(LC);%均衡函数

(7)直方图规定化:
实现局部的灰度均衡。

img = rgb2gray(imread(‘theatre.jpg‘));
img_ref = rgb2gray(imread(‘rpic.jpg‘));%参考图,按照这个的的直方图进行规定化
[hgram, x] = imhist(img_ref);
J = histeq(img, hgram);

以上是关于用matlab做图像的灰度变换,指令I=rgb2gray(A)执行的时候总是报错,用的是matlab2009a的主要内容,如果未能解决你的问题,请参考以下文章

matlab 灰度变换函数

应用霍夫变换方法,用matlab语言编写相应的程序

数字图像处理:图像的灰度变换(Matlab实现)

matlab图像灰度调整——imadjust函数的使用

MATLAB如何切割图像

如何用matlab做图片的灰度直方图