Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Conversion between cv2, cv, mahotas, and SimpleCV

I am having to do a lot of vision related work in Python lately, and I am facing a lot of difficulties switching between formats. When I read an image using Mahotas, I cannot seem to get it to cv2, though they are both using numpy.ndarray. SimpleCV can take OpenCV images easily, but getting SimpleCV image out for legacy cv or mahotas seems to be quite a task.

Some format conversion syntaxes would be really appreciated. For example, if I open a greyscale image using mahotas, it is treated to be in floating point colour space by default, as I gather. Even when I assign the type as numpy.uint8, cv2 cannot seem to recognise it as an array. I do not know how to solve this problem. I am not having much luck with colour images either. I am using Python 2.7 32bit on Ubuntu Oneiric Ocelot.

Thanks in advance!

like image 522
Subhamoy S. Avatar asked Oct 09 '22 17:10

Subhamoy S.


2 Answers

I have never used mahotas. But I'm currently working on SimpleCV. I have just sent a pull request for making SimpleCV numpy array compatible with cv2.

So, basically,

Image.getNumpy() -> numpy.ndarray for cv2

Image.getBitmap() -> cv2.cv.iplimage

Image.getMatrix() -> cv2.cv.cvmat

To convert cv2 numpy array to SimpleCV Image object,

Image(cv2_image) -> SimpleCV.ImageClass.Image

like image 121
Froyo Avatar answered Oct 11 '22 06:10

Froyo


With only experience in cv2 and SimpleCV, to convert from SimpleCV to cv2:

cv2_image = simplecv_image.getNumpyCv2()

To convert from cv2 to SimpleCV:

simplecv_image = Image(cv2_image.transpose(1, 0, 2)[:, :, ::-1])
like image 26
Anthony Avatar answered Oct 11 '22 06:10

Anthony