def pil2cvGrey(pil_im):
    # Convert a PIL image to a greyscale cv image
    # from: http://pythonpath.wordpress.com/2012/05/08/pil-to-opencv-image/
    pil_im = pil_im.convert('L')
    cv_im = cv2.CreateImageHeader(pil_im.size, cv2.IPL_DEPTH_8U, 1)
    cv2.SetData(cv_im, pil_im.tostring(), pil_im.size[0])
    return cv_im
예제 #2
0
def DoNiceConvertRGB(video):

    video = video[:, :, ::-1]  # RGB -> BGR
    image = cv.CreateImageHeader((video.shape[1], video.shape[0]),
                                 cv.IPL_DEPTH_8U, 3)
    cv.SetData(image, video.tostring(),
               video.dtype.itemsize * 3 * video.shape[1])
    return image
예제 #3
0
def faces_from_pil_image(pil_image):
    "Return a list of (x,y,h,w) tuples for faces detected in the PIL image"
    storage = cv2.CvMemStorage(0)
    facial_features = cv2.Load('haarcascade_frontalface_alt.xml', storage=storage)
    cv_im = cv2.CreateImageHeader(pil_image.size, cv.IPL_DEPTH_8U, 3)
    cv2.SetData(cv_im, pil_image.tostring())
    faces = cv2.HaarDetectObjects(cv2_im, facial_features, storage)
    # faces includes a `neighbors` field that we aren't going to use here
    return [f[0] for f in faces]
예제 #4
0
def DoNiceConvert8(depth):

    np.clip(depth, 0, 2**10 - 1, depth)
    depth >>= 2
    depth = depth.astype(np.uint8)

    image = cv.CreateImageHeader((depth.shape[1], depth.shape[0]),
                                 cv.IPL_DEPTH_8U, 1)
    cv.SetData(image, depth.tostring(), depth.dtype.itemsize * depth.shape[1])
    return image
예제 #5
0
def show_threshold():
    global threshold
    global current_depth

    depth, timestamp = freenect.sync_get_depth()
    depth = 255 * np.logical_and(depth >= current_depth - threshold,
                                 depth <= current_depth + threshold)
    depth = depth.astype(np.uint8)
    threshold_image = cv.CreateImageHeader((depth.shape[1], depth.shape[0]),
                                 cv.IPL_DEPTH_8U,
                                 1)
    cv.SetData(threshold_image, depth.tostring(),
              depth.dtype.itemsize * depth.shape[1])
    cv.ShowImage('Threshold', resize_image(threshold_image))
예제 #6
0
def npArray2cvMat(inputMat, dataType=cv.CV_32FC1):
    """
    This function is a utility for converting numpy arrays to the cv.cvMat format.

    Returns: cvMatrix
    """
    if (type(inputMat) == np.ndarray):
        sz = len(inputMat.shape)
        temp_mat = None
        if (dataType == cv.CV_32FC1 or dataType == cv.CV_32FC2
                or dataType == cv.CV_32FC3 or dataType == cv.CV_32FC4):
            temp_mat = np.array(inputMat, dtype='float32')
        elif (dataType == cv.CV_8UC1 or dataType == cv.CV_8UC2
              or dataType == cv.CV_8UC3 or dataType == cv.CV_8UC3):
            temp_mat = np.array(inputMat, dtype='uint8')
        else:
            logger.warning(
                "MatrixConversionUtil: the input matrix type is not supported")
            return None
        if (sz == 1):  #this needs to be changed so we can do row/col vectors
            retVal = cv.CreateMat(inputMat.shape[0], 1, dataType)
            cv.SetData(retVal, temp_mat.tostring(),
                       temp_mat.dtype.itemsize * temp_mat.shape[0])
        elif (sz == 2):
            retVal = cv.CreateMat(temp_mat.shape[0], temp_mat.shape[1],
                                  dataType)
            cv.SetData(retVal, temp_mat.tostring(),
                       temp_mat.dtype.itemsize * temp_mat.shape[1])
        elif (sz > 2):
            logger.warning(
                "MatrixConversionUtil: the input matrix type is not supported")
            return None
        return retVal
    else:
        logger.warning(
            "MatrixConversionUtil: the input matrix type is not supported")
예제 #7
0
def pretty_depth_cv(depth):
    """Converts depth into a 'nicer' format for display

    This is abstracted to allow for experimentation with normalization

    Args:
        depth: A numpy array with 2 bytes per pixel

    Returns:
        An opencv image who's datatype is unspecified
    """
    import cv2
    depth = pretty_depth(depth)
    image = cv2.CreateImageHeader((depth.shape[1], depth.shape[0]),
                                  cv2.IPL_DEPTH_8U, 1)
    cv2.SetData(image, depth.tostring(), depth.dtype.itemsize * depth.shape[1])
    return image
