clc,close all% 空域內的圖像增強I = imread('圖片1.png');%讀取圖像I = uint8(rgb2gray(I));subplot(121),imshow(I); %顯示原圖像subplot(122),imhist(I); %顯示其直方圖title('直方圖')
clc,close allI = imread('圖片2.jpg'); % 讀取圖像figure,subplot(221),imshow(uint8(I));title('原始圖像');subplot(222),imhist(I(:,:,1));title('R分量直方圖');subplot(223),imhist(I(:,:,2));title('G分量直方圖');subplot(224),imhist(I(:,:,3));title('B分量直方圖');
clc,close allI = imread('圖片1.png'); % 讀取圖像subplot(121),imshow(I);title('Initial Image') % 顯示原始圖像I = double(I);[row, column] = size(I(:,:,1)); % 獲取矩陣行、列{PS:若針對RGB圖像即可以用size(I(:,:,1)})J = zeros(row, column); % 構造新的矩陣for i = 1:row for j = 1:column J(i,j) = 32 * (I(i,j)<= 66)... + (200-32)/(123-66) * (I(i,j)-66) + 32 * (66 <= I(i,j) <= 123)... + 200 * (I(i,j) >= 123); endendsubplot(122),imshow(J);title('Image Greyscale Adjustment') % 顯示處理後的圖像
Non-linear Transformation
clc,close allI = imread('圖片1.png'); % 讀取圖像I = rgb2gray(I);subplot(121),imshow(I);title('Initial Image'); % 顯示原圖像I = double(I);J = 12 + 40 * (log(I) + 1); % 非線性變換J = uint8(J);subplot(122),imshow(J);title('Non-linear Tranformation') % 顯示處理後圖片
Imadjust(I,[low_in;high_in],[low_out;high_out],...)
clc,close allI = imread('圖片1.png'); % 讀取圖像I = rgb2gray(I);subplot(221),imshow(I);title('Initial Image'); % 顯示原圖像subplot(222),imhist(I);title('Original Image Histogram')J = imadjust(I,[],[0.3,0.7]);subplot(223),imshow(J);title('Final Image'); % 顯示處理後圖像subplot(224),imhist(J);title('Final Image Histogram')
clc,close allI = imread('圖片1.png'); % 讀取圖像I = rgb2gray(I);subplot(221),imshow(I);title('Initial Image'); % 顯示原圖像subplot(222),imhist(I);title('Original Image Histogram')J = imadjust(I,[],[0.3,0.7],0.4);subplot(223),imshow(J);title('Final Image 1 gamma=0.4 低灰度區擴展 高灰度區壓縮'); % 顯示處理後圖像K = imadjust(I,[],[0.3,0.7],4);subplot(224),imshow(K);title('Final Image 2 gamma=4 高灰度區擴展 低灰度區壓縮')
Histogram Equalization
histeq(I)
clc,close allI = imread('圖片1.png'); % 讀取圖像I = rgb2gray(I);subplot(221),imshow(I);title('Initial Image'); % 顯示原圖像subplot(222),imhist(I);title('Original Image Histogram')J = histeq(I);subplot(223),imshow(J);title('Final Image'); % 顯示處理後圖像subplot(224),imhist(J);title('Final Image Histogram')
clc;close all;img1 = imread('圖片1.png');img = rgb2gray(img1);img = double(img);[img2, func_T] = myHistogramEqualization(img); %顯示圖像figure('NumberTitle', 'off', 'Name', '運行結果'); subplot(231),imshow(img1);title('原始圖像'); subplot(233),imshow(img2);title('均衡化後圖像'); subplot(234),imhist(rgb2gray(img1));xlim([0 255]);title('原始圖像的直方圖'); subplot(235),plot(1:256,func_T);xlim([0 255]);ylim([0 255]);title('變換函數'); subplot(236),imhist(img2);xlim([0 255]);title('均衡化後圖像的直方圖');
function [img2, func_T] = myHistogramEqualization(img) [r,c] = size(img(:,:,1));%獲取圖像的高r和寬c %統計圖像中每個灰度級出現的次數 count = zeros(1,256); for i=1:r for j=1:c count(1,img(i,j)+1) = count(1,img(i,j)+1)+1; end end %統計圖像中每個灰度級出現的概率P矩陣 p = zeros(1,256); for i=1:256 p(1,i) = count(1,i)/(r*c); end img2 = im2uint8(ones(r,c));%創建一個r X c大小的1矩陣 % 內部矩陣元全為255 func_T = zeros(1,256);%變換函數 p_sum = 0; %求直方圖均衡化的變換函數 for k = 1:256 p_sum = p_sum + p(k);%求每個灰度級的概率之和 func_T(k) = (256-1)*p_sum;%根據變換函數的公式求和 end func_T_z = round(func_T);%對變換函數進行取整 %完成每個像素點的映射 for i = 1:256 findi = find(func_T_z==i);%找到灰度級為i的概率和 len = length(findi); for j=1:len findj = find(img==(findi(j)-1));%進行對應每個像素點的映射 img2(findj) = i; end endend
PS:不知道為什麼這樣做出來的均衡和histeq出來的結果不太一樣
還是得看看這個函數是怎麼寫的
困了,繼續
clc;close all;I = imread('圖片2.jpg');subplot(121),imshow(I);title('原始圖像');J = imnoise(I,'salt & pepper');subplot(122),imshow(J);title('加入椒鹽噪聲後的圖像')
-未完待續-
-上毛概去-