Convert image between cv2, cv, mahotas and SimpleCV

I have had to work a lot on Python lately, and I have to deal with a lot of difficulties when switching between formats. When I read the image using Mahotas, I cannot get it before cv2, although both of them use numpy.ndarray. SimpleCV can easily receive OpenCV images, but acquiring SimpleCV images for legacy cv or mahotas seems to be quite a challenge.

Some format conversion syntaxes will be really appreciated. For example, if I open a grayscale image with mahotas, by default I get it in a floating-point color space. Even when I assign type to numpy.uint8, cv2 does not seem to recognize it as an array. I do not know how to solve this problem. I also don’t really like color images. I am using Python 2.7 32bit on Ubuntu Oneiric Ocelot.

Thanks in advance!

+4
source share
2 answers

I have never used a macho. But I'm working on SimpleCV right now. I just sent a pull request to create a cv2 compatible SimpleCV numpy array.

So basically

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

Image .getBitmap () β†’ cv2.cv.iplimage

Image.getMatrix () β†’ cv2.cv.cvmat

To convert a cv2 numpy array to a SimpleCV object object,

Image (cv2_image) β†’ SimpleCV.ImageClass.Image

+2
source

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]) 
+1
source

Source: https://habr.com/ru/post/1395696/


All Articles