Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

image registration for 16 bit gray images

Hi you all,

I'm trying to stabilise a video sequence from an infrared camera. Each frame is a PNG 16 bit gray image and I'm trying to reproduce an image registration procedure found My code is the following:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img0 = cv2.imread('test_55.png',-1)
img1 = cv2.imread('test_400.png',-1)

def get_gradient(im) :
    # Calculate the x and y gradients using Sobel operator
    grad_x = cv2.Sobel(im,cv2.CV_64F,1,0,ksize=3)
    grad_y = cv2.Sobel(im,cv2.CV_64F,0,1,ksize=3)

    # Combine the two gradients
    grad = cv2.addWeighted(np.absolute(grad_x), 0.5, np.absolute(grad_y), 0.5, 0)
    return grad

warp_mode = cv2.MOTION_AFFINE

    # Set the warp 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)

# Set the stopping criteria for the algorithm.
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 5000,  1e-10)

(cc, warp_matrix) = cv2.findTransformECC (get_gradient(img0), get_gradient(img1),warp_matrix, warp_mode, criteria)

if warp_mode == cv2.MOTION_HOMOGRAPHY :
    im_aligned = cv2.warpPerspective (img1, warp_matrix, (width,height), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
else :
    # Use Affine warp when the transformation is not a Homography
    im_aligned = cv2.warpAffine(img1, warp_matrix, (width, height), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP);

plt.imshow(im_aligned,cmap = 'gray')

I'm getting the following error:(-210) Images must have 8uC1 or 32fC1 type in function findTransformECC. I could transform the image from 16bit to 8 bits but I want a 16bit image aligned to the reference frame. I think that the only important motion in the video would be a translation because of fine tremor of fingers and hand. Please let me know if there are other options for stabilisation/image registration that could deal with 16 bit grey images. Here are the links to 2 frames from the video:

@h_t_t_p_s:// @h_t_t_p_s://

Thank you for your help