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
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
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]
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
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))
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
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
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 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)
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
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 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
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
import socket from io import StringIO from PIL import Image import cv2 sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.bind(("127.0.0.1", 10000)) sock.listen(2) src,src_addr = sock.accept() i = 0; while True: msg = src.recv(1024*1024) if not msg: break img = cv2.CreateImageHeader((640, 480), cv2.IPL_DEPTH_8U, 3)) cv2.imshow("camera",img) if (cv2.waitKey(1) & 0xFF) == ord('q'): break sock.close() cv2.DestoryAllWindows()
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()
# Get all data from the socket 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)
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