欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mtalab图像灰度变换

程序员文章站 2022-03-13 10:57:46
...
clear all;
close all;
clc;

r=127;                              
x=-r:r+1;
sigma=20;
y1=exp(-((x-80).^2)/(2*sigma^2));
y2=exp(-((x+80).^2)/(2*sigma^2));
y=y1+y2;                       %双峰高斯函数,任意函数都可以

y=y/sum(y);         %归一化,使函数符合概率分布的sum(y)==1这样一个规律
x=x+r;
figure;
bar(x,y,0.000000000000000000000000000000000000000001)
xlim([0 255])
%  plot(y);            %待匹配的直方图
% imhist(y)
G=[];               %函数的累积直方图
for i=1:256
   G=[G sum(y(1:i))]; 
end

img=imread('1.jpg');
 img=rgb2gray(img);
[m n]=size(img);
hist=imhist(img);  %待处理图像的直方图
figure;
imhist(img);
p=hist/(m*n);           
figure;bar(p,0.0000000000000000000001)
xlim([0 255])
        %原图直方图

s=[];                   %待处理图像的累积直方图
for i=1:256
    s=[s sum(p(1:i))];
end

for i=1:256
    tmp{i}=G-s(i);
    tmp{i}=abs(tmp{i});         %因为要找距离最近的点,所以取绝对值
    [a index(i)]=min(tmp{i});   %找到两个累积直方图距离最近的点
end

imgn=zeros(m,n);
for i=1:m
   for j=1:n
      imgn(i,j)=index(img(i,j)+1)-1;    %由原图的灰度通过索引映射到新的灰度
   end
end

imgn=uint8(imgn);
figure;imshow(imgn)
imwrite(imgn,'1.jpg');
% figure;plot(imhist(imgn))       %新图的直方图
hist=imhist(imgn)
p=hist/(m*n);           
figure;bar(p,0.0000000000000000000001)
xlim([0 255])



相关标签: 图形