影象質量客觀評價指標

在做紅外影象細節增強演算法研究時,很重要一點就是要對經過演算法處理的影象結果進行評價,分成兩種評價方法。一種是視覺效果評價:主觀的人眼觀察,主要是通過觀察者能否看到更多影象細節,給人的感覺是否真實自然,與其他演算法處理的影象的亮度和對比度的比較。在論文中的呈現方式,就是把各個演算法的處理結果和原圖一起呈現給觀察者,再對影象某一具體區域進行詳細分析,如影象中的文字,黑暗區域,欄杆樹木和建築物等細節資訊進行比較,看那種演算法的結果更清晰,暴露出更多的細節。當然咯,主觀評價方式往往取決於觀察者,並不是一種客觀可靠的方式,往往需要幾種公認的影象質量客觀評價指標,對紅外影象細節增強演算法的處理結果進行比較。通過閱讀大量的SCI文獻,如IEEE Transactions on Image Processing,Optic Engineering, Applied Optics, Optik, Infrared Physics & Technology等多種權威期刊的影象處理論文精讀,本文選取了四種經典的客觀評價指標:RMSC, SSIM, FSIM, MOS

一  RMSC

The Root-Mean-Square Contrast (RMSC) index,即均方根對比度,用來衡量影象的對比度,其值越大對比度越高。其中, 為影象總畫素的均值。I為影象某點的畫素。M和N分別為影象的高和寬。

The  MATLAB code of RMSC is following:

  1. close all
  2. clear
  3. clc;
  4. load(‘S11.mat’);
  5. I = double(S11);
  6. avg=mean2(I); %求影象均值
  7. [m,n]=size(I);
  8. s=0;
  9. for x=1:m
  10. for y=1:n
  11. s=s+(I(x,y)-avg)^2; %求得所有畫素與均值的平方和。
  12. end
  13. end
  14. %故RMSCMATLAB程式碼如下:
  15. RMSC=sqrt(s/(m*n)); %利用方差公式求得. sqrt返回陣列 X 的每個元素的平方根

MATLAB Code

二  SSIM

The Structural SIMilarity (SSIM) index between two images, 影象結構相似性,是一種衡量兩幅影象相似度的指標。用來判斷圖片壓縮後的質量。由於這個影象指標很流行,百度搜索一大堆,而且MATLAB有函式可以呼叫,故不再詳細介紹。接下來,只是給出我平時用SSIM評價紅外影象質量的MATLAB程式碼:

How to use RMSC  in MATLAB:

  1. clc;
  2. close all;
  3. clear;
  4. % A=imread('GABF1_dde_S4.jpg');
  5. % ref=imread('S4.tif');
  6. load('S1.mat');
  7. load('S1_GABF.mat');
  8. A= im2double(OUT);
  9. ref = im2double(S);%S1-S4.mat
  10. %ref = im2double(S11);%S11.mat
  11. %調整影象大小有三種方法
  12. %分別是近鄰內插法、雙線性內插法和雙立方內插法
  13. %使用的函式是imresize(src,size,method)
  14. %A=imresize(A,[254,324],'nearest'); %近鄰內插法,調整影象大小為254×324
  15. %調整影象的維度
  16. %ref=reshape(ref,254,324,3);
  17. % ref1=ref(:,:,1);
  18. %
  19. % ref2=ref(:,:,2);
  20. % ref3=ref(:,:,3);
  21. % ref_a=zeros(254,324,3);%先申請變數空間;
  22. % ref_a(:,:,1)=ref1;
  23. % ref_a(:,:,2)=ref2;
  24. % ref_a(:,:,3)=ref3;
  25. % %ref=horzcat(ref1,ref2,ref3);
  26. %
  27. % A=imresize(A,[256,324],'bilinear'); %雙線性內插法,調整影象大小為256×324
  28. % %A=imresize(A,4,'bicubic'); %雙立方內插法,放大4
  29. % %ref ima
  30. % ref_a=imresize(ref_a,[256,324],'bilinear'); %雙線性內插法,調整影象大小為256×324
  31. % ref=im2uint8(ref_a);
  32. % ref=ref(:,:,3);
  33. % A=A(:,:,3);
  34. mval = ssim(A,ref)

The use of SSIM Code

上面的MATLAB程式碼,註釋為以前的編寫的,輸入影象和參考影象以tif和bmp格式。沒註釋則為今天編寫的,為mat格式,更加準確,一般經典的影象增強演算法,輸入影象都是mat格式。即把結果儲存為mat格式,用save,直接在命令視窗輸入help save檢視儲存為mat格式的具體用法。論文的演算法,輸入也是mat格式。

三  FSIM

feature similarity index mersure(FSIM),是SSIM的變種,該演算法認為一張圖片中的所有畫素並非具有相同的重要性,比如物體邊緣的畫素點對於界定物體的結構肯定比其他背景區域的畫素點更為重要;另外一種重要的評價指標VIF儘管在不同的子帶上具有不同的權重,但是在具體的某一子帶上參與計算的畫素點均具有相同的權重;根據影象本身的特性,這樣不加區分並不合適。因此改進的方向實際上重在如何區分這些重要點並給與合適的權重。在GitHub上可以搜到程式碼,要想進一步理解,可以去搜作者的論文。

在MATLAB軟體裡有函式可以呼叫,直接help FSIM, 即[FSIM, FSIMc] = FeatureSIM(img1, img2);

  1. function [FSIM, FSIMc] = FSIM(imageRef, imageDis)
  2. % ========================================================================
  3. % FSIM Index with automatic downsampling, Version 1.0
  4. % Copyright(c) 2010 Lin ZHANG, Lei Zhang, Xuanqin Mou and David Zhang
  5. % All Rights Reserved.
  6. %
  7. % ----------------------------------------------------------------------
  8. % Permission to use, copy, or modify this software and its documentation
  9. % for educational and research purposes only and without fee is here
  10. % granted, provided that this copyright notice and the original authors'
  11. % names appear on all copies and supporting documentation. This program
  12. % shall not be used, rewritten, or adapted as the basis of a commercial
  13. % software or hardware product without first obtaining permission of the
  14. % authors. The authors make no representations about the suitability of
  15. % this software for any purpose. It is provided "as is" without express
  16. % or implied warranty.
  17. %----------------------------------------------------------------------
  18. %
  19. % This is an implementation of the algorithm for calculating the
  20. % Feature SIMilarity (FSIM) index between two images.
  21. %
  22. % Please refer to the following paper
  23. %
  24. % Lin Zhang, Lei Zhang, Xuanqin Mou, and David Zhang,"FSIM: a feature similarity
  25. % index for image qualtiy assessment", IEEE Transactions on Image Processing, vol. 20, no. 8, pp. 2378-2386, 2011.
  26. %
  27. %----------------------------------------------------------------------
  28. %
  29. %Input : (1) imageRef: the first image being compared
  30. % (2) imageDis: the second image being compared
  31. %
  32. %Output: (1) FSIM: is the similarty score calculated using FSIM algorithm. FSIM
  33. % only considers the luminance component of images. For colorful images,
  34. % they will be converted to the grayscale at first.
  35. % (2) FSIMc: is the similarity score calculated using FSIMc algorithm. FSIMc
  36. % considers both the grayscale and the color information.
  37. %Note: For grayscale images, the returned FSIM and FSIMc are the same.
  38. %
  39. %-----------------------------------------------------------------------
  40. %
  41. %Usage:
  42. %Given 2 test images img1 and img2. For gray-scale images, their dynamic range should be 0-255.
  43. %For colorful images, the dynamic range of each color channel should be 0-255.
  44. %
  45. %[FSIM, FSIMc] = FeatureSIM(img1, img2);
  46. %-----------------------------------------------------------------------
  47.  
  48. [rows, cols] = size(imageRef(:,:,1));
  49. I1 = ones(rows, cols);
  50. I2 = ones(rows, cols);
  51. Q1 = ones(rows, cols);
  52. Q2 = ones(rows, cols);
  53.  
  54. if ndims(imageRef) == 3 %images are colorful
  55. Y1 = 0.299 * double(imageRef(:,:,1)) + 0.587 * double(imageRef(:,:,2)) + 0.114 * double(imageRef(:,:,3));
  56. Y2 = 0.299 * double(imageDis(:,:,1)) + 0.587 * double(imageDis(:,:,2)) + 0.114 * double(imageDis(:,:,3));
  57. I1 = 0.596 * double(imageRef(:,:,1)) - 0.274 * double(imageRef(:,:,2)) - 0.322 * double(imageRef(:,:,3));
  58. I2 = 0.596 * double(imageDis(:,:,1)) - 0.274 * double(imageDis(:,:,2)) - 0.322 * double(imageDis(:,:,3));
  59. Q1 = 0.211 * double(imageRef(:,:,1)) - 0.523 * double(imageRef(:,:,2)) + 0.312 * double(imageRef(:,:,3));
  60. Q2 = 0.211 * double(imageDis(:,:,1)) - 0.523 * double(imageDis(:,:,2)) + 0.312 * double(imageDis(:,:,3));
  61. else %images are grayscale
  62. Y1 = imageRef;
  63. Y2 = imageDis;
  64. end
  65.  
  66. Y1 = double(Y1);
  67. Y2 = double(Y2);
  68. %%%%%%%%%%%%%%%%%%%%%%%%%
  69. % Downsample the image
  70. %%%%%%%%%%%%%%%%%%%%%%%%%
  71. minDimension = min(rows,cols);
  72. F = max(1,round(minDimension / 256));
  73. aveKernel = fspecial('average',F);
  74.  
  75. aveI1 = conv2(I1, aveKernel,'same');
  76. aveI2 = conv2(I2, aveKernel,'same');
  77. I1 = aveI1(1:F:rows,1:F:cols);
  78. I2 = aveI2(1:F:rows,1:F:cols);
  79.  
  80. aveQ1 = conv2(Q1, aveKernel,'same');
  81. aveQ2 = conv2(Q2, aveKernel,'same');
  82. Q1 = aveQ1(1:F:rows,1:F:cols);
  83. Q2 = aveQ2(1:F:rows,1:F:cols);
  84.  
  85. aveY1 = conv2(Y1, aveKernel,'same');
  86. aveY2 = conv2(Y2, aveKernel,'same');
  87. Y1 = aveY1(1:F:rows,1:F:cols);
  88. Y2 = aveY2(1:F:rows,1:F:cols);
  89.  
  90. %%%%%%%%%%%%%%%%%%%%%%%%%
  91. % Calculate the phase congruency maps
  92. %%%%%%%%%%%%%%%%%%%%%%%%%
  93. PC1 = phasecong2(Y1);
  94. PC2 = phasecong2(Y2);
  95.  
  96. %%%%%%%%%%%%%%%%%%%%%%%%%
  97. % Calculate the gradient map
  98. %%%%%%%%%%%%%%%%%%%%%%%%%
  99. dx = [3 0 -3; 10 0 -10; 3 0 -3]/16;
  100. dy = [3 10 3; 0 0 0; -3 -10 -3]/16;
  101. IxY1 = conv2(Y1, dx, 'same');
  102. IyY1 = conv2(Y1, dy, 'same');
  103. gradientMap1 = sqrt(IxY1.^2 + IyY1.^2);
  104.  
  105. IxY2 = conv2(Y2, dx, 'same');
  106. IyY2 = conv2(Y2, dy, 'same');
  107. gradientMap2 = sqrt(IxY2.^2 + IyY2.^2);
  108.  
  109. %%%%%%%%%%%%%%%%%%%%%%%%%
  110. % Calculate the FSIM
  111. %%%%%%%%%%%%%%%%%%%%%%%%%
  112. T1 = 0.85; %fixed
  113. T2 = 160; %fixed
  114. PCSimMatrix = (2 * PC1 .* PC2 + T1) ./ (PC1.^2 + PC2.^2 + T1);
  115. gradientSimMatrix = (2*gradientMap1.*gradientMap2 + T2) ./(gradientMap1.^2 + gradientMap2.^2 + T2);
  116. PCm = max(PC1, PC2);
  117. SimMatrix = gradientSimMatrix .* PCSimMatrix .* PCm;
  118. FSIM = sum(sum(SimMatrix)) / sum(sum(PCm));
  119.  
  120. %%%%%%%%%%%%%%%%%%%%%%%%%
  121. % Calculate the FSIMc
  122. %%%%%%%%%%%%%%%%%%%%%%%%%
  123. T3 = 200;
  124. T4 = 200;
  125. ISimMatrix = (2 * I1 .* I2 + T3) ./ (I1.^2 + I2.^2 + T3);
  126. QSimMatrix = (2 * Q1 .* Q2 + T4) ./ (Q1.^2 + Q2.^2 + T4);
  127.  
  128. lambda = 0.03;
  129.  
  130. SimMatrixC = gradientSimMatrix .* PCSimMatrix .* real((ISimMatrix .* QSimMatrix) .^ lambda) .* PCm;
  131. FSIMc = sum(sum(SimMatrixC)) / sum(sum(PCm));
  132.  
  133. return;
  134.  
  135. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  136.  
  137. function [ResultPC]=phasecong2(im)
  138. % ========================================================================
  139. % Copyright (c) 1996-2009 Peter Kovesi
  140. % School of Computer Science & Software Engineering
  141. % The University of Western Australia
  142. % http://www.csse.uwa.edu.au/
  143. %
  144. % Permission is hereby granted, free of charge, to any person obtaining a copy
  145. % of this software and associated documentation files (the "Software"), to deal
  146. % in the Software without restriction, subject to the following conditions:
  147. %
  148. % The above copyright notice and this permission notice shall be included in all
  149. % copies or substantial portions of the Software.
  150. %
  151. % The software is provided "as is", without warranty of any kind.
  152. % References:
  153. %
  154. % Peter Kovesi, "Image Features From Phase Congruency". Videre: A
  155. % Journal of Computer Vision Research. MIT Press. Volume 1, Number 3,
  156. % Summer 1999 http://mitpress.mit.edu/e-journals/Videre/001/v13.html
  157.  
  158. nscale = 4; % Number of wavelet scales.
  159. norient = 4; % Number of filter orientations.
  160. minWaveLength = 6; % Wavelength of smallest scale filter.
  161. mult = 2; % Scaling factor between successive filters.
  162. sigmaOnf = 0.55; % Ratio of the standard deviation of the
  163. % Gaussian describing the log Gabor filter's
  164. % transfer function in the frequency domain
  165. % to the filter center frequency.
  166. dThetaOnSigma = 1.2; % Ratio of angular interval between filter orientations
  167. % and the standard deviation of the angular Gaussian
  168. % function used to construct filters in the
  169. % freq. plane.
  170. k = 2.0; % No of standard deviations of the noise
  171. % energy beyond the mean at which we set the
  172. % noise threshold point.
  173. % below which phase congruency values get
  174. % penalized.
  175. epsilon = .0001; % Used to prevent division by zero.
  176.  
  177. thetaSigma = pi/norient/dThetaOnSigma; % Calculate the standard deviation of the
  178. % angular Gaussian function used to
  179. % construct filters in the freq. plane.
  180.  
  181. [rows,cols] = size(im);
  182. imagefft = fft2(im); % Fourier transform of image
  183.  
  184. zero = zeros(rows,cols);
  185. EO = cell(nscale, norient); % Array of convolution results.
  186.  
  187. estMeanE2n = [];
  188. ifftFilterArray = cell(1,nscale); % Array of inverse FFTs of filters
  189.  
  190. % Pre-compute some stuff to speed up filter construction
  191.  
  192. % Set up X and Y matrices with ranges normalised to +/- 0.5
  193. % The following code adjusts things appropriately for odd and even values
  194. % of rows and columns.
  195. if mod(cols,2)
  196. xrange = [-(cols-1)/2:(cols-1)/2]/(cols-1);
  197. else
  198. xrange = [-cols/2:(cols/2-1)]/cols;
  199. end
  200.  
  201. if mod(rows,2)
  202. yrange = [-(rows-1)/2:(rows-1)/2]/(rows-1);
  203. else
  204. yrange = [-rows/2:(rows/2-1)]/rows;
  205. end
  206.  
  207. [x,y] = meshgrid(xrange, yrange);
  208.  
  209. radius = sqrt(x.^2 + y.^2); % Matrix values contain *normalised* radius from centre.
  210. theta = atan2(-y,x); % Matrix values contain polar angle.
  211. % (note -ve y is used to give +ve
  212. % anti-clockwise angles)
  213.  
  214. radius = ifftshift(radius); % Quadrant shift radius and theta so that filters
  215. theta = ifftshift(theta); % are constructed with 0 frequency at the corners.
  216. radius(1,1) = 1; % Get rid of the 0 radius value at the 0
  217. % frequency point (now at top-left corner)
  218. % so that taking the log of the radius will
  219. % not cause trouble.
  220.  
  221. sintheta = sin(theta);
  222. costheta = cos(theta);
  223. clear x; clear y; clear theta; % save a little memory
  224.  
  225. % Filters are constructed in terms of two components.
  226. % 1) The radial component, which controls the frequency band that the filter
  227. % responds to
  228. % 2) The angular component, which controls the orientation that the filter
  229. % responds to.
  230. % The two components are multiplied together to construct the overall filter.
  231.  
  232. % Construct the radial filter components...
  233.  
  234. % First construct a low-pass filter that is as large as possible, yet falls
  235. % away to zero at the boundaries. All log Gabor filters are multiplied by
  236. % this to ensure no extra frequencies at the 'corners' of the FFT are
  237. % incorporated as this seems to upset the normalisation process when
  238. % calculating phase congrunecy.
  239. lp = lowpassfilter([rows,cols],.45,15); % Radius .45, 'sharpness' 15
  240.  
  241. logGabor = cell(1,nscale);
  242.  
  243. for s = 1:nscale
  244. wavelength = minWaveLength*mult^(s-1);
  245. fo = 1.0/wavelength; % Centre frequency of filter.
  246. logGabor{s} = exp((-(log(radius/fo)).^2) / (2 * log(sigmaOnf)^2));
  247. logGabor{s} = logGabor{s}.*lp; % Apply low-pass filter
  248. logGabor{s}(1,1) = 0; % Set the value at the 0 frequency point of the filter
  249. % back to zero (undo the radius fudge).
  250. end
  251.  
  252. % Then construct the angular filter components...
  253.  
  254. spread = cell(1,norient);
  255.  
  256. for o = 1:norient
  257. angl = (o-1)*pi/norient; % Filter angle.
  258.  
  259. % For each point in the filter matrix calculate the angular distance from
  260. % the specified filter orientation. To overcome the angular wrap-around
  261. % problem sine difference and cosine difference values are first computed
  262. % and then the atan2 function is used to determine angular distance.
  263.  
  264. ds = sintheta * cos(angl) - costheta * sin(angl); % Difference in sine.
  265. dc = costheta * cos(angl) + sintheta * sin(angl); % Difference in cosine.
  266. dtheta = abs(atan2(ds,dc)); % Absolute angular distance.
  267. spread{o} = exp((-dtheta.^2) / (2 * thetaSigma^2)); % Calculate the
  268. % angular filter component.
  269. end
  270.  
  271. % The main loop...
  272. EnergyAll(rows,cols) = 0;
  273. AnAll(rows,cols) = 0;
  274.  
  275. for o = 1:norient % For each orientation.
  276. sumE_ThisOrient = zero; % Initialize accumulator matrices.
  277. sumO_ThisOrient = zero;
  278. sumAn_ThisOrient = zero;
  279. Energy = zero;
  280. for s = 1:nscale, % For each scale.
  281. filter = logGabor{s} .* spread{o}; % Multiply radial and angular
  282. % components to get the filter.
  283. ifftFilt = real(ifft2(filter))*sqrt(rows*cols); % Note rescaling to match power
  284. ifftFilterArray{s} = ifftFilt; % record ifft2 of filter
  285. % Convolve image with even and odd filters returning the result in EO
  286. EO{s,o} = ifft2(imagefft .* filter);
  287.  
  288. An = abs(EO{s,o}); % Amplitude of even & odd filter response.
  289. sumAn_ThisOrient = sumAn_ThisOrient + An; % Sum of amplitude responses.
  290. sumE_ThisOrient = sumE_ThisOrient + real(EO{s,o}); % Sum of even filter convolution results.
  291. sumO_ThisOrient = sumO_ThisOrient + imag(EO{s,o}); % Sum of odd filter convolution results.
  292. if s==1 % Record mean squared filter value at smallest
  293. EM_n = sum(sum(filter.^2)); % scale. This is used for noise estimation.
  294. maxAn = An; % Record the maximum An over all scales.
  295. else
  296. maxAn = max(maxAn, An);
  297. end
  298. end % ... and process the next scale
  299.  
  300. % Get weighted mean filter response vector, this gives the weighted mean
  301. % phase angle.
  302.  
  303. XEnergy = sqrt(sumE_ThisOrient.^2 + sumO_ThisOrient.^2) + epsilon;
  304. MeanE = sumE_ThisOrient ./ XEnergy;
  305. MeanO = sumO_ThisOrient ./ XEnergy;
  306.  
  307. % Now calculate An(cos(phase_deviation) - | sin(phase_deviation)) | by
  308. % using dot and cross products between the weighted mean filter response
  309. % vector and the individual filter response vectors at each scale. This
  310. % quantity is phase congruency multiplied by An, which we call energy.
  311.  
  312. for s = 1:nscale,
  313. E = real(EO{s,o}); O = imag(EO{s,o}); % Extract even and odd
  314. % convolution results.
  315. Energy = Energy + E.*MeanE + O.*MeanO - abs(E.*MeanO - O.*MeanE);
  316. end
  317.  
  318. % Compensate for noise
  319. % We estimate the noise power from the energy squared response at the
  320. % smallest scale. If the noise is Gaussian the energy squared will have a
  321. % Chi-squared 2DOF pdf. We calculate the median energy squared response
  322. % as this is a robust statistic. From this we estimate the mean.
  323. % The estimate of noise power is obtained by dividing the mean squared
  324. % energy value by the mean squared filter value
  325.  
  326. medianE2n = median(reshape(abs(EO{1,o}).^2,1,rows*cols));
  327. meanE2n = -medianE2n/log(0.5);
  328. estMeanE2n(o) = meanE2n;
  329.  
  330. noisePower = meanE2n/EM_n; % Estimate of noise power.
  331.  
  332. % Now estimate the total energy^2 due to noise
  333. % Estimate for sum(An^2) + sum(Ai.*Aj.*(cphi.*cphj + sphi.*sphj))
  334.  
  335. EstSumAn2 = zero;
  336. for s = 1:nscale
  337. EstSumAn2 = EstSumAn2 + ifftFilterArray{s}.^2;
  338. end
  339.  
  340. EstSumAiAj = zero;
  341. for si = 1:(nscale-1)
  342. for sj = (si+1):nscale
  343. EstSumAiAj = EstSumAiAj + ifftFilterArray{si}.*ifftFilterArray{sj};
  344. end
  345. end
  346. sumEstSumAn2 = sum(sum(EstSumAn2));
  347. sumEstSumAiAj = sum(sum(EstSumAiAj));
  348.  
  349. EstNoiseEnergy2 = 2*noisePower*sumEstSumAn2 + 4*noisePower*sumEstSumAiAj;
  350.  
  351. tau = sqrt(EstNoiseEnergy2/2); % Rayleigh parameter
  352. EstNoiseEnergy = tau*sqrt(pi/2); % Expected value of noise energy
  353. EstNoiseEnergySigma = sqrt( (2-pi/2)*tau^2 );
  354.  
  355. T = EstNoiseEnergy + k*EstNoiseEnergySigma; % Noise threshold
  356.  
  357. % The estimated noise effect calculated above is only valid for the PC_1 measure.
  358. % The PC_2 measure does not lend itself readily to the same analysis. However
  359. % empirically it seems that the noise effect is overestimated roughly by a factor
  360. % of 1.7 for the filter parameters used here.
  361.  
  362. T = T/1.7; % Empirical rescaling of the estimated noise effect to
  363. % suit the PC_2 phase congruency measure
  364. Energy = max(Energy - T, zero); % Apply noise threshold
  365.  
  366. EnergyAll = EnergyAll + Energy;
  367. AnAll = AnAll + sumAn_ThisOrient;
  368. end % For each orientation
  369. ResultPC = EnergyAll ./ AnAll;
  370. return;
  371.  
  372. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  373. % LOWPASSFILTER - Constructs a low-pass butterworth filter.
  374. %
  375. % usage: f = lowpassfilter(sze, cutoff, n)
  376. %
  377. % where: sze is a two element vector specifying the size of filter
  378. % to construct [rows cols].
  379. % cutoff is the cutoff frequency of the filter 0 - 0.5
  380. % n is the order of the filter, the higher n is the sharper
  381. % the transition is. (n must be an integer >= 1).
  382. % Note that n is doubled so that it is always an even integer.
  383. %
  384. % 1
  385. % f = --------------------
  386. % 2n
  387. % 1.0 + (w/cutoff)
  388. %
  389. % The frequency origin of the returned filter is at the corners.
  390. %
  391. % See also: HIGHPASSFILTER, HIGHBOOSTFILTER, BANDPASSFILTER
  392. %
  393.  
  394. % Copyright (c) 1999 Peter Kovesi
  395. % School of Computer Science & Software Engineering
  396. % The University of Western Australia
  397. % http://www.csse.uwa.edu.au/
  398. %
  399. % Permission is hereby granted, free of charge, to any person obtaining a copy
  400. % of this software and associated documentation files (the "Software"), to deal
  401. % in the Software without restriction, subject to the following conditions:
  402. %
  403. % The above copyright notice and this permission notice shall be included in
  404. % all copies or substantial portions of the Software.
  405. %
  406. % The Software is provided "as is", without warranty of any kind.
  407.  
  408. % October 1999
  409. % August 2005 - Fixed up frequency ranges for odd and even sized filters
  410. % (previous code was a bit approximate)
  411.  
  412. function f = lowpassfilter(sze, cutoff, n)
  413.  
  414. if cutoff < 0 || cutoff > 0.5
  415. error('cutoff frequency must be between 0 and 0.5');
  416. end
  417.  
  418. if rem(n,1) ~= 0 || n < 1
  419. error('n must be an integer >= 1');
  420. end
  421.  
  422. if length(sze) == 1
  423. rows = sze; cols = sze;
  424. else
  425. rows = sze(1); cols = sze(2);
  426. end
  427.  
  428. % Set up X and Y matrices with ranges normalised to +/- 0.5
  429. % The following code adjusts things appropriately for odd and even values
  430. % of rows and columns.
  431. if mod(cols,2)
  432. xrange = [-(cols-1)/2:(cols-1)/2]/(cols-1);
  433. else
  434. xrange = [-cols/2:(cols/2-1)]/cols;
  435. end
  436.  
  437. if mod(rows,2)
  438. yrange = [-(rows-1)/2:(rows-1)/2]/(rows-1);
  439. else
  440. yrange = [-rows/2:(rows/2-1)]/rows;
  441. end
  442.  
  443. [x,y] = meshgrid(xrange, yrange);
  444. radius = sqrt(x.^2 + y.^2); % A matrix with every pixel = radius relative to centre.
  445. f = ifftshift( 1 ./ (1.0 + (radius ./ cutoff).^(2*n)) ); % The filter
  446. return;

FSIM Code

上面的MATLAB程式碼為我在網上搜索的FSIM程式碼,還未詳細研究和呼叫,故以後再進一步介紹。

四 MOS

the mean opinion score(MOS),一種主觀的評價方式,即對影象的處理效果打分,我的理解為觀察者對演算法的處理結果從對比度,真實性,細節清晰度等三方面進行打分,如每一項滿分5分,值越大說明處理結果越好,具體的介紹見下面的文獻[ref]:

【ref】Realtime infrared image detail enhancement based on fast guided image filter and plateau equalization.

 

五  資訊熵(entropy)

影象中平均資訊量的多少。這是一種簡單的評價方式,MATLAB程式碼在網上和matlab軟體裡都有函式可以呼叫。

The matlab code of entropy

  1. %求影象熵值
  2. %傳入一張彩色圖片的矩陣
  3. %輸出圖片的影象熵值
  4. I_gray=imread('S11_ROG1.tif');
  5. %I_gray = rgb2gray(I);
  6. [ROW,COL] = size(I_gray);
  7.  
  8. %%
  9. %新建一個size =256的矩陣,用於統計256個灰度值的出現次數
  10. temp = zeros(256);
  11. for i= 1:ROW
  12.  
  13. for j = 1:COL
  14. %統計當前灰度出現的次數
  15. temp(I_gray(i,j)+1)= temp(I_gray(i,j)+1)+1;
  16.  
  17. end
  18. end
  19.  
  20. %%
  21.  
  22. res = 0.0 ;
  23. for i = 1:256
  24. %計算當前灰度值出現的概率
  25. temp(i) = temp(i)/(ROW*COL);
  26.  
  27. %如果當前灰度值出現的次數不為0
  28. if temp(i)~=0.0
  29.  
  30. res = res - temp(i) * (log(temp(i)) / log(2.0));
  31. end
  32. end
  33. disp(res);

Entropy Code

當然影象的評價指標還有psnr, mse等都是常見的,由於簡單,故不會作為論文中的客觀評價指標。本文選取的為RMSC, SSIM, MOS三種。還有演算法執行時間,這個與MATLAB軟體,個人電腦的記憶體,CPU型號,系統等有關,故作為評價指標並不可靠,謹記!通常用演算法複雜度衡量更準確。演算法執行時間,在matlab中用tic與 toc函式分別加在演算法的開頭與末尾即可。

六 常見影象處理指標(影象的均值和標準差)

這些基本的程式碼是得掌握,直接貼出matlab程式碼如下:

  1. 1. 求影象的均值
  2. close all;
  3. clear;
  4. clc;
  5. i=imread('d:/lena.jpg'); %載入真彩色影象路徑
  6. i=rgb2gray(i); %轉換為灰度圖
  7. i=double(i); %將uint8型轉換為double型,否則不能計算統計量
  8. [m,n]=size(i);
  9. s=0;
  10. for x=1:m
  11. for y=1:n
  12. s=s+i(x,y); %求畫素值總和 s i(x,y)表示位於某個座標下的畫素值
  13. end
  14. end
  15. %所有畫素均值
  16. a1=mean(mean(i)); %第一種方法:先計算列向量均值,再求總均值。
  17. a2=mean2(i); %第二種方法:用函式mean2求總均值
  18. a3=s/(m*n); %第三種方法:按公式計算,畫素值總和除以畫素個數。
  19. a4=sum(sum(i))/(m*n); %第四種方法:也是按公式計算,但是用sum來求畫素值總和。
  20.  
  21. 2.求影象的標準差
  22. close all
  23. clear
  24. clc;
  25. i=imread('d:/lena.jpg'); %載入真彩色影象
  26. i=rgb2gray(i); %轉換為灰度圖
  27. i=double(i); %將uint8型轉換為double型,否則不能計算統計量
  28. avg=mean2(i); %求影象均值
  29. [m,n]=size(i);
  30. s=0;
  31. for x=1:m
  32. for y=1:n
  33. s=s+(i(x,y)-avg)^2; %求得所有畫素與均值的平方和。
  34. end
  35. end
  36. %求影象的方差
  37. a1=var(i(:)); %第一種方法:利用函式var求得。
  38. a2=s/(m*n-1); %第二種方法:利用方差公式求得
  39. a3=(std2(i))^2; %第三種方法:利用std2求得標準差,再平方即為方差。

六 MATLAB除錯(找錯與改正)

錯誤使用 conv2

不支援 N 維陣列。

出錯 filter2 (line 59)第二級

y = conv2(hcol, hrow, x, shape);

出錯 ssim (line 188)第一級

mu1   = filter2(window, img1, 'valid');

出錯 SSIM_CAL (line 24)頂層

mval = ssim(A,ref)

%%上面為報錯資訊

1.找錯:

一是看懂MATLAB提示,最底下為最頂層函式1,往上為第一級函式1.1,第二級函式1.1.1。在上面的例子,SSIM_CAL 為頂層函式,在其24行的ssim函式(第一級)出錯。在ssim函式的188行的filter2函式(第二級)出錯。再繼續深入到filter2函式中的59行出錯,即conv2函數出錯;就是問題源頭所在、已找到錯誤。

2.改正(解決問題)

設定斷點,即在問題源頭出設定斷點,執行matlab程式,然後再檢視斷點處函式的值或變數值,找到出錯原因,並改正。比如,本例子中則在filter2函式中的59行(問題源頭)設定斷點,執行後檢視這一行程式碼conv2的變數值,和conv2函式的用法和注意事項,即conv2函式僅支援2維陣列卷積,但執行到這個斷點,發現conv2函式中的x變數,即輸入影象為m*n*3,即多維陣列,故報錯。通過斷點找到原因後,解決問題十分容易,即將輸入影象搞成二維即可。

這就是除錯matlab程式的方法,通過報錯定位問題,再通過設定斷點找出問題,再根據問題源頭解決即可!

這是今天做紅外影象細節增強演算法的收穫!