def PIL2Ipl(input):
        """Converts a PIL image to the OpenCV/IPL CvMat data format.
  
      Supported input image formats are:
          RGB
          L
          F
      """

        if not (isinstance(input, PIL.Image.Image)):
            raise TypeError, 'Must be called with PIL.Image.Image!'

        # mode dictionary:
        # (pil_mode : (ipl_depth, ipl_channels)
        mode_list = {
            "RGB": (cv.IPL_DEPTH_8U, 3),
            "L": (cv.IPL_DEPTH_8U, 1),
            "F": (cv.IPL_DEPTH_32F, 1)
        }

        if not mode_list.has_key(input.mode):
            raise ValueError, 'unknown or unsupported input mode'

        result = cv.cvCreateImage(
            cv.cvSize(input.size[0], input.size[1]),  # size
            mode_list[input.mode][0],  # depth
            mode_list[input.mode][1]  # channels
        )

        # set imageData
        result.imageData = input.tostring()
        return result
Example #2
0
 def PIL2Ipl(input):
     """Converts a PIL image to the OpenCV/IPL CvMat data format.
 
     Supported input image formats are:
         RGB
         L
         F
     """
 
     if not (isinstance(input, PIL.Image.Image)):
         raise TypeError, 'Must be called with PIL.Image.Image!'
     
     # mode dictionary:
     # (pil_mode : (ipl_depth, ipl_channels)
     mode_list = {
         "RGB" : (cv.IPL_DEPTH_8U, 3),
         "L"   : (cv.IPL_DEPTH_8U, 1),
         "F"   : (cv.IPL_DEPTH_32F, 1)
         }
     
     if not mode_list.has_key(input.mode):
         raise ValueError, 'unknown or unsupported input mode'
     
     result = cv.cvCreateImage(
         cv.cvSize(input.size[0], input.size[1]),  # size
         mode_list[input.mode][0],  # depth
         mode_list[input.mode][1]  # channels
         )
 
     # set imageData
     result.imageData = input.tostring()
     return result
Example #3
0
    def ellipse(self, dx, dy, width, height, color = (0, 255, 0), border = -1, line = 8):
        """Draw a ellipse on image"""
        rotation = 0
        start_angle = 0
        stop_angle = 360
        shift = 0

        cv.Ellipse(self.frame,
            cv.cvPoint (dx, dy),
            cv.cvSize(width, height),
            rotation, start_angle, stop_angle, color, border, line, shift)
    def NumPy2Ipl(input):
        """Converts a numpy array to the OpenCV/IPL CvMat data format.
  
      Supported input array layouts:
         2 dimensions of numpy.uint8
         3 dimensions of numpy.uint8
         2 dimensions of numpy.float32
         2 dimensions of numpy.float64
      """

        if not isinstance(input, numpy.ndarray):
            raise TypeError, 'Must be called with numpy.ndarray!'

        # Check the number of dimensions of the input array
        ndim = input.ndim
        if not ndim in (2, 3):
            raise ValueError, 'Only 2D-arrays and 3D-arrays are supported!'

        # Get the number of channels
        if ndim == 2:
            channels = 1
        else:
            channels = input.shape[2]

        # Get the image depth
        if input.dtype == numpy.uint8:
            depth = cv.IPL_DEPTH_8U
        elif input.dtype == numpy.float32:
            depth = cv.IPL_DEPTH_32F
        elif input.dtype == numpy.float64:
            depth = cv.IPL_DEPTH_64F

        # supported modes list: [(channels, dtype), ...]
        modes_list = [(1, numpy.uint8), (3, numpy.uint8), (1, numpy.float32),
                      (1, numpy.float64)]

        # Check if the input array layout is supported
        if not (channels, input.dtype) in modes_list:
            raise ValueError, 'Unknown or unsupported input mode'

        result = cv.cvCreateImage(
            cv.cvSize(input.shape[1], input.shape[0]),  # size
            depth,  # depth
            channels  # channels
        )

        # set imageData
        result.imageData = input.tostring()

        return result
Example #5
0
 def NumPy2Ipl(input):
     """Converts a numpy array to the OpenCV/IPL CvMat data format.
 
     Supported input array layouts:
        2 dimensions of numpy.uint8
        3 dimensions of numpy.uint8
        2 dimensions of numpy.float32
        2 dimensions of numpy.float64
     """
     
     if not isinstance(input, numpy.ndarray):
         raise TypeError, 'Must be called with numpy.ndarray!'
 
     # Check the number of dimensions of the input array
     ndim = input.ndim
     if not ndim in (2, 3):
         raise ValueError, 'Only 2D-arrays and 3D-arrays are supported!'
     
     # Get the number of channels
     if ndim == 2:
         channels = 1
     else:
         channels = input.shape[2]
     
     # Get the image depth
     if input.dtype == numpy.uint8:
         depth = cv.IPL_DEPTH_8U
     elif input.dtype == numpy.float32:
         depth = cv.IPL_DEPTH_32F
     elif input.dtype == numpy.float64:
         depth = cv.IPL_DEPTH_64F
     
     # supported modes list: [(channels, dtype), ...]
     modes_list = [(1, numpy.uint8), (3, numpy.uint8), (1, numpy.float32), (1, numpy.float64)]
     
     # Check if the input array layout is supported
     if not (channels, input.dtype) in modes_list:
         raise ValueError, 'Unknown or unsupported input mode'
     
     result = cv.cvCreateImage(
         cv.cvSize(input.shape[1], input.shape[0]),  # size
         depth,  # depth
         channels  # channels
         )
     
     # set imageData
     result.imageData = input.tostring()
     
     return result
def Ipl2NumPy(input):
    """Converts an OpenCV image to a numpy array.

    Supported input image formats are
    IPL_DEPTH_8U  x 1 channel
    IPL_DEPTH_8U  x 3 channels
    IPL_DEPTH_32F x 1 channel
    IPL_DEPTH_32F x 2 channels
    IPL_DEPTH_32S x 1 channel
    IPL_DEPTH_64F x 1 channel
    IPL_DEPTH_64F x 2 channels
    """
    import ipdb
    ipdb.set_trace()

    if not isinstance(input, cv.CvMat):
        raise TypeError, 'must be called with a cv.CvMat!'

    # data type dictionary:
    # (channels, depth) : numpy dtype
    ipl2dtype = {
        (1, cv.IPL_DEPTH_8U): numpy.uint8,
        (3, cv.IPL_DEPTH_8U): numpy.uint8,
        (1, cv.IPL_DEPTH_32F): numpy.float32,
        (2, cv.IPL_DEPTH_32F): numpy.float32,
        (1, cv.IPL_DEPTH_32S): numpy.int32,
        (1, cv.IPL_DEPTH_64F): numpy.float64,
        (2, cv.IPL_DEPTH_64F): numpy.float64
    }

    key = (input.nChannels, input.depth)
    if not ipl2dtype.has_key(key):
        raise ValueError, 'unknown or unsupported input mode'

    # Get the numpy array and reshape it correctly
    # ATTENTION: flipped dimensions width/height on 2007-11-15
    if input.nChannels == 1:
        array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
        return numpy.reshape(array_1d, (input.height, input.width))
    elif input.nChannels == 2:
        array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
        return numpy.reshape(array_1d, (input.height, input.width, 2))
    elif input.nChannels == 3:
        # Change the order of channels from BGR to RGB
        rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height),
                               input.depth, 3)
        cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB)
        array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key])
        return numpy.reshape(array_1d, (input.height, input.width, 3))
def Ipl2NumPy(input):
    """Converts an OpenCV image to a numpy array.

    Supported input image formats are
    IPL_DEPTH_8U  x 1 channel
    IPL_DEPTH_8U  x 3 channels
    IPL_DEPTH_32F x 1 channel
    IPL_DEPTH_32F x 2 channels
    IPL_DEPTH_32S x 1 channel
    IPL_DEPTH_64F x 1 channel
    IPL_DEPTH_64F x 2 channels
    """
    import ipdb

    ipdb.set_trace()

    if not isinstance(input, cv.CvMat):
        raise TypeError, "must be called with a cv.CvMat!"

    # data type dictionary:
    # (channels, depth) : numpy dtype
    ipl2dtype = {
        (1, cv.IPL_DEPTH_8U): numpy.uint8,
        (3, cv.IPL_DEPTH_8U): numpy.uint8,
        (1, cv.IPL_DEPTH_32F): numpy.float32,
        (2, cv.IPL_DEPTH_32F): numpy.float32,
        (1, cv.IPL_DEPTH_32S): numpy.int32,
        (1, cv.IPL_DEPTH_64F): numpy.float64,
        (2, cv.IPL_DEPTH_64F): numpy.float64,
    }

    key = (input.nChannels, input.depth)
    if not ipl2dtype.has_key(key):
        raise ValueError, "unknown or unsupported input mode"

    # Get the numpy array and reshape it correctly
    # ATTENTION: flipped dimensions width/height on 2007-11-15
    if input.nChannels == 1:
        array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
        return numpy.reshape(array_1d, (input.height, input.width))
    elif input.nChannels == 2:
        array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
        return numpy.reshape(array_1d, (input.height, input.width, 2))
    elif input.nChannels == 3:
        # Change the order of channels from BGR to RGB
        rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height), input.depth, 3)
        cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB)
        array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key])
        return numpy.reshape(array_1d, (input.height, input.width, 3))
 def test05_Ipl2NumPy(self):
     """Test the adaptors.Ipl2NumPy function."""
 
     a = adaptors.Ipl2NumPy(self.ipl_image)
     a_1d = numpy.reshape(a, (a.size, ))
     # For 3-channel IPL images  the order of channels will be BGR
     # but NumPy array order of channels will be RGB so a conversion
     # is needed before we can compare both images
     if self.ipl_image.nChannels == 3:
         rgb = cv.cvCreateImage(cv.cvSize(self.ipl_image.width, self.ipl_image.height), self.ipl_image.depth, 3)
         cv.cvCvtColor(self.ipl_image, rgb, cv.CV_BGR2RGB)
         self.assert_(a_1d.tostring() == rgb.imageData,
             'The returned image has not been properly constructed.')
     else:
         self.assert_(a_1d.tostring() == self.ipl_image.imageData,
             'The returned image has not been properly constructed.')
Example #9
0
    def ellipse(self,
                dx,
                dy,
                width,
                height,
                color=(0, 255, 0),
                border=-1,
                line=8):
        """Draw a ellipse on image"""
        rotation = 0
        start_angle = 0
        stop_angle = 360
        shift = 0

        cv.Ellipse(self.frame, cv.cvPoint(dx, dy), cv.cvSize(width,
                                                             height), rotation,
                   start_angle, stop_angle, color, border, line, shift)
Example #10
0
 def Ipl2NumPy(input):
     """Converts an OpenCV/IPL image to a numpy array.
 
     Supported input image formats are
        IPL_DEPTH_8U  x 1 channel
        IPL_DEPTH_8U  x 3 channels
        IPL_DEPTH_32F x 1 channel
        IPL_DEPTH_64F x 1 channel
     """
 
     if not isinstance(input, cv.CvMat):
         raise TypeError, 'must be called with a cv.CvMat!'
 
     # assert that the channels are interleaved
     if input.dataOrder != 0:
         raise ValueError, 'dataOrder must be 0 (interleaved)!'
 
     # data type dictionary:
     # (channels, depth) : numpy dtype
     ipl2dtype = {
         (1, cv.IPL_DEPTH_8U)  : numpy.uint8,
         (3, cv.IPL_DEPTH_8U)  : numpy.uint8,
         (1, cv.IPL_DEPTH_32F) : numpy.float32,
         (1, cv.IPL_DEPTH_64F) : numpy.float64
         }
 
     key = (input.nChannels, input.depth)
     if not ipl2dtype.has_key(key):
         raise ValueError, 'unknown or unsupported input mode'
 
     # Get the numpy array and reshape it correctly
     if input.nChannels == 1:
         array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
         return numpy.reshape(array_1d, (input.height, input.width))
     elif input.nChannels == 3:
         # Change the order of channels from BGR to RGB
         rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height), input.depth, 3)
         cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB)
         array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key])
         return numpy.reshape(array_1d, (input.height, input.width, 3))
Example #11
0
def logPolar(im,center=None,radius=None,M=None,size=(64,128)):
    '''
    Produce a log polar transform of the image.  See OpenCV for details.
    The scale space is calculated based on radius or M.  If both are given 
    M takes priority.
    '''
    #M=1.0
    w,h = im.size
    if radius == None:
        radius = 0.5*min(w,h)
        
    if M == None:
        #rho=M*log(sqrt(x2+y2))
        #size[0] = M*log(r)
        M = size[0]/np.log(radius)

    if center == None:
        center = pv.Point(0.5*w,0.5*h)
    src = im.asOpenCV()
    dst = cv.cvCreateImage( cv.cvSize(size[0],size[1]), 8, 3 );
    cv.cvLogPolar( src, dst, center.asOpenCV(), M, cv.CV_INTER_LINEAR+cv.CV_WARP_FILL_OUTLIERS )
    return pv.Image(dst)
Example #12
0
	def initialize_video(self):

		webcam_frame = cv.QueryFrame( self.capture )

		if not webcam_frame:
			print "Frame acquisition failed."
			return False

		self.webcam_pixbuf = gtk.gdk.pixbuf_new_from_data(
			webcam_frame.imageData,
			gtk.gdk.COLORSPACE_RGB,
			False,
			8,
			webcam_frame.width,
			webcam_frame.height,
			webcam_frame.widthStep)
		self.video_image.set_from_pixbuf(self.webcam_pixbuf)


                self.display_frame = cv.cvCreateImage( cv.cvSize(webcam_frame.width, webcam_frame.height), cv.IPL_DEPTH_8U, 3)

		return True