5

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!

luispedro
  • 6,934
  • 4
  • 35
  • 45
Subhamoy S.
  • 6,566
  • 10
  • 37
  • 53
  • Now, I've never used Mahotas or SimpleCV, but have you tried converting NumPy arrays to cv images via `cv.fromarray()`? Additionally, you can convert cv images to NumPy via an IplImage's `.tostring()` method. (I.E. `array = numpy.fromstring(image.tostring(),dtype=numpy.float32)` Also, be sure to check out [OpenCV's Cookbook](http://opencv.willowgarage.com/documentation/python/cookbook.html). Finally, out of curiousity, is it really necessary to be using all the different libraries? I would've thought, at the very least, SimpleCV and OpenCV together would have ALOT of overlap. – Ancallan Feb 10 '12 at 06:24
  • I am no expert, so it would not be wise for me to say how necessary it is, but usually I try to find all the fastest running methods and try to combine them, because speed at low system resource is crucial for what I am trying. I have been using the methods you have mentioned, and that is the easy bit. Mahotas has some implementations that are superfast, and I would love to try them out, except I can't, because I cannot seem to get the numpy out! Programmers keep pointing out how cv2 is more flexible with numpy but much slower, so I thought it is worth a shot! – Subhamoy S. Feb 10 '12 at 09:54
  • I don't often come across an OpenCV implementation that's particularly slow. Most of what I've used them for has been able to run realtime. My recommendation would be to pick one and stick with it for most things. If you end up running into problems with resources, take it from there! SimpleCV is largely an OpenCV wrapper, if I recall correctly - that might be a good one to try for now. I'm biased towards OpenCV, myself, but then I do alot of my work in C#. – Ancallan Feb 10 '12 at 12:52
  • I have tried to run blob detection with SimpleCV on my intel i3 laptop with 4 gigs of RAM, and it is quite slow, even after I thresholded most of the colour information off the image before running blob detection on it. I have to finally run the code on a robot with much inferior processor than this and more limited RAM, and I do not see SimpleCV delivering anything usable in there. – Subhamoy S. Feb 10 '12 at 15:52
  • Ah yes, I seem to remember they did their own blob detection when I did some looking around when you first mentioned it. If I get the chance today, I'll grab both Mahotas and SimpleCV and see if I can't deliver a proper answer to you. It may also help to take a look through this, if you havn't already! [PerformancePython](http://www.scipy.org/PerformancePython) – Ancallan Feb 11 '12 at 01:19
  • Thank you very much! I have some other questions too, which I will post shortly. – Subhamoy S. Feb 11 '12 at 17:18
  • I also have played with simplecv and opencv. I find cv2 is very fast indeed. faster than both of the other two. Also simplecv uses an older lib and is based on cv. It is wrapped nicely though. IMHO use cv2. Numpy is very fast. As fast as C++ in all my test cases and very sophisticated operations are possible easily in numpy. I suggest searching stackoverlfow for cv2 and referencing those solutions as an intial step. – Neon22 Apr 03 '12 at 02:31
  • I'm currently working on making SimpleCV image compatible with cv2. But I'm stuck with pygame surface. If you know anything about it, please post it here. – Froyo Jul 10 '12 at 10:41

2 Answers2

2

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

Community
  • 1
  • 1
Froyo
  • 17,947
  • 8
  • 45
  • 73
2

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])
Anthony
  • 1,513
  • 11
  • 17