Morph-M Python - ImportExport to other python librairies

Introduction

Sometimes, you need to convert an image created by morphee to the image format of other libraries. It's possible to make this conversion and then to transform them back to MorpheeImages.

PIL

Morph-M Gray images To PIL...

im = mp.fileRead("testGray.png")
imPIL = pil.new("L",(im.getWxSize(),im.getWySize()))
data =mp.ImageToString(im,1,0,mp.colorType.GRAY)
imPIL.fromstring(data)

... To Morph-M

imMorp=mp.ImCreate(imPIL.size[0],imPIL.size[1],"UINT8")
data = imPIL.tostring()
mp.ImageFromString(imMorp, data, 1, 0, colorType.GRAY)

Morph-M Color images to PIL...

import morphee as mp
import Image as pil
im = mp.fileRead("testRGB.png")
imPIL = pil.new("RGB",(im.getWxSize(),im.getWySize()))
data =mp.ImageToString(im,3,0,mp.colorType.ARGB)
imPIL.fromstring(data)

wxPython

Morph-M Images To WX

import morphee as mp
import wx
im = mp.fileRead("testRGB.png")
imWX = wx.EmptyImage(im.getWxSize(), im.getWySize())
data = mp.ImageToString(im,3,0,mp.colorType.ABGR)
imWX.SetData(data)

Numpy(the replacement for Numeric/numarray)

Morph-M Images To Numpy....

import morphee as mp
import numpy
im = mp.fileRead("testGray.png")
arr = mp.ImageToNumarray(im)

... To Morph-M

imMorp=mp.ImCreate(arr.shape[0],arr.shape[1],"UINT8")
im.fromList( map(int,arr.flat) )

VTK images

Morph-M Images To VTK

import morphee as mp
import vtk
im = mp.fileRead("testGray.png")
str = mp.ImageToString(im,1,0,mp.colorType.GRAY)
x=im.getXSize()
y=im.getYSize()
z=im.getZSize()
imimport = vtk.vtkImageImport()
imimport.CopyImportVoidPointer(str,x*y*z)
imimport.SetDataScalarTypeToUnsignedChar()
imimport.SetDataExtent(0,x-1,0,y-1,0,z-1)
imimport.SetWholeExtent(0,x-1,0,y-1,0,z-1)
imdata = imimport.GetOutput()
imdata.Update()

Qt4 Image

Morph-M To QT4: Gray level images

import morphee as mp
from PyQt4 import QtGui
# create/read the image
im = mp.fileRead("testGray.png")
x=im.getXSize()
y=im.getYSize()
sdata=mp.ImageToString(im,1,(4 - x % 4) % 4,mp.colorType.GRAY)
fmt = QtGui.QImage.Format_Indexed8
img = QtGui.QImage(sdata, x, y, fmt)
# make sure the image is displayed in shades of gray
# (and not with a funky LUT)
img.setColorTable([QtGui.qRgb(i,i,i) for i in range(0,256)])

Morph-M To QT4: Color images

# create/read the color image
im = mp.fileRead("testRGB.png")
# transfering the data to a qtpixmap
sdata = mp.ImageToString(imLUT,4,0,mp.colorType.ARGB)
x=imLUT.getXSize()
y=imLUT.getYSize()
# convert 32bit buffer to a Qt QImage
fmt = QtGui.QImage.Format_ARGB32
img = QtGui.QImage(sdata, x, y, fmt)

OpenCV

Introduction

OpenCV (Open Source Computer vision) is a library of programming functions mainly aimed at real time computer Vision. This library contains Python bindings that we can use easily. Some links:

Reference functions

imOut = ImageFromIplImage( imIn ) : convert a IMorpheeImage from an Ipl or CvMat image
  • imIn : Ipl or CvMat images
  • imOut : Morphee Image Interface
ImageToIplImage(imIn,imOut) : convert a an Ipl or CvMat image from IMorpheeImage
  • imIn : Morphee Image Interface
  • imOut : Ipl or CvMat images
Note:
  • Functions name is the same under python or C++
  • Under python, these functions only accept CvMat Images.

OpenCV and Morph-M under Python

Basic use


# import opencv and Morph-M modules
from opencv.cv import *
from opencv.highgui import *
from morphee import *

cvStartWindowThread()
cvNamedWindow("UnderBuild Retina")

#Structuring Element
nl = SquareSE
nl_cv = cvCreateStructuringElementEx(12, 12,6,6, CV_SHAPE_RECT)

#Load images
imIn = cvLoadImage(images_dir+"/Gray/retina2.png",0)
imTmp=cvCloneImage(imIn)
imOpen=cvCloneImage(imIn)

# -------------------------------
# open with OpenCV...
# -------------------------------
cvMorphologyEx(imIn, imOpen, imTmp, nl_cv, CV_MOP_OPEN)

# ------------------------------
# ...then build with Morph-M
# ------------------------------

#image conversion
imOut=ImageFromIplImage(imTmp)

ImUnderBuild(ImageFromIplImage(imOpen),ImageFromIplImage(imIn),nl,imOut)
fileWrite(imOut,"imBuildOpen.png")

# ------------------------------
# Show Result with OpenCV
# ------------------------------

ImageToIplImage(imOut,imTmp)
cvShowImage("Retina: Original",imIn)
cvShowImage("Retina: Processed",imTmp)
cvWaitKey(0)

cvDestroyWindow("Retina: Original")
cvDestroyWindow("Retina: Processed")

Basic use 2: Working with Video Sequence


# import opencv and Morph-M modules
from opencv.cv import *
from opencv.highgui import *
from morphee import *

#------------------------------
#Initializing a video writer:
#------------------------------ 
isColor = 1
fps     = 25  # or 30
frameW  = 256 # 744 for firewire cameras
frameH  = 256 # 480 for firewire cameras
writer = cvCreateVideoWriter("myRoad_Original.avi",-1, 
                    fps,cvSize(frameW,frameH),isColor)
writer_result = cvCreateVideoWriter("myRoad_Processed.avi",-1,
                           fps,cvSize(frameW,frameH),isColor)

#-----------------------------
#Writing the video file:
#-----------------------------
nFrames = 70;
for i in range(nFrames):
    img = cvLoadImage(images_dir+"/Sequences/route/route_%d.png"%i) 
     # add the frame to the file myRoad_Original.avi    
    cvWriteFrame(writer,img)      

    # ------------------------------
        # Morpho operation with Morph-M
        # ------------------------------

        im_gray = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1)
    cvCvtColor(img,im_gray, CV_BGR2GRAY)

    #allocate images
    imIn=ImageFromIplImage(im_gray)        
    imGrad = getSame(imIn)
    imWat = getSame(imIn)
    imIplWat = cvCloneImage(im_gray)

    # Segmentation with the waterfall algo.
    nl = NeighborList.neighborsHex2D
    ImMorphoGradient(imIn,nl,imGrad)
    ImWaterfallsClassical(imGrad,nl,1,imWat)
    arithInvertImage(imWat,imWat)

    ImageToIplImage(imWat,imIplWat)

    im_processed = cvCloneImage(img)
    cvCvtColor(imIplWat,im_processed, CV_GRAY2BGR)

    # add the frame to the file myRoad_Processed.avi            
        cvWriteFrame(writer_result,im_processed)

cvReleaseVideoWriter(writer)
cvReleaseVideoWriter(writer_result)