首页 > 解决方案 > cv2.findTransformECC 如何忽略小颗粒

问题描述

我尝试根据零件的轮廓对齐测量图像。不幸的是,周围的粒子通常被认为过于对齐,我得到了错误的结果。

这是我使用的基本 openCV 代码。也许我必须以某种方式过滤粒子,然后在原始图像上使用包裹矩阵。

im1 =  cv2.imread(im1Conv)
im2 =  cv2.imread(im2Conv)

# Convert images to grayscale
im1 = cv2.cvtColor(im1,cv2.COLOR_BGR2GRAY)
im2 = cv2.cvtColor(im2,cv2.COLOR_BGR2GRAY)

# percent of original size
width = int(im1.shape[1] * scale_percent / 100)
height = int(im1.shape[0] * scale_percent / 100)
dim1 = (width, height)

# percent of original size
width = int(im2.shape[1] * scale_percent / 100)
height = int(im2.shape[0] * scale_percent / 100)
dim2 = (width, height)

# resize image
im1 = cv2.resize(im1, dim1, interpolation = cv2.INTER_AREA)
im2 = cv2.resize(im2, dim2, interpolation = cv2.INTER_AREA)

# Find size of image1
sz = im1.shape

# Define the motion model
if convMode != "down":
    warp_mode = cv2.MOTION_EUCLIDEAN
else:
    warp_mode = cv2.MOTION_HOMOGRAPHY

# 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 = int(iteFromUi);

# Specify the threshold of the increment
# in the correlation coefficient between two iterations
termination_eps = float(koreFromUi);

# Define termination criteria
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations,  termination_eps)

# Run the ECC algorithm. The results are stored in warp_matrix.
(cc, warp_matrix) = cv2.findTransformECC (im1,im2,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);

有谁知道我该如何解决这个问题?

不幸的是,我无法提供要分析的图像。它们看起来像这样:

示例图像

与特征匹配 (Sift) 的结果相同:

结果与特征匹配

错误对齐: 对齐错误 正确对齐: 正确对齐

标签: python-3.xopencv

解决方案


推荐阅读