2

I try to align measuring images according to the contour of the parts. Unfortunately, the surrounding particles are often considered too aligned and I get wrong results.

Here is the basic openCV code iam using. Maybe i have to filter the particels somehow and use the wrap matrix on the original image afterwards.

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);

Does anyone have an idea how I can solve this problem?

Unfortunately I cannot provide the images to be analysed. They look something like this:

example image

Same result with feature matching (Sift):

result with feature matching

Wrong alignment: wrong alignment Correct alignment: correct alignment

Burak
  • 2,251
  • 1
  • 16
  • 33
bambuzzz
  • 31
  • 3
  • Instead of ECC, you may try feature matching. See [this similar question](https://stackoverflow.com/questions/68252060/image-alignment-of-multispectral-images-fails-with-ecc). – Burak Jul 23 '21 at 10:52
  • Same result. Its faster, but still its alligning the particles. I will edit the main post – bambuzzz Jul 23 '21 at 15:49
  • Looks like noise is the problem. Maybe [morphological opening](https://www.mathworks.com/help/images/morphological-dilation-and-erosion.html#f18-13322) helps? – Burak Jul 25 '21 at 09:55

0 Answers0