예제 #8
0
def video_cv(video):
    """Converts video into a BGR format for opencv

    This is abstracted out to allow for experimentation

    Args:
        video: A numpy array with 1 byte per pixel, 3 channels RGB

    Returns:
        An opencv image who's datatype is 1 byte, 3 channel BGR
    """
    import cv2
    video = video[:, :, ::-1]  # RGB -> BGR
    image = cv2.CreateImageHeader((video.shape[1], video.shape[0]),
                                  cv2.IPL_DEPTH_8U, 3)
    cv2.SetData(image, video.tostring(),
                video.dtype.itemsize * 3 * video.shape[1])
    return image
예제 #9
0
def array2cv(a): 
  dtype2depth = { 
        'uint8':   cv.IPL_DEPTH_8U, 
        'int8':    cv.IPL_DEPTH_8S, 
        'uint16':  cv.IPL_DEPTH_16U, 
        'int16':   cv.IPL_DEPTH_16S, 
        'int32':   cv.IPL_DEPTH_32S, 
        'float32': cv.IPL_DEPTH_32F, 
        'float64': cv.IPL_DEPTH_64F, 
    } 
  try: 
    nChannels = a.shape[2] 
  except: 
    nChannels = 1 
  cv_im = cv.CreateImageHeader((a.shape[1],a.shape[0]), 
dtype2depth[str(a.dtype)], nChannels) 
  cv.SetData(cv_im, a.tostring(),a.dtype.itemsize*nChannels*a.shape[1]) 
  return cv_im 
def rotateImage(image, angle):
    image0 = image
    if hasattr(image, 'shape'):
        image_center = tuple(np.array(image.shape) / 2)
        shape = tuple(image.shape)
    elif hasattr(image, 'width') and hasattr(image, 'height'):
        image_center = tuple(np.array((image.width / 2, image.height / 2)))
        shape = (image.width, image.height)
    else:
        pass
    rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
    image = np.asarray(image[:, :])

    rotated_image = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR)

    # Copy the rotated data back into the original image object.
    cv2.SetData(image0, rotated_image.tostring())

    return image0
예제 #11
0
def nao_camera_calibration(robotIP, port):
    if port == None:
        port = 9559

    width = 160
    height = 120
    raw_img = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 3)

    videoProxy = ALProxy("ALVideoDevice", robotIP, port)
    resolution = vision_definitions.kQQVGA
    colorSpace = vision_definitions.kRGBColorSpace
    fps = 30
    img_client = videoProxy.subscribe("calib_client", resolution, colorSpace, fps)
    videoProxy.setActiveCamera("_client", 1)

    alImg = video_proxy.getImageRemote(img_client)
    rgb_img = cv.CreateImageHeader((width, height), cv.IPL_DEPTH_8U, 3)
    cv.SetData(rgb_img, alImg[6])
    cv.CvtColor(rgb_img, raw_img, cv.CV_RGB2BGR)

    videoProxy.unsubscribe(img_client)
예제 #12
0
    def request_image(self, source):
        # Request the new image
        start = time.time()
        self.conn.sendall({"get_image": source})

        data = self.conn.wait_data(0.15)
        for meta, img_string in data:
            if 'error' in meta:
                self.logger.warn("Obtaining image failed. Error message: %s" %
                                 meta['error'])
                continue
            shape = meta['shape']
            nchannels = meta['nChannels']
            depth = meta['depth']
            mtime = meta['time']
            img = cv.CreateImageHeader(shape, depth, nchannels)
            cv.SetData(img, img_string)
            took = time.time() - start
            return mtime, img

        took = time.time() - start
        return None, None
예제 #13
0
def yarp_get():
    img_array = numpy.ones((480, 640, 3), dtype=numpy.uint8)

    source = img_array
    bitmap = cv.CreateImageHeader((source.shape[1], source.shape[0]),
                                  cv.IPL_DEPTH_8U, 3)
    cv.SetData(bitmap, source.tostring(),
               source.dtype.itemsize * 3 * source.shape[1])
    img_array = bitmap

    yarp_image = yarp.ImageRgb()
    yarp_image.resize(640, 480)
    yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0])
    #    print img_array.__array_interface__['data'][0]
    #yarp_image.setExternal(img_array.__array_interface__['data'][0], img_array.shape[1], img_array.shape[0])

    input_port.read(yarp_image)

    #    print img_array.getIplImage()
    plt.imshow(img_array)
    #plt.show()
    return img_array
def toCVMat(im, channels):
    image = cv2.CreateImage((im.shape[1], im.shape[0]), cv2.IPL_DEPTH_8U,
                            channels)
    cv2.SetData(image, im.tostring(),
                im.dtype.itemsize * channels * im.shape[1])
    return image
예제 #15
0
def DoNiceConvert11(depth):

    image = cv.CreateImageHeader((depth.shape[1], depth.shape[0]),
                                 cv.IPL_DEPTH_16U, 1)
    cv.SetData(image, depth.tostring(), depth.dtype.itemsize * depth.shape[1])
    return image
예제 #16
0
import socket, time
import Image, StringIO
import numpy as np

HOST, PORT = "10.0.1.13", 9996
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT)) 

f = sock.makefile()

cv2.NamedWindow("camera_server")

while True:
	msg = f.readline()
	if not msg:
		break
	jpeg = msg.replace("\\-n", "\n")
	buf = StringIO.StringIO(jpeg[0:-1])
	buf.seek(0)
	pi = Image.open(buf)
	img = cv2.CreateImageHeader((320, 240), cv.IPL_DEPTH_8U, 3)
	cv2.SetData(img, pi.tostring())
	buf.close()
	frame_cvmat=cv2.GetMat(img)
	frame=np.asarray(frame_cvmat)
	cv2.imshow('frame',frame)
	if cv2.waitKey(1) && 0xFF == ord('q'):
		break

sock.close()
cv2.DistoryAllWindows()
예제 #17
0
def pygame_to_cv2image(surface):
    """Convert a pygame surface into a cv2 image"""
    cv2_image = cv2.CreateImageHeader(surface.get_size(), cv2.IPL_DEPTH_8U, 3)
    image_string = surface_to_string(surface)
    cv2.SetData(cv2_image, image_string)
    return cv2_image
예제 #18
0
파일: Morphw.py 프로젝트: nipponnp/aboutme
def Dilation(pos):
    element = cv.CreateStructuringElementEx(pos * 2 + 1, pos * 2 + 1, pos, pos,
                                            element_shape)
    cv.Dilate(src, dest, element, 1)
    cv.ShowImage("Erosion & Dilation", dest)


if __name__ == "__main__":
    if len(sys.argv) > 1:
        src = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_COLOR)
    else:
        url = 'https://code.ros.org/svn/opencv/trunk/opencv/samples/c/fruits.jpg'
        filedata = urllib2.urlopen(url).read()
        imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
        cv.SetData(imagefiledata, filedata, len(filedata))
        src = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)

    image = cv.CloneImage(src)
    dest = cv.CloneImage(src)
    cv.NamedWindow("Opening & Closing", 1)
    cv.NamedWindow("Erosion & Dilation", 1)
    cv.ShowImage("Opening & Closing", src)
    cv.ShowImage("Erosion & Dilation", src)
    cv.CreateTrackbar("Open", "Opening & Closing", 0, 10, Opening)
    cv.CreateTrackbar("Close", "Opening & Closing", 0, 10, Closing)
    cv.CreateTrackbar("Dilate", "Erosion & Dilation", 0, 10, Dilation)
    cv.CreateTrackbar("Erode", "Erosion & Dilation", 0, 10, Erosion)
    cv.WaitKey(0)
    cv.DestroyWindow("Opening & Closing")
    cv.DestroyWindow("Erosion & Dilation")
        data = client.get_data()
        for metadata, binary_data in data:
            # Display the received image
            shape = metadata['shape']
            nchannels = metadata['nChannels']
            depth = metadata['depth']
            digest = metadata['md5']
            h = hashlib.md5()
            h.update(binary_data)
            dig = h.hexdigest()
            if dig == digest:
                print "Correct MD5 sum on binary data: %s" % dig
            else:
                print "Incorrect MD5 sum: %s  (should be %s)" % (dig, digest)
            img = cv.CreateImageHeader(shape, depth, nchannels)
            cv.SetData(img, binary_data)
            cv.ShowImage(name, img)
            cv.WaitKey(30)

        if not server:
            # Send an image to the server
            img = random.choice(images)
            metadata = {
                "shape": (img.width, img.height),
                "nChannels": img.nChannels,
                "depth": img.depth
            }
            binary_data = img.tostring()
            h = hashlib.md5()
            h.update(binary_data)
            metadata['md5'] = h.hexdigest()
예제 #20
0
def pil2cvGrey(pil_im):
    pil_im = pil_im.convert('L')
    cv_im = cv.CreateImageHeader(pil_im.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_im, pil_im.tostring(), pil_im.size[0])
    return cv_im