反思一下,以后不能拿到任务相当然的开始直接试,多收集资料,先看看论文,吸收知识再有的放矢较为明智。
早上先了解一下行业相关,毕竟刚进来什么都不懂:https://zhuanlan.zhihu.com/p/31319878
对于行业及其相关识别方法有了一定的了解,开始进行图像配准+形态学处理识别短路的测试。
参考大佬的github和论文:https://blog.youkuaiyun.com/full_speed_turbo/article/details/68484450
将前面无法直接做异或的图像进行对齐,首先对图像进行前期的预处理:
im1 = cv2.imread("./short_circuit_2/0_13_211_c.bmp")
im2 = cv2.imread("./short_circuit_2/0_13_211_g.bmp")
#大小调整
#im1 = cv2.resize(im1,(im2.shape[1],im2.shape[0]), interpolation = cv2.INTER_CUBIC)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) # 矩形结构
# Convert images to grayscale
im1_gray1 = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
im1_gray = cv2.morphologyEx(im1_gray1, cv2.MORPH_CLOSE,kernel) # 闭运算
im2_gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
ECC图像配准大法:
# Find size of image1
sz = im1_gray.shape
# Define the motion model
warp_mode = cv2.MOTION_TRANSLATION
# Define 2x3 or 3x3 matrices and initialize the matrix to identity
if warp_mode == cv2.MOTION_HOMOGRAPHY:
warp_matrix = np.eye(3, 3, dtype=np.float32)
else:
warp_matrix = np.eye(2, 3, dtype=np.float32)
# Specify the number of iterations.
number_of_iterations = 5000
# Specify the threshold of the increment
# in the correlation coefficient between two iterations
termination_eps = 1e-10
# Define termination criteria
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps)
im1_gray = cv2.resize(im1_gray, (im2_gray.shape[1], im2_gray.shape[0]), interpolation=cv2.INTER_CUBIC)
# Run the ECC algorithm. The results are stored in warp_matrix.
(cc, warp_matrix) = cv2.findTransformECC(im1_gray, im2_gray, warp_matrix, warp_mode, criteria)
if warp_mode == cv2.MOTION_HOMOGRAPHY:
# Use warpPerspective for Homography
im2_aligned = cv2.warpPerspective(im2, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
else:
# Use warpAffine for Translation, Euclidean and Affine
im2_aligned = cv2.warpAffine(im2, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
图像对齐后,对图像进行减法,并后续做一个开运算消除噪点,后续用cv的findcontours函数找连通域化矩形框进行标记:
img_diff = cv2.subtract(im1, im2_aligned)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) # 矩形结构
img_diff2 = cv2.morphologyEx(img_diff, cv2.MORPH_OPEN,kernel) # 开运算
cv2.namedWindow('subtracted', cv2.WINDOW_NORMAL)
cv2.imshow("subtracted", img_diff) # 减运算图
#cv2.imwrite("img_diff.bmp",img_diff,None)
cv2.namedWindow('subtracted2', cv2.WINDOW_NORMAL)
cv2.imshow("subtracted2", img_diff2)
img_diff2 = cv2.cvtColor(img_diff2, cv2.COLOR_BGR2GRAY)
_,contours, hierarchy = cv2.findContours(img_diff2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for j in range(0,len(contours)):
x, y, w, h = cv2.boundingRect(contours[j])
cv2.rectangle(im1, (x,y), (x+w,y+h), (0,0,255), 4)
cv2.namedWindow('Image 1', cv2.WINDOW_NORMAL)
cv2.imshow("Image 1", im1)
#cv2.imwrite("./result_2/img_result%d.jpg"%(i),im1,None)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果图: