Without Function數位影像處理(細線化)程式碼python數位影像處理(二值化,低通,高通,中值濾波器)程式碼python
值方圖等化
import cv2 img=cv2.imread("8.png",0) cv2.imshow("IMG",img) dst=cv2.equalizeHist(img) cv2.imshow("DST",dst) cv2.waitKey(0) cv2.destroyAllWindows()
影像二值化
import cv2 a=cv2.imread("8.png",0) cv2.imshow("IMG",a) x=a.shape[0] y=a.shape[1] threshold=125 for i in range(x): for j in range(y): if(a[i][j]>threshold): a[i][j]=0 else: a[i][j]=255 cv2.imshow("threshold",a) cv2.waitKey(0) cv2.destroyAllWindows()
調整影像強度
import cv2 a=cv2.imread("8.png",0) cv2.imshow("IMG",a) x=a.shape[0] y=a.shape[1] for i in range(x): for j in range(y): a[i][j]=a[i][j]*0.7 cv2.imshow("dark",a) b=cv2.imread("8.png",0) for i in range(x): for j in range(y): b[i][j]=b[i][j]*1.5 if(int(b[i][j])>=int(255)): b[i][j]=int(255) cv2.imshow("bright",b) cv2.waitKey(0) cv2.destroyAllWindows()
中值濾波
import cv2 import numpy as np a=cv2.imread("8.png",0) cv2.imshow("IMG",a) x=a.shape[0] y=a.shape[1] for i in range(1,x): for j in range(1,y): a[i][j]=np.median(a[i-1:i+2,j-1:j+2]) cv2.imshow("cv2.medianBlur",a) cv2.waitKey(0) cv2.destroyAllWindows()
低通濾波
import cv2 import numpy as np img=cv2.imread("8.png",0) cv2.imshow("Ori",img) #原圖 fft1=np.fft.fft2(img) #傅立葉轉頻域 fft2=np.fft.fftshift(fft1) #把低頻訊號放在中間 fft3=np.log(np.abs(fft2))/20 #取絕對值取log不然強度太強 cv2.imshow("fft",fft3) #成功轉成頻域 ifft2=np.fft.ifftshift(fft2) #反傅立葉回來(把低頻移到邊角) iimg=np.fft.ifft2(ifft2) #反傅立葉回時域 iimg=np.abs(iimg) #取絕對值取log不然強度太強 cv2.imshow("ifft",iimg/200) rows,cols = img.shape mask = np.ones(img.shape,np.uint8) #創造跟原圖一樣大小都是1的陣列 mask[int(rows/2)-30:int(rows/2)+30,int(cols/2)-30:int(cols/2)+30] = 0 cv2.imshow("mask",mask) #低通遮罩 highpass=mask*fft2 #傅立葉跟低通相乘 highpass1=np.fft.ifftshift(highpass) #反傅立葉回來(把低頻移到邊角) highpass2=np.fft.ifft2(highpass1) #反傅立葉回時域 highpass3=np.abs(highpass2) #取絕對值取log不然強度太強 cv2.imshow("highpass",highpass3/60)#成功轉成頻域 result=highpass3+img #邊緣跟原圖相加 cv2.imshow("result",result/200) #達到銳化效果 cv2.waitKey(0) cv2.destroyAllWindows()
高通濾波
import cv2 import numpy as np img=cv2.imread("8.png",0) cv2.imshow("Ori",img) #原圖 fft1=np.fft.fft2(img) #傅立葉轉頻域 fft2=np.fft.fftshift(fft1) #把低頻訊號放在中間 fft3=np.log(np.abs(fft2))/20 #取絕對值取log不然強度太強 cv2.imshow("fft",fft3) #成功轉成頻域 rows,cols = img.shape mask = np.ones(img.shape,np.uint8) #創造跟原圖一樣大小都是0的陣列 #mask = cv2.circle(mask, (int(rows/2), int(cols/2)), 30, (255, 255, 255), -1) #畫一個園 cv2.imshow("mask",mask) #低通遮罩 lowpass=mask*fft2 #傅立葉跟低通相乘 lowpass1=np.fft.ifftshift(lowpass) #反傅立葉回來(把低頻移到邊角) lowpass2=np.fft.ifft2(lowpass1) #反傅立葉回時域 lowpass3=np.abs(lowpass2) #取絕對值取log不然強度太強 cv2.imshow("highpass",lowpass3/60000)#成功轉成頻域 cv2.waitKey(0) cv2.destroyAllWindows()
留言
張貼留言