计算机视觉(一)-matlab直方图均衡,线性变换,伽马变换

首先,matlab中直方图均衡的函数是A = histeq(img),描绘图像直方图的函数是imhist(img)
所以,描绘图像的直方图:imhist(img);
直方图均衡:H = histeq(img);
描绘均衡后的直方图:imhist(H);
显示直方图均衡后的图像:imshow(H);

灰度图像的直方图均衡

[file,path] = uigetfile({'*.png';'*.jpg'},'选择图片');%选择图片
A=imread([path,file]);
img = rgb2gray(A);%灰度处理
 
H=histeq(img);%用histeq函数均衡
figure;
subplot(221);imshow(img);title('原图');
subplot(222);imhist(img);title('原图的直方图');
subplot(223);imshow(H);title('histeq均衡后的图');
subplot(224);imhist(H);title('histeq均衡后的直方图');

结果:
在这里插入图片描述

彩色图像的直方图均衡(三个颜色通道分别处理)

img=imread([path,file]);
R = img(:,:,1);  
G = img(:,:,2);  
B = img(:,:,3);  
 
H_R = histeq(R, 256);  
H_G = histeq(G, 256);  
H_B = histeq(B, 256);  
 
 
figure,
subplot(321);imhist(R);title('R直方图');
subplot(322);imhist(H_R);title('R均衡后的直方图');
subplot(323);imhist(G);title('G直方图');
subplot(324);imhist(H_G);title('G均衡后的直方图');
subplot(325);imhist(B);title('B直方图');
subplot(326);imhist(H_B);title('B均衡后的直方图');
 
final_img=img;
final_img(:,:,1) = H_R;  
final_img(:,:,2) = H_G;  
final_img(:,:,3) = H_B;  
 
figure,
subplot(121),
imshow(img);title('原图');
subplot(122),
imshow(final_img); title('直方图均衡后的结果');

在这里插入图片描述
在这里插入图片描述

图像的线性变换和伽马变换

[file,path] = uigetfile({'*.png';'*.jpg'},'选择图片');% 选择图片
r=imread([path,file]);
r = mat2gray(r);
figure;imshow(r);
 
a= 1;
Gamma = .5;
s1 = a*(r.^Gamma);
a=1.5;b=0.2;
s2 = a*r+b;
s3=log(1+r);
 
figure;
subplot(1,2,1);
imshow(r,[0 1]);
xlabel('a) 原图');
 
subplot(1,2,2);
imshow(s1,[0 1]);
xlabel('b) 伽马变换后 (gamma = 0.5) ');
 
figure;
imshow(s2,[0 1]);
figure;
imshow(s3,[0 1]);
 
figure;
subplot(221);imshow(r,[0 1]);xlabel('a) 原图');
subplot(222);imshow(s2,[0 1]);xlabel('b) 线性变换后(a=1.5,b=0.2) ');
subplot(223);imshow(s1,[0 1]);xlabel('c) 伽马变换(gamma = 0.5) ');
subplot(224);imshow(s3,[0 1]);xlabel('d) 对数变换后');

结果:
在这里插入图片描述

reference:
李卫军,肖宛昂,董肖莉,覃鸿老师《视觉信息处理及FPGA实现》课程等

越是憧憬,越要风雨兼程。

posted @ 2019-04-08 17:07  晨青  阅读(1543)  评论(0编辑  收藏  举报