Wednesday, October 15, 2014

Coordinate and colour channel differences between OpenCV and VTK

When you are working with computer vision projects, you will probably have to encounter both OpenCV and VTK. To compare these two with regard to how their image coordinates are oriented is very important.

OpenCV image pixel coordinates are numbered  (x,y) from top left and x denotes columns and y denotes rows. Hence for a 1280*720 (width * height) image, (0,0) will be at top left. Top right will be (1280,0) and bottom right corner will be (1280,720).

VTK uses entirely different image coordinate system. Here a pixel coordinate (0,0) is at bottom left. The x denotes columns and y denotes rows. Hence bottom right will be (1280,0), top left will be (0,720) and top right will be (1280,720). So to convert an image properly from VTK to OpenCV you have to flip it vertically.

cv::flip(vtkImage, OpenCVimage, 0)
Click here for cv::flip Documentation 

The flag 0 is for flipping the image vertically to switch between top left and bottom left image origin.
The arguments vtkImage and OpenCVimage are cv::Mat types with vtkImage being the source and OpenCVimage being the output.   vtkImage in cv::Mat format is made using windowToImageFilter class in VTK.
Another big difference is the the default colour channel coding in OpenCV is in BGR format whilst it is RGB in VTK.
These two differences should always be kept in mind while working with images. Usually you have an image rendered by VTK and then you have to process it by OpenCV. This is possible by using the windowToImageFilter class in VTK. Using this, vtkImage in cv::Mat format can be made. I have written a small program which shows how these differences affect when you convert VTK image to OpenCV image. Click here for the Github repository link




No comments:

Post a Comment