1. 程式人生 > >[數字影象處理]灰度變換——反轉,對數變換,伽馬變換,灰度拉伸,灰度切割,點陣圖切割

[數字影象處理]灰度變換——反轉,對數變換,伽馬變換,灰度拉伸,灰度切割,點陣圖切割

[plain] view plaincopyprint?
  1. close all;  
  2. clear all;  
  3. %% -------------Contrast Stretching-----------------  
  4. f = imread('washed_out_pollen_image.tif');  
  5. %f = imread('einstein_orig.tif');  
  6. f = mat2gray(f,[0 255]);  
  7. [M,N] = size(f);  
  8. g = zeros(M,N);  
  9. Min_f = min(min(f));  
  10. Max_f = max(max(f));  
  11. m = (Min_f + Max_f)/2;  
  12. Out_put_min = 0.05;  
  13. Out_put_max = 0.95;  
  14. E_1 = log(1/Out_put_min - 1)/log(m/(Min_f+eps));  
  15. E_2 = log(1/Out_put_max - 1)/log(m/(Max_f+eps));  
  16. E = ceil(min(E_1,E_2)-1);  
  17. g = 1 ./(1 + (m ./ (f+ eps)).^E);  
  18. g = mat2gray(g,[1/(1+(m/eps)^E) 1/(1+(m/1+eps)^E)]);  
  19. figure();  
  20. subplot(2,2,1);  
  21. imshow(f,[0 1]);  
  22. xlabel('a).Original Image');  
  23. subplot(2,2,2);  
  24. r = imhist(f)/(M*N);  
  25. bar(0:1/255:1,r);  
  26. axis([0 1 0 max(r)]);  
  27. xlabel('b).The Histogram of a');  
  28. ylabel('Number of pixels');  
  29. subplot(2,2,3);  
  30. imshow(g,[0 1]);  
  31. xlabel('c).Results of Contrast stretching');  
  32. subplot(2,2,4);  
  33. s = imhist(g)/(M*N);  
  34. bar(0:1/255:1,s);  
  35. axis([0 1 0 max(s)]);  
  36. xlabel('b).The Histogram of a');  
  37. ylabel('Number of pixels');  
  38. in_put = 0:1/255:1;  
  39. Out_put1 = 1 ./(1 + (m ./ (double(in_put)+ eps)).^E);  
  40. Out_put1 = mat2gray(Out_put1,[1/(1+(m/eps)^E) 1/(1+(m/1+eps)^E)]);  
  41. figure();  
  42. plot(in_put,Out_put1);  
  43. axis([0,1,0,1]),grid;  
  44. axis square;  
  45. xlabel('Input intensity level');  
  46. ylabel('Onput intensity level');  
close all;
clear all;

%% -------------Contrast Stretching-----------------
f = imread('washed_out_pollen_image.tif');
%f = imread('einstein_orig.tif');
f = mat2gray(f,[0 255]);

[M,N] = size(f);
g = zeros(M,N);

Min_f = min(min(f));
Max_f = max(max(f));
m = (Min_f + Max_f)/2;

Out_put_min = 0.05;
Out_put_max = 0.95;

E_1 = log(1/Out_put_min - 1)/log(m/(Min_f+eps));
E_2 = log(1/Out_put_max - 1)/log(m/(Max_f+eps));
E = ceil(min(E_1,E_2)-1);

g = 1 ./(1 + (m ./ (f+ eps)).^E);
g = mat2gray(g,[1/(1+(m/eps)^E) 1/(1+(m/1+eps)^E)]);

figure();
subplot(2,2,1);
imshow(f,[0 1]);
xlabel('a).Original Image');

subplot(2,2,2);
r = imhist(f)/(M*N);
bar(0:1/255:1,r);
axis([0 1 0 max(r)]);
xlabel('b).The Histogram of a');
ylabel('Number of pixels');

subplot(2,2,3);
imshow(g,[0 1]);
xlabel('c).Results of Contrast stretching');

subplot(2,2,4);
s = imhist(g)/(M*N);
bar(0:1/255:1,s);
axis([0 1 0 max(s)]);
xlabel('b).The Histogram of a');
ylabel('Number of pixels');

in_put = 0:1/255:1;
Out_put1 = 1 ./(1 + (m ./ (double(in_put)+ eps)).^E);
Out_put1 = mat2gray(Out_put1,[1/(1+(m/eps)^E) 1/(1+(m/1+eps)^E)]);

figure();
plot(in_put,Out_put1);
axis([0,1,0,1]),grid;
axis square;
xlabel('Input intensity level');
ylabel('Onput intensity level');