Ask Your Question

androgenus's profile - activity

2017-04-07 05:57:35 -0500 asked a question 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 @www.learnopencv.com/image-alignment-ecc-in-opencv-c-python. 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')
plt.show()

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://drive.google.com/file/d/0BzQd3MR0r7Y6Zm56WmQzVUR2V2s/view?usp=sharing @h_t_t_p_s://drive.google.com/file/d/0BzQd3MR0r7Y6bFozd1p5bzhMSk0/view?usp=sharing

Thank you for your help

2017-01-05 12:56:49 -0500 answered a question Open 16 bit video file (proprietary format) with OpenCV

I've managed to write a code in Python that can extract the frames from the .xvi file (as Tetragramm suggested in C#). It's strange that the first frame seems to be discarded or there's something else in the first 640x480x2 bytes after the videofile header (which is 1024 bytes). It also seems that each frame has a 32 byte header.

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

#read the data from the file
with open('filename.xvi', 'rb') as infile:
    #choose the frame number to extract
    n=51
    infile.seek(1024+(32+640*480*2)*n)
    #read frame bytes into buffer
    buf = infile.read(640*480*2)

#use numpy to construct an array from the bytes
x = np.frombuffer(buf, dtype='uint16').reshape(480, 640)
#visualize extracted frame
plt.figimage(x, cmap='gray_r', vmin=np.min(x), vmax=np.max(x))
plt.show()

As you can see from the code I've visualized the frame (16bit depth array) with matplotlib. I cannot understand why when trying to visualize with opencv like in the code below it gives me an error (img_data = None). Maybe someone could help? Does opencv have a problem opening 16 bit greyscale images? If someone could help, here is a link to a small .xvi file with 81 frames (32 Mb) https://drive.google.com/file/d/0BzQd...

x = np.frombuffer(buf, dtype='uint16').reshape(480, 640)
img_data = cv2.imdecode(x,2)

Thank you for your help.

2017-01-05 10:46:23 -0500 received badge  Enthusiast
2016-12-17 08:51:38 -0500 received badge  Supporter (source)
2016-12-17 08:51:24 -0500 commented answer Open 16 bit video file (proprietary format) with OpenCV

Thank you for your answer. Thats great! Now have to figure out how to do it in Python.

2016-12-15 12:31:03 -0500 received badge  Editor (source)
2016-02-16 06:03:58 -0500 commented question Open 16 bit video file (proprietary format) with OpenCV

Hi and thank you for the answer. The SDK/API costs a lot like 1000 eur and have not bought it with the camera (stupid mistake). So looking now for some shortcut maybe someone here has used a xenics infrared camera with opencv?

2016-02-15 11:54:15 -0500 asked a question Open 16 bit video file (proprietary format) with OpenCV

I would very much like to import in OpenCV a video file which is in a proprietary format - from a Xenics-Gobi infrared camera - file extension *.xvi.

The file is a 16 bit video format and with the image processing program that came with the camera i can export frames as 16 bit png frames that contain thermal information.

Would it be possible somehow to open the video files in OpenCV and process the frames?

Thank you for your time.

I've just found out how the .xvi file is structured:

The first 1024 bytes of an .XVI file is reserved for header information. After this block of 1kb you have raw frame data in the following format: Pixel(x,y) = block[ x+(y*width) ] Where each pixel is represented by an integer scaled up to the nearest CPU representation. For example, if the bit size is 12, you will get a 16bit integer. For bit sizes larger than 16 you will get a 32 bit integer, followed by a 64 bit representation (and so on ...) Note: All integers used are in Intel byte order.

Maybe with the above information someone could help me import the file in openCV (python code).

Thank you again