def pil_to_ipl(im_pil): im_ipl = cvCreateImageHeader(cvSize(im_pil.size[0], im_pil.size[1]), IPL_DEPTH_8U, 3) data = im_pil.tostring('raw', 'RGB', im_pil.size[0] * 3) cvSetData(im_ipl, cast(data, POINTER(c_byte)), im_pil.size[0] * 3) cvCvtColor(im_ipl, im_ipl, CV_RGB2BGR) im_ipl._depends = (data, ) return im_ipl
def __init__(self, parent, img): wx.Panel.__init__(self, parent, -1) # Convert the raw image data to something wxpython can handle. cv.cvCvtColor(img, img, cv.CV_BGR2RGB) self.bmp = wx.BitmapFromBuffer(img.width, img.height,\ img.imageData) # Display the resulting image sbmp = wx.StaticBitmap(self, -1, bitmap=self.bmp)
def pil_to_ipl(im_pil): im_ipl = cvCreateImageHeader(cvSize(im_pil.size[0], im_pil.size[1]), IPL_DEPTH_8U, 3) data = im_pil.tostring('raw', 'RGB', im_pil.size[0] * 3) cvSetData(im_ipl, cast(data, POINTER(c_byte)), im_pil.size[0] * 3) cvCvtColor(im_ipl, im_ipl, CV_RGB2BGR) im_ipl._depends = (data,) return im_ipl
def run(self): if self.capture: webcam_frame = cv.QueryFrame( self.capture ) else: print "Capture failed!" return if self.inverted_video.get_active(): cv.ConvertImage(webcam_frame, webcam_frame, cv._CVTIMG_FLIP) cv.ConvertImage(webcam_frame, self.display_frame, cv._CVTIMG_SWAP_RB) if False: # PROCESS WEBCAM FRAME HERE... inputImage = cv.cvCreateImage(cv.cvGetSize(webcam_frame), cv.IPL_DEPTH_8U, 1) cv.cvCvtColor(webcam_frame, inputImage, cv.CV_RGB2GRAY); cv.cvThreshold(inputImage, inputImage, 128, 255, cv.CV_THRESH_BINARY) mysize = cv.cvGetSize(webcam_frame) height = mysize.height width = mysize.width # Find horizontal first-moment: if False: mysum = 0 for i in range(height): mysum += sum(inputImage[i,:]) print "Sum:", mysum cv.cvMerge( inputImage, inputImage, inputImage, None, self.display_frame ) incoming_pixbuf = gtk.gdk.pixbuf_new_from_data( self.display_frame.imageData, gtk.gdk.COLORSPACE_RGB, False, 8, self.display_frame.width, self.display_frame.height, self.display_frame.widthStep) incoming_pixbuf.copy_area(0, 0, self.display_frame.width, self.display_frame.height, self.webcam_pixbuf, 0, 0) self.video_image.queue_draw() return self.video_enabled_button.get_active()
def pil_to_ipl(im_pil): """Converts a PIL.Image into an IplImage This function may be obsolete. Use cvCreateImageFromPilImage() instead. """ im_ipl = cvCreateImageHeader(cvSize(im_pil.size[0], im_pil.size[1]), IPL_DEPTH_8U, 3) data = im_pil.tostring('raw', 'RGB', im_pil.size[0] * 3) cvSetData(im_ipl, cast(data, POINTER(c_byte)), im_pil.size[0] * 3) cvCvtColor(im_ipl, im_ipl, CV_BGR2RGB) im_ipl._depends = (data,) return im_ipl
def pil_to_ipl(im_pil): """Converts a PIL.Image into an IplImage This function may be obsolete. Use cvCreateImageFromPilImage() instead. """ im_ipl = cvCreateImageHeader(cvSize(im_pil.size[0], im_pil.size[1]), IPL_DEPTH_8U, 3) data = im_pil.tostring('raw', 'RGB', im_pil.size[0] * 3) cvSetData(im_ipl, cast(data, POINTER(c_byte)), im_pil.size[0] * 3) cvCvtColor(im_ipl, im_ipl, CV_BGR2RGB) im_ipl._depends = (data,) return im_ipl
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.')
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))