http://www.mamicode.com/info-detail-2102099.html
import cv2
import math
import numpy as np
from matplotlib import pyplot as plt
im1 = cv2.imread('Sample_BW_1.png',0)
Zernike_keinel_M00 = np.array([[0,0.287,0.0686,0.0807,0.0686,0.0287,0],
[0.0287, 0.0815, 0.0816, 0.0816, 0.0816, 0.0815, 0.0287],
[0.0686, 0.0816, 0.0816, 0.0816, 0.0816, 0.0816, 0.0686],
[0.0807, 0.0816, 0.0816, 0.0816, 0.0816, 0.0816, 0.0807],
[0.0686, 0.0816, 0.0816, 0.0816, 0.0816, 0.0816, 0.0686],
[0.0287, 0.0815, 0.0816, 0.0816, 0.0816, 0.0815, 0.0287],
[0,0.0287, 0.0686, 0.0807, 0.0686, 0.0287, 0]])
Zernike_keinel_M11R = np.array([[0, -0.015, -0.019, 0, 0.019, 0.015, 0],
[-0.0224, -0.0466, -0.0233, 0, 0.0233, 0.0466, 0.0224],
[-0.0573, -0.0466, -0.0233, 0, 0.0233, 0.0466, 0.0573],
[-0.069, -0.0466, -0.0233, 0, 0.0233, 0.0466, 0.069],
[-0.0573, -0.0466, -0.0233, 0, 0.0233, 0.0466, 0.0573],
[-0.0224, -0.0466, -0.0233, 0, 0.0233, 0.0466, 0.0224],
[0, -0.015, -0.019, 0, 0.019, 0.015, 0]])
Zernike_keinel_M11L = np.array([[0, -0.0224, -0.0573, -0.069, -0.0573, -0.0224, 0],
[-0.015, -0.0466, -0.0466, -0.0466, -0.0466, -0.0466, -0.015],
[-0.019, -0.0233, -0.0233, -0.0233, -0.0233, -0.0233, -0.019],
[0, 0, 0, 0, 0, 0, 0],
[0.019, 0.0233, 0.0233, 0.0233, 0.0233, 0.0233, 0.019],
[0.015, 0.0466, 0.0466, 0.0466, 0.0466, 0.0466, 0.015],
[0, 0.0224, 0.0573, 0.069, 0.0573, 0.0224, 0]])
Zernike_keinel_M20 = np.array([[0, 0.0225, 0.0394, 0.0396, 0.0394, 0.0225, 0],
[0.0225, 0.0271, -0.0128, -0.0261, -0.0128, 0.0271, 0.0225],
[0.0394, -0.0128, -0.0528, -0.0661, -0.0528, -0.0128, 0.0394],
[0.0396, -0.0261, -0.0661, -0.0794, -0.0661, -0.0261, 0.0396],
[0.0394, -0.0128, -0.0528, -0.0661, -0.0528, -0.0128, 0.0394],
[0.0225, 0.0271, -0.0128, -0.0261, -0.0128, 0.0271, 0.0225],
[0, 0.0225, 0.0394, 0.0396, 0.0394, 0.0225, 0]])
Zernike_keinel_M31R = np.array([[0, -0.0103, -0.0073, 0, 0.0073, 0.0103, 0],
[-0.0153, -0.0018, 0.0162, 0, -0.0162, 0.0018, 0.0153],
[-0.0223, 0.0324, 0.0333, 0, -0.0333, -0.0324, 0.0223],
[-0.0190, 0.0438, 0.0390, 0, -0.0390, -0.0438, 0.0190],
[-0.0223, 0.0324, 0.0333, 0, -0.0333, -0.0324, 0.0223],
[-0.0153, -0.0018, 0.0162, 0, -0.0162, 0.0018, 0.0153],
[0, -0.0103, -0.0073, 0, 0.0073, 0.0103, 0]])
Zernike_keinel_M31L = np.array([[0, -0.0153, -0.0223, -0.019, -0.0223, -0.0153, 0],
[-0.0103, -0.0018, 0.0324, 0.0438, 0.0324, -0.0018, -0.0103],
[-0.0073, 0.0162, 0.0333, 0.039, 0.0333, 0.0162, -0.0073],
[0, 0, 0, 0, 0, 0, 0],
[0.0073, -0.0162, -0.0333, -0.039, -0.0333, -0.0162, 0.0073],
[0.0103, 0.0018, -0.0324, -0.0438, -0.0324, 0.0018, 0.0103],
[0, 0.0153, 0.0223, 0.0190, 0.0223, 0.0153, 0]])
Zernike_keinel_M40 = np.array([[0, 0.013, 0.0056, -0.0018, 0.0056, 0.013, 0],
[0.0130, -0.0186, -0.0323, -0.0239, -0.0323, -0.0186, 0.0130],
[0.0056, -0.0323, 0.0125, 0.0406, 0.0125, -0.0323, 0.0056],
[-0.0018, -0.0239, 0.0406, 0.0751, 0.0406, -0.0239, -0.0018],
[0.0056, -0.0323, 0.0125, 0.0406, 0.0125, -0.0323, 0.0056],
[0.0130, -0.0186, -0.0323, -0.0239, -0.0323, -0.0186, 0.0130],
[0, 0.013, 0.0056, -0.0018, 0.0056, 0.013, 0]])
im1_Blur = cv2.medianBlur(im1,5) #中值滤波
im1_Thre = cv2.adaptiveThreshold(im1_Blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,7, 4) #自适应阈值处理
im1_canny = cv2.Canny(im1_Blur,20,160) #Canny算子
#求矩
im1_m00 = cv2.filter2D(im1_canny,-1,Zernike_keinel_M00)
im1_m11L = cv2.filter2D(im1_canny,-1,Zernike_keinel_M11L)
im1_m11R = cv2.filter2D(im1_canny,-1,Zernike_keinel_M11R)
im1_m20 = cv2.filter2D(im1_canny,-1,Zernike_keinel_M20)
im1_m31R = cv2.filter2D(im1_canny,-1,Zernike_keinel_M31R)
im1_m31L = cv2.filter2D(im1_canny,-1,Zernike_keinel_M31L)
im1_m40 = cv2.filter2D(im1_canny,-1,Zernike_keinel_M40)
row,clou = im1_canny.shape
for i in range(row):
for y in range(clou):
if im1_m00[i][y] == 0:
else:
theta = math.atan2(im1_m31R[i][y],im1_m31L[i][y]) # 计算theta`````````````````````````
print(theta)
#计算Z11` Z31`
rotated_z11 = math.sin(theta) * (im1_m11L[i][y]) + math.cos(theta) * (im1_m11R[i][y])
rotated_z31 = math.sin(theta) * (im1_m31L[i][y]) + math.cos(theta) * (im1_m31R[i][y])
l_mathod1 = (math.sqrt((5 * im1_m40[i][y] + 3 * im1_m20[i][y]))) / (8 * im1_m20[i][y])
l_mathod2 = math.sqrt((5 * rotated_z31 + rotated_z11) / (6 * rotated_z11))
l = (l_mathod1+l_mathod2)/2 #计算l
k = (3 * rotated_z11) / (2 / math.pow((1 - l * l), 1.5))
h = ((im1_m00[i][y] - (k * math.pi)) / 2 + k * math.asin(l) + k * (l_mathod2 * (math.sqrt(1 - l_mathod2 * l_mathod2)))) / (math.pi)
k_value = 20
l_value = math.sqrt(2)/g_n
asb1 = abs(l_mathod2-l_mathod1)
if k>=k_value and asb1<=l_value:
im1_canny[]
评论0