--python realization of multi-focus image fusion algorithm based on wavelet transform

Source: Internet
Author: User
Tags xdiff

# Finish set to do, before the csdn on the data, wavelet transform this part is all MATLAB, and few write the reliable. Python's Pywavelet library is too little, so I'm going to pass on what I'm doing.
# What do you ask me why not use MATLAB. Well, I hate this software, that's all.
#-*-Coding:utf-8-*-
Import PYWT Import NumPy as NP import Matplotlib.pyplot as plt from PIL import Image import cv2 ' ' paper Two scenarios: 1, for all the low frequency components
The pixel point calculates its local variance, the variance of each graph is added up by a little by two chart, obtains the weight of two graphs to merge the image the value of each pixels is two graphs corresponding pixel value weighted average, this right is calculated from above weight value.
2, the pixel points of each high frequency component are extracted with canny operator, then the local variance is computed for each pixel point of the edge image, and the pixel of the pixel of the variance image fusion image is obtained with the corresponding pixel value of the picture.
3, the effect is not very good, at the edge of the small wave block has a significant gray-scale jump (that is, some of the paper said the block effect), but why? 4, the reason has been found, not the variance of a point to the whole graph, but rather a small window near a point to find the local variance of the window 5, the method of local variance is very perfect for the multiple focus image ' Def imgopen (path): Img=image.open (Path). Convert (' L ') Imgarray=np.array (img) return Imgarray # for low-frequency components, the weight of two graphs is calculated as a weighted ratio of Def varianceweight (IMG1,IMG2): Mean1,va
    R1=cv2.meanstddev (IMG1) Mean2,var2=cv2.meanstddev (IMG2) weight1=var1/(VAR1+VAR2) weight2=var2/(VAR1+VAR2)
Return WEIGHT1,WEIGHT2 # Measured this function is very good ... def getvarianceimg (array): Row,col=array.shape Varimg=np.zeros ((row,col)) for I in Xrange (row): for J In Xrange (COL): up=i-5 if i-5>0 else 0 down=i+5 if i+5<row else row left=j-5 if J-5>0 Else 0 right=j+5 if j+5<col else Col Window=array[up:down,left:right] Mean,va
    R=cv2.meanstddev (window) Varimg[i,j]=var return varimg # does not write canny, temporarily replaces Def Sobel (IMG) with calcgradient operator: Xdiff=cv2. Sobel (Img,cv2. cv_16s,1,0) Ydiff=cv2. Sobel (Img,cv2. cv_16s,0,1) Stdxdiff=cv2.convertscaleabs (Xdiff) stdydiff=cv2.convertscaleabs (Ydiff) gradient=np.sqrt (stdXdiff* *2+stdydiff**2) return gradient def testwave (IMG1,IMG2): transf1=pywt.wavedec2 (IMG1, ' Haar ', level=4) transf2=p
        YWT.WAVEDEC2 (Img2, ' Haar ', level=4) assert Len (TRANSF1) ==len (TRANSF2) recwave=[] for K in range (len (TRANSF1)): # Processing of low-frequency components if k==0:loweight1,loweight2 = Varianceweight (transf1[0],transf2[0]) LOWFR eq = Np.zeros (transf2[0].shape) Row,col = Transf1[0].shape to I in range (row): fo R J in Range (COL): lowfreq[i,j] = loweight1*transf1[0][i,j]+ Loweight2*transf2[0][i,j] Recwave.append (lowfreq) Continue # Processing high-frequency Components cvtarray=[]  For array1,array2 in Zip (Transf1[k],transf2[k]): Tmp_row,tmp_col = Array1.shape Highfreq = Np.zeros ((Tmp_row,tmp_col)) var1=getvarianceimg (array1); var2=getvarianceimg (array2) for I in rang E (Tmp_row): for J in Range (Tmp_col): Highfreq[i,j]=array1[i,j] If var1[i,j]>var2[i, J] Else Array2[i,j] Cvtarray.append (highfreq) recwave.append (tuple (Cvtarray)) return PYWT.WAVEREC2
    (Recwave, ' Haar ') def testplot (org1,org2,img): Plt.subplot (131) plt.imshow (org1,cmap= ' Gray ') plt.axis (' off ') Plt.subplot (132) plt.imshow (org2,cmap= ' Gray ') plt.axis (' Off ') Plt.subplot () plt.imshow (img,cmap= ' GRA Y ') plt.axis (' Off ') plt.show () if __name__== ' __main__ ': Img1=imgopen (' f:\\python\\try\\basicimageoperation\\p Epsia.jpg ') img2=imgOpen (' f:\\python\\try\\basicimageoperation\\pepsib.jpg ') rec=testwave (IMG1,IMG2) testplot (IMG1,IMG2,REC) 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.