程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+opencv plt Imshow() can display CV2 normally Imshow() is displayed as a white problem

編輯:Python

problem :
In image processing , After Fourier transform, the image needs to be passed through cv2.imshow() Display or pass cv2.imwrite() Save to the specified path , But the displayed or saved images are all white , Only through plt.imshow() To display properly . For example, the following code corresponds to the processing result :

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("Images/ExperimentalImage4/originalImage/architecture2.png",-1)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
ishift = np.fft.ifftshift(dftShift)
iImage = cv2.idft(ishift)
iImg = cv2.magnitude(iImage[:,:,0],iImage[:,:,1])
plt.subplot(121),plt.imshow(img,cmap = "gray"),plt.title("original"),plt.axis('off')
plt.subplot(122),plt.imshow(iImg,cmap = "gray"),plt.title("inverse"),plt.axis('off')
cv2.imshow("inverse",iImg)
cv2.waitKey(10)
plt.show()


terms of settlement :
Divide each pixel value after inverse Fourier transform by the product ( Row number * Number of columns , For example, the image separation rate in this example is 640*512, Divide by 327680), Finally, convert the floating-point number to 8 Bit unsigned integer , Specific code and processing The results are shown below :

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("Images/ExperimentalImage4/originalImage/architecture2.png",-1)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
ishift = np.fft.ifftshift(dftShift)
iImage = cv2.idft(ishift)
iImg = cv2.magnitude(iImage[:,:,0],iImage[:,:,1])
plt.subplot(121),plt.imshow(img,cmap = "gray"),plt.title("original"),plt.axis('off')
plt.subplot(122),plt.imshow(iImg,cmap = "gray"),plt.title("inverse"),plt.axis('off')
iImg = np.uint8(iImg / 327680) ## Divide each pixel value after inverse Fourier transform by the product ( Row number * Number of columns ), Finally, convert the floating-point number to 8 Bit unsigned integer 
cv2.imwrite('./.inverse.png',iImg)
cv2.imshow("inverse",iImg)
cv2.waitKey(10)
plt.show()

Final display results :
That's the solution , I hope I can help you !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved