def get_frame(self, face_rec = False): image = highgui.cvQueryFrame(self.device) face_matches = False if face_rec: grayscale = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) for cascade in self.haarfiles: matches = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100)) if matches: face_matches = True for i in matches: cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), cv.CV_RGB(0,0,255), 2, 5, 0) image = cv.cvGetMat(image) return (image, face_matches)
def determine_next_position(self, image): self.image = image self.im = array2image(image) self.ipl_im = opencv.adaptors.PIL2Ipl(self.im) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) self.cascade = opencv( '/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml', opencv.cvSize(1, 1)) self.faces = opencv.cvHaarDetectObjects( self.ipl_im, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) if self.faces.total < 1: return None for f in self.faces: print "face detected: %s" % f #row and column are inverted in Opencv self.pos = (f.y, f.x) self.coormin_bbox = (self.pos[0], self.pos[1]) self.coormax_bbox = (self.pos[0] + f.width, self.pos[1] + f.height) self.brightpixel = False return (self.pos, self.coormin_bbox, self.coormax_bbox, self.brightpixel)
def normalise(self, i_ipl_image): #Do the affine transform if self.__rot_mat == None: warped_image = i_ipl_image else: warped_image = cv.cvCreateImage( cv.cvSize(i_ipl_image.width, i_ipl_image.height), 8, 1) cv.cvWarpAffine(i_ipl_image, warped_image, self.__rot_mat) #Crop if self.__roi == None: self.__cropped_image = warped_image else: self.crop(warped_image) #Scale if self.__resize_scale == 1: scaled_image = self.__cropped_image else: w = int(round(self.__cropped_image.width * self.__resize_scale)) h = int(round(self.__cropped_image.height * self.__resize_scale)) scaled_image = cv.cvCreateImage(cv.cvSize(w, h), 8, 1) cv.cvResize(self.__cropped_image, scaled_image, cv.CV_INTER_LINEAR) #Histogram equalisation if self.__equalise_hist: cv.cvEqualizeHist(scaled_image, scaled_image) #Blur if self.__filter_size == 0: smoothed_image = scaled_image else: smoothed_image = cv.cvCreateImage( cv.cvSize(scaled_image.width, scaled_image.height), 8, 1) cv.cvSmooth(scaled_image, smoothed_image, cv.CV_GAUSSIAN, self.__filter_size) return smoothed_image
def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint(int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects( grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def normalise(self, i_ipl_image): #Do the affine transform if self.__rot_mat == None: warped_image = i_ipl_image else: warped_image = cv.cvCreateImage(cv.cvSize(i_ipl_image.width,i_ipl_image.height), 8, 1) cv.cvWarpAffine(i_ipl_image , warped_image, self.__rot_mat ); #Crop if self.__roi == None: self.__cropped_image = warped_image else: self.crop(warped_image) #Scale if self.__resize_scale == 1: scaled_image = self.__cropped_image else: w = int(round( self.__cropped_image.width * self.__resize_scale)) h = int(round( self.__cropped_image.height * self.__resize_scale)) scaled_image = cv.cvCreateImage(cv.cvSize(w, h), 8, 1) cv.cvResize( self.__cropped_image, scaled_image ,cv.CV_INTER_LINEAR) #Histogram equalisation if self.__equalise_hist: cv.cvEqualizeHist(scaled_image,scaled_image) #Blur if self.__filter_size == 0: smoothed_image = scaled_image else: smoothed_image = cv.cvCreateImage(cv.cvSize(scaled_image.width, scaled_image.height), 8, 1) cv.cvSmooth(scaled_image, smoothed_image, cv.CV_GAUSSIAN, self.__filter_size) return smoothed_image
def normalize(img): """Utility function to normalize the image and return the new normalized image using opencv.""" norm = cvCreateImage(cvSize(img.width, img.height), IPL_DEPTH_32F, 1) cvCopy(img, norm) cvNormalize(norm, norm, 1, 0, CV_MINMAX) norm_u = cvCreateImage(cvSize(img.width, img.height), IPL_DEPTH_8U, 1) cvConvertScale(norm, norm_u, 255) return norm_u
def eigen_texture(cv_image, blocksize=8, filtersize=3): gray_image = cv.cvCreateImage(cv.cvSize(cv_image.width, cv_image.height), cv.IPL_DEPTH_8U, 1) eig_tex = cv.cvCreateImage(cv.cvSize(cv_image.width * 6, cv_image.height), cv.IPL_DEPTH_32F, 1) cv.cvCvtColor(cv_image, gray_image, cv.CV_BGR2GRAY) cv.cvCornerEigenValsAndVecs(gray_image, eig_tex, blocksize, filtersize) eig_tex_np = ut.cv2np(eig_tex) eig_tex_np = np.reshape(eig_tex_np, [cv_image.height, cv_image.width, 6]) return eig_tex_np[:, :, 0:2]
def eigen_texture(cv_image, blocksize=8, filtersize=3): gray_image = cv.cvCreateImage(cv.cvSize(cv_image.width, cv_image.height), cv.IPL_DEPTH_8U, 1) eig_tex = cv.cvCreateImage(cv.cvSize(cv_image.width*6, cv_image.height), cv.IPL_DEPTH_32F, 1) cv.cvCvtColor(cv_image, gray_image, cv.CV_BGR2GRAY) cv.cvCornerEigenValsAndVecs(gray_image, eig_tex, blocksize, filtersize) eig_tex_np = ut.cv2np(eig_tex) eig_tex_np = np.reshape(eig_tex_np, [cv_image.height, cv_image.width, 6]) return eig_tex_np[:,:,0:2]
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
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
def __init__(self): conf = Configuration.get() hdir = conf['haar_classifiers_dir'] self.haarfiles = [ cv.cvLoadHaarClassifierCascade(str(hdir + h), cv.cvSize(10, 10)) for h in conf['haar_classifiers'] ] self.camera_devices = glob(conf['cameras_glob'])
def detect(image): # Find out how large the file is, as the underlying C-based code # needs to allocate memory in the following steps image_size = opencv.cvGetSize(image) # create grayscale version - this is also the point where the allegation about # facial recognition being racist might be most true. A caucasian face would have more # definition on a webcam image than an African face when greyscaled. # I would suggest that adding in a routine to overlay edge-detection enhancements may # help, but you would also need to do this to the training images as well. grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage (It is C-based so you need to do this sort of thing) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects - Haar cascade step # In this case, the code uses a frontal_face cascade - trained to spot faces that look directly # at the camera. In reality, I found that no bearded or hairy person must have been in the training # set of images, as the detection routine turned out to be beardist as well as a little racist! cascade = opencv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', opencv.cvSize(1,1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) if faces: for face in faces: # Hmm should I do a min-size check? # Draw a Chartreuse rectangle around the face - Chartruese rocks opencv.cvRectangle(image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # RGB #7FFF00 width=2
def mask(img, img_mask): dim = img.width, img.height depth = img.depth channels = img.nChannels r_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1) g_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1) b_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1) combined = cv.cvCreateImage(cv.cvSize(*dim), depth, 3) cv.cvSplit(img, r_chan, g_chan, b_chan, None) cv.cvAnd(r_chan, img_mask, r_chan) cv.cvAnd(g_chan, img_mask, g_chan) cv.cvAnd(b_chan, img_mask, b_chan) cv.cvMerge(r_chan, g_chan, b_chan, None, combined) return combined
def transformImage(self,im): ''' Transform an image. ''' matrix = pv.NumpyToOpenCV(self.matrix) src = im.asOpenCV() dst = cv.cvCreateImage( cv.cvSize(self.size[0],self.size[1]), cv.IPL_DEPTH_8U, src.nChannels ); cv.cvWarpPerspective( src, dst, matrix) return pv.Image(dst)
def get_cascade(self, cascade_name): self._cached_cascades[ cascade_name] = opencv.cvLoadHaarClassifierCascade( os.path.join(self.cascade_dir, cascade_name), opencv.cvSize(1, 1)) #cascade_name, opencv.cvSize(1,1)) return self._cached_cascades[cascade_name]
def Ipl2Pil(i_image): o_flipped_image = cv.cvCreateImage( cv.cvSize(i_image.width, i_image.height), i_image.depth, i_image.nChannels) cv.cvFlip(i_image, o_flipped_image) o_pil_image = cv.adaptors.Ipl2Pil(o_flipped_image) return o_pil_image
def detectFaces(self): self._faces = [] frame = self._camera.getFrameAsIpl() storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) cascade = opencv.cvLoadHaarClassifierCascade(self._trainedHaar, opencv.cvSize(1, 1)) mugsht = opencv.cvHaarDetectObjects(frame, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(75, 75)) if mugsht: for mug in mugsht: face = [0, 0, 0, 0] face[0], face[1], face[2], face[ 3] = mug.x, mug.y, mug.width, mug.height self._faces.append(face)
def IplGrayToRGB( i_image ): """Convert RGB Ipl image to gray scale, returns copy of image if the format is already right.""" o_image = cv.cvCreateImage( cv.cvSize( i_image.width, i_image.height ), 8 ,3) if (i_image.depth == 8) and (i_image.nChannels == 3) : cv.cvCopy( i_image, o_image ) else: #cv.cvCvtColor(i_image, o_image , cv.CV_BGR2GRAY ) cv.highgui.cvConvertImage( i_image, o_image ) return o_image
def IplGrayToRGB(i_image): """Convert RGB Ipl image to gray scale, returns copy of image if the format is already right.""" o_image = cv.cvCreateImage(cv.cvSize(i_image.width, i_image.height), 8, 3) if (i_image.depth == 8) and (i_image.nChannels == 3): cv.cvCopy(i_image, o_image) else: #cv.cvCvtColor(i_image, o_image , cv.CV_BGR2GRAY ) cv.highgui.cvConvertImage(i_image, o_image) return o_image
def detectFaces( self ): self._faces = [] frame = self._camera.getFrameAsIpl() storage = opencv.cvCreateMemStorage( 0 ) opencv.cvClearMemStorage( storage ) cascade = opencv.cvLoadHaarClassifierCascade( self._trainedHaar, opencv.cvSize( 1, 1 ) ) mugsht = opencv.cvHaarDetectObjects( frame, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize( 75, 75 ) ) if mugsht: for mug in mugsht: face = [ 0, 0, 0, 0 ] face[0], face[1], face[2], face[3] = mug.x, mug.y, mug.width, mug.height self._faces.append( face )
def addFrame(self, i_image ): if self.__videoWriter == None: frame_size = cv.cvSize(i_image.width , i_image.height ) self.__initVideoWriter( frame_size ) if i_image.nChannels == 3: cv.highgui.cvWriteFrame( self.__videoWriter, i_image ) else: img = image_utils.IplGrayToRGB(i_image) cv.highgui.cvWriteFrame( self.__videoWriter, img )
def matchTemplate(image, template): """Utility function to match the template against the image and return a heightmap. The match is made using V_TM_SQDIFF_NORMED, so smaller values in the heightmap indicate closer matches (look for local minima).""" match_hmap = cvCreateImage( cvSize(image.width-template.width+1, image.height-template.height+1), IPL_DEPTH_32F, 1 ) cvMatchTemplate(image, template, match_hmap, CV_TM_SQDIFF_NORMED) return match_hmap
def detectObject(self, classifier): self.grayscale = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 1) opencv.cvCvtColor(self.iplimage, self.grayscale, opencv.CV_BGR2GRAY) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) opencv.cvEqualizeHist(self.grayscale, self.grayscale) try: self.cascade = opencv.cvLoadHaarClassifierCascade( os.path.join(os.path.dirname(__file__), classifier + ".xml"), opencv.cvSize(1, 1)) except: raise AttributeError, "could not load classifier file" self.objects = opencv.cvHaarDetectObjects( self.grayscale, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) return self.objects
def main(): pub = rospy.TopicPub('image', Image) rospy.ready('test_send') image = cv.cvCreateImage(cv.cvSize(640,480), 8, 1) cv.cvSet(image, 133) while not rospy.is_shutdown(): pub.publish(Image(None, 'test', image.width, image.height, 'none', 'mono8', image.imageData)) time.sleep(.033) rospy.spin()
def drawImage(image,h,w,psize): """ Draw the image as a continuous line on a surface h by w "pixels" where each continuous line representation of a pixel in image is represented on the output suface using psize by psize "pixels". @param image an opencv image with at least 3 channels @param h integer representing the hight of the output surface @param w integer representing the width of the output surface @param psize ammount that each pixel in the input image is scaled up """ h = (h/psize)-2 w = (w/psize)-2 size = opencv.cvSize(w,h) scaled = opencv.cvCreateImage(size,8,3) red = opencv.cvCreateImage(size,8,1) blue = opencv.cvCreateImage(size,8,1) green = opencv.cvCreateImage(size,8,1) opencv.cvSplit(scaled,blue,green,red,None) opencv.cvEqualizeHist(red,red) opencv.cvEqualizeHist(green,green) opencv.cvEqualizeHist(blue,blue) opencv.cvMerge(red,green,blue,None,scaled) opencv.cvResize(image,scaled,opencv.CV_INTER_LINEAR) opencv.cvNot(scaled,scaled) # Draw each pixel in the image xr = range(scaled.width) whitespace = 0 for y in range(scaled.height): for x in xr: s = opencv.cvGet2D(scaled,y,x) s = [s[j] for j in range(3)] if (sum(s)/710.0 < 1.0/psize): whitespace = whitespace+psize else: if whitespace is not 0: line(whitespace,6,(xr[0]>0)) whitespace = 0 drawPixel([j/255.0 for j in s],psize,(xr[0]>0)) if whitespace is not 0: line(whitespace,6,(xr[0]>0)) whitespace = 0 line(psize,2) xr.reverse() displayImage(output) events = pygame.event.get() for event in events: if event.type == QUIT: exit()
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 main(): pub = rospy.TopicPub('image', Image) rospy.ready('test_send') image = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1) cv.cvSet(image, 133) while not rospy.is_shutdown(): pub.publish( Image(None, 'test', image.width, image.height, 'none', 'mono8', image.imageData)) time.sleep(.033) rospy.spin()
def tile_images(img_width, img_height, num_width, num_height, images, channels=3): w = img_width * num_width h = img_height * num_height image = cv.cvCreateImage(cv.cvSize(int(w), int(h)), 8, channels) cv.cvSet(image, cv.cvScalar(255,255,255)) while len(images) > 0: try: for y in range(int(num_height)): for x in range(int(num_width)): small_tile = images.pop() img_x = x * img_width img_y = y * img_height cropped = cv.cvGetSubRect(image, cv.cvRect(img_x, img_y, img_width,img_height)) cv.cvCopy(small_tile, cropped) except exceptions.IndexError, e: break
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_32F x 2 channels IPL_DEPTH_32S x 1 channel IPL_DEPTH_64F x 1 channel IPL_DEPTH_64F x 2 channels """ 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/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_32F x 2 channels IPL_DEPTH_32S x 1 channel IPL_DEPTH_64F x 1 channel IPL_DEPTH_64F x 2 channels """ 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 get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) # Resize image if needed resized_im = opencv.cvCreateImage(opencv.cvSize(XSCALE, YSCALE), 8, 3) opencv.cvResize(im, resized_im, opencv.CV_INTER_CUBIC) #convert Ipl image to PIL image pil_img = opencv.adaptors.Ipl2PIL(resized_im) enhancer = ImageEnhance.Brightness(pil_img) pil_img = enhancer.enhance(BRIGHTNESS) pil_img = ImageOps.mirror(pil_img) return pil_img
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) # Resize image if needed resized_im=opencv.cvCreateImage(opencv.cvSize(XSCALE,YSCALE),8,3) opencv.cvResize( im, resized_im, opencv.CV_INTER_CUBIC); #convert Ipl image to PIL image pil_img = opencv.adaptors.Ipl2PIL(resized_im) enhancer=ImageEnhance.Brightness(pil_img) pil_img=enhancer.enhance(BRIGHTNESS) pil_img=ImageOps.mirror(pil_img) return pil_img
def main(argv=None): # declair globals global ser, output, curX, curY, last_dir # Open serial connection ser = serial.Serial(DEFAULT_SERIAL_PORT, 9600, timeout=1) last_dir = [None, None] # Print starting time stamp print time.strftime ('Start Time: %H:%M:%S') # Create the output image curX = 0 curY = 0 output = opencv.cvCreateImage(opencv.cvSize(DEFAULT_OUTPUT_WIDTH,DEFAULT_OUTPUT_HEIGHT), opencv.IPL_DEPTH_8U, 3) opencv.cvZero(output) opencv.cvNot(output,output) global window, screen # Initialize pygame if not already initialized if not pygame.display.get_init(): pygame.init() window = pygame.display.set_mode((DEFAULT_OUTPUT_WIDTH,DEFAULT_OUTPUT_HEIGHT)) pygame.display.set_caption("Etch-A-Sketch") screen = pygame.display.get_surface() # Draw the image calibrationRoutine(60) # Show the image displayImage(output) # Print end time stamp print time.strftime ('End Time: %H:%M:%S') # loop while(1): events = pygame.event.get() for event in events: if event.type == QUIT or (event.type == KEYDOWN): exit()
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)
def tile_images(img_width, img_height, num_width, num_height, images, channels=3): w = img_width * num_width h = img_height * num_height image = cv.cvCreateImage(cv.cvSize(int(w), int(h)), 8, channels) cv.cvSet(image, cv.cvScalar(255, 255, 255)) while len(images) > 0: try: for y in range(int(num_height)): for x in range(int(num_width)): small_tile = images.pop() img_x = x * img_width img_y = y * img_height cropped = cv.cvGetSubRect( image, cv.cvRect(img_x, img_y, img_width, img_height)) cv.cvCopy(small_tile, cropped) except exceptions.IndexError, e: break
def detect(self, pil_image, cascade_name, recogn_w=50, recogn_h=50): # Get cascade: cascade = self.get_cascade(cascade_name) image = opencv.PIL2Ipl(pil_image) image_size = opencv.cvGetSize(image) grayscale = image if pil_image.mode == "RGB": # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) # Change to RGB2Gray - I dont think itll affect the conversion opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
def crop(self, i_image): src_region = cv.cvGetSubRect(i_image, self.__roi) self.__cropped_image = cv.cvCreateImage( cv.cvSize(self.__roi.width, self.__roi.height), 8, 1) cv.cvCopy(src_region, self.__cropped_image) return self.__cropped_image
def CropImage(i_ipl_image, i_ipl_roi): src_region = cv.cvGetSubRect(i_ipl_image, i_ipl_roi) cropped_image = cv.cvCreateImage( cv.cvSize(i_ipl_roi.width, i_ipl_roi.height), 8, 1) cv.cvCopy(src_region, cropped_image) return cropped_image
def scale_image(img, scale): new_img = cv.cvCreateImage( cv.cvSize(img.width * scale, img.height * scale), img.depth, img.nChannels) cv.cvResize(img, new_img, cv.CV_INTER_NN) return new_img
def detect(self, pil_image, cascade_name, recogn_w = 50, recogn_h = 50): # Get cascade: cascade = self.get_cascade(cascade_name) image = opencv.PIL2Ipl(pil_image) image_size = opencv.cvGetSize(image) grayscale = image if pil_image.mode == "RGB": # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) # Change to RGB2Gray - I dont think itll affect the conversion opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
def get_cascade(self, cascade_name): self._cached_cascades[cascade_name] = opencv.cvLoadHaarClassifierCascade( os.path.join(self.cascade_dir, cascade_name), opencv.cvSize(1,1)) #cascade_name, opencv.cvSize(1,1)) return self._cached_cascades[cascade_name]
def detectObject(self, classifier): self.grayscale = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 1) opencv.cvCvtColor(self.iplimage, self.grayscale, opencv.CV_BGR2GRAY) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) opencv.cvEqualizeHist(self.grayscale, self.grayscale) try: self.cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1,1)) except: raise AttributeError, "could not load classifier file" self.objects = opencv.cvHaarDetectObjects(self.grayscale, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50)) return self.objects
def detectHaar(iplimage, classifier): srcimage = opencv.cvCloneImage(iplimage) grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) opencv.cvEqualizeHist(grayscale, grayscale) try: cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier + ".xml"), opencv.cvSize(1, 1)) except: raise AttributeError("could not load classifier file") objs = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) objects = [] for obj in objs: objects.append(Haarobj(obj)) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) opencv.cvReleaseMemStorage(storage) return objects
def split(image): img1 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1) img2 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1) img3 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1) cv.cvSplit(image, img1, img2, img3, None) return (img1, img2, img3)
def scale(image, s): scaled = cv.cvCreateImage( cv.cvSize(int(image.width * s), int(image.height * s)), image.depth, image.nChannels) cv.cvResize(image, scaled, cv.CV_INTER_AREA) return scaled
def compute(self, i_data, i_ipl=False): frame = 0 self.__frame = -1 max_dist = 0 if i_ipl: nframes = len(i_data) else: nframes = i_data.shape[2] list_of_roi = [] list_of_frames = [] list_of_sizes = [] for frame in range(0, nframes): if i_ipl: ipl_image = i_data[frame] else: ipl_image = cv.NumPy2Ipl(i_data[:,:,frame]) if self.__scale < 1.0: w = numpy.int(numpy.round( float( ipl_image.width ) * self.__scale )) h = numpy.int(numpy.round( float( ipl_image.height ) * self.__scale )) small_image = cv.cvCreateImage( cv.cvSize( w, h ) , ipl_image.depth, ipl_image.nChannels ) cv.cvResize( ipl_image , small_image ) vj_box = viola_jones_opencv(small_image) else: vj_box = viola_jones_opencv(ipl_image) if vj_box is not None: (min_row, min_col, max_row, max_col) = vj_box w = max_col - min_col h = max_row - min_row dist = w*w + h*h list_of_roi.append(vj_box) list_of_frames.append(frame) list_of_sizes.append(dist) self.__n_rows = ipl_image.height self.__n_cols = ipl_image.width #Choose a percentile of the sorted list nboxes = len(list_of_sizes) if nboxes == 0: #print "Viola-Jones failed on all images" return list_of_sizes = numpy.array(list_of_sizes) idx = numpy.argsort(list_of_sizes) percentile = 0.8 arg_idx = numpy.int(numpy.round(percentile * nboxes)) if arg_idx >= nboxes: arg_idx = nboxes - 1 if arg_idx < 0: arg_idx = 0 #print "n boxes: ", nboxes, " chosen arg: ", arg_idx self.__frame = frame = list_of_frames[idx[arg_idx]] best_roi = list_of_roi[idx[arg_idx]] (min_row, min_col, max_row, max_col) = best_roi if self.__scale < 1.0: #print "best roi width = ", max_col - min_col, " best roi height = ", max_row-min_row max_col = numpy.int(numpy.round( float(max_col) / self.__scale )) max_row = numpy.int(numpy.round( float(max_row) / self.__scale )) min_col = numpy.int(numpy.round( float(min_col) / self.__scale )) min_row = numpy.int(numpy.round( float(min_row) / self.__scale )) vj_box = (min_row, min_col, max_row, max_col) self.setRoi(vj_box) return self.getRoi()
def determine_next_position(self,image): self.image=image self.im=array2image(image) self.ipl_im = opencv.adaptors.PIL2Ipl(self.im) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) self.cascade = opencv('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',opencv.cvSize(1,1)) self.faces = opencv.cvHaarDetectObjects(self.ipl_im, self.cascade, self.storage, 1.2, 2,opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50)) if self.faces.total < 1: return None for f in self.faces: print "face detected: %s" %f #row and column are inverted in Opencv self.pos=(f.y,f.x) self.coormin_bbox=(self.pos[0],self.pos[1]) self.coormax_bbox=(self.pos[0]+f.width,self.pos[1]+f.height) self.brightpixel=False return (self.pos,self.coormin_bbox,self.coormax_bbox,self.brightpixel)
def main(argv=None): if argv is None: argv = sys.argv if len(argv) <= 1 or argv[1] is "-": im = DEFAULT_INPUT_IMAGE else: im = argv[1] if len(argv) <= 2 or argv[2] is "-": psize = DEFAULT_PIXEL_SCALE else: psize = int(argv[2]) if len(argv) <= 3 or argv[3] is "-": h = DEFAULT_OUTPUT_HEIGHT else: h = int(argv[3]) if len(argv) <= 4 or argv[4] is "-": w = DEFAULT_OUTPUT_WIDTH else: w = int(argv[4]) if len(argv) <= 5 or argv[5] is "-": serialport = DEFAULT_SERIAL_PORT else: serialport = argv[5] # declair globals global ser, output, curX, curY, last_dir # Open serial connection if serialport is not -1: ser = serial.Serial(serialport, 9600, timeout=1) else: ser = 0 last_dir = [None,None] # Print starting time stamp print time.strftime ('Start Time: %H:%M:%S') # Load the image image = highgui.cvLoadImage(im) # Create the output image curX = 0 curY = 0 output = opencv.cvCreateImage(opencv.cvSize(w,h), opencv.IPL_DEPTH_8U, 3) opencv.cvZero(output) opencv.cvNot(output,output) # Draw the image drawImage(image,h,w,psize) # Show the image displayImage(output) # Print end time stamp print time.strftime ('End Time: %H:%M:%S') # loop while(1): events = pygame.event.get() for event in events: if event.type == QUIT or (event.type == KEYDOWN): exit()
def IplResize(i_image, i_width, i_height): """Convert RGB to Gray scale and resize to input width and height""" small_image = cv.cvCreateImage(cv.cvSize(i_width, i_height), i_image.depth, i_image.nChannels) cv.cvResize(i_image, small_image) return small_image
def run(exposure, video=None, display=False, debug=False): if display: hg.cvNamedWindow("video", 1) hg.cvMoveWindow("video", 0, 0) if debug: hg.cvNamedWindow('right', 1) hg.cvMoveWindow("right", 800, 0) hg.cvNamedWindow("thresholded", 1) hg.cvNamedWindow('motion', 1) hg.cvNamedWindow('intensity', 1) hg.cvMoveWindow("thresholded", 800, 0) hg.cvMoveWindow("intensity", 0, 600) hg.cvMoveWindow("motion", 800, 600) if video is None: #video = cam.VidereStereo(0, gain=96, exposure=exposure) video = cam.StereoFile('measuring_tape_red_left.avi','measuring_tape_red_right.avi') frames = video.next() detector = LaserPointerDetector(frames[0], LaserPointerDetector.SUN_EXPOSURE, use_color=False, use_learning=True) detector_right = LaserPointerDetector(frames[1], LaserPointerDetector.SUN_EXPOSURE, use_color=False, use_learning=True, classifier=detector.classifier) stereo_cam = cam.KNOWN_CAMERAS['videre_stereo2'] for i in xrange(10): frames = video.next() detector.detect(frames[0]) detector_right.detect(frames[1]) lt = cv.cvCreateImage(cv.cvSize(640,480), 8, 3) rt = cv.cvCreateImage(cv.cvSize(640,480), 8, 3) for l, r in video: start_time = time.time() #l = stereo_cam.camera_left.undistort_img(l) #r = stereo_cam.camera_right.undistort_img(r) cv.cvCopy(l, lt) cv.cvCopy(r, rt) l = lt r = rt undistort_time = time.time() _, _, right_cam_detection, stats = detector_right.detect(r) if debug: draw_blobs(r, stats) draw_detection(r, right_cam_detection) hg.cvShowImage('right', r) image, combined, left_cam_detection, stats = detector.detect(l) detect_time = time.time() if debug: motion, intensity = detector.get_motion_intensity_images() show_processed(l, [combined, motion, intensity], left_cam_detection, stats, detector) elif display: #draw_blobs(l, stats) draw_detection(l, left_cam_detection) hg.cvShowImage('video', l) hg.cvWaitKey(10) if right_cam_detection != None and left_cam_detection != None: x = np.matrix(left_cam_detection['centroid']).T xp = np.matrix(right_cam_detection['centroid']).T result = stereo_cam.triangulate_3d(x, xp) print '3D point located at', result['point'].T, print 'distance %.2f error %.3f' % (np.linalg.norm(result['point']), result['error']) triangulation_time = time.time() diff = time.time() - start_time print 'Main: Running at %.2f fps, took %.4f s' % (1.0 / diff, diff)
def IplResize(i_image, i_width, i_height): """Convert RGB to Gray scale and resize to input width and height""" small_image = cv.cvCreateImage( cv.cvSize( i_width, i_height ) , i_image.depth, i_image.nChannels ) cv.cvResize( i_image , small_image ) return small_image