Exemplo n.º 1
0
def cv2np(im, format='RGB'):
	if format == 'BGR':
		cv.cvCvtColor( im, im, cv.CV_BGR2RGB )
	numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(im)]
	array_size = [im.height, im.width, nchannels]
	np_im = np.frombuffer(im.imageData, dtype=numpy_type, 
            count=im.height*im.width*nchannels*(im.depth/8))
	return np.reshape(np_im, array_size)
Exemplo n.º 2
0
def cv2np(im, format='RGB'):
    if format == 'BGR':
        cv.cvCvtColor(im, im, cv.CV_BGR2RGB)
    numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(im)]
    array_size = [im.height, im.width, nchannels]
    np_im = np.frombuffer(im.imageData,
                          dtype=numpy_type,
                          count=im.height * im.width * nchannels *
                          (im.depth / 8))
    return np.reshape(np_im, array_size)
Exemplo n.º 3
0
def cv2np(im, format='RGB'):
	if format == 'BGR':
		cv.cvCvtColor( im, im, cv.CV_BGR2RGB )
	numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(im)]
	array_size = [im.height, im.width, nchannels]
    #Removed multiplication of size by (im.depth/8) as numpy takes
    #into account of that in numpy_type
	np_im = np.frombuffer(im.imageData, dtype=numpy_type, 
            count=im.height*im.width*nchannels)
	return np.reshape(np_im, array_size)
Exemplo n.º 4
0
def cv2np(im, format='RGB'):
    if format == 'BGR':
        cv.cvCvtColor(im, im, cv.CV_BGR2RGB)
    numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(im)]
    array_size = [im.height, im.width, nchannels]
    #Removed multiplication of size by (im.depth/8) as numpy takes
    #into account of that in numpy_type
    np_im = np.frombuffer(im.imageData,
                          dtype=numpy_type,
                          count=im.height * im.width * nchannels)
    return np.reshape(np_im, array_size)
Exemplo n.º 5
0
 def np2cv(im):
     print 'WARNING: np2cv is not reliable or well tested (it is a bit flakey...)'
     #raise AssertionError('np2cv does not work :-(')
     if len(im.shape) == 3:
         shp = im.shape
         channels = shp[2]
         height = shp[0]
         width = shp[1]
         #height, width, channels = im.shape
     elif len(im.shape) == 2:
         height, width = im.shape
         channels = 1
     else:
         raise AssertionError(
             "unrecognized shape for the input image. should be 3 or 2, but was %d."
             % len(im.shape))
     key = str(im.dtype)
     cv_type = np2cv_type_dict[key]
     print 'attempt to create opencv image with (key, width, height, channels) =', (
         key, width, height, channels)
     cv_im = cv.cvCreateImage(cv.cvSize(width, height), cv_type, channels)
     #cv_im.imageData = im.tostring()
     if True:
         if len(im.shape) == 3:
             for y in xrange(height):
                 for x in xrange(width):
                     pix = [float(v) for v in im[y, x]]
                     scalar = cv.cvScalar(*pix)
                     #print scalar
                     cv_im[y, x] = scalar
         else:
             for y in xrange(height):
                 for x in xrange(width):
                     pix = float(im[y, x])
                     cv_im[y, x] = cv.cvScalar(pix, pix, pix)
                     #print 'im[y,x], cv_im[y,x] =', im[y,x], cv_im[y,x]
     print 'resulted in an image openCV image with the following properties:'
     numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(cv_im)]
     print '(numpy_type, nchannels, cvmat.width, cvmat.height) =', (
         numpy_type, nchannels, cv_im.width, cv_im.height)
     return cv_im
Exemplo n.º 6
0
	def np2cv(im):
		print 'WARNING: np2cv is not reliable or well tested (it is a bit flakey...)'
		#raise AssertionError('np2cv does not work :-(')
		if len(im.shape) == 3:
			shp = im.shape
			channels = shp[2]
			height = shp[0]
			width = shp[1]
			#height, width, channels = im.shape
		elif len(im.shape) == 2:
			height, width = im.shape
			channels = 1
		else:
			raise AssertionError("unrecognized shape for the input image. should be 3 or 2, but was %d." % len(im.shape))
		key = str(im.dtype)
		cv_type = np2cv_type_dict[key]
		print 'attempt to create opencv image with (key, width, height, channels) =', (key, width, height, channels)
		cv_im = cv.cvCreateImage(cv.cvSize(width, height), cv_type, channels)
		#cv_im.imageData = im.tostring()
		if True:
			if len(im.shape) == 3:
				for y in xrange(height):
					for x in xrange(width):
						pix = [float(v) for v in im[y,x]]
						scalar = cv.cvScalar(*pix)
						#print scalar
						cv_im[y,x] = scalar
			else:
				for y in xrange(height):
					for x in xrange(width):
						pix = float(im[y,x])
						cv_im[y,x] = cv.cvScalar(pix, pix, pix)
						#print 'im[y,x], cv_im[y,x] =', im[y,x], cv_im[y,x]
		print 'resulted in an image openCV image with the following properties:'
		numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(cv_im)]
		print '(numpy_type, nchannels, cvmat.width, cvmat.height) =', (numpy_type, nchannels, cv_im.width, cv_im.height)
		return cv_im