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 findFaces(self, img): faces = [] self.detect_time = time.time() rects = self.face_detector.detect(img) self.detect_time = time.time() - self.detect_time cvtile = opencv.cvCreateMat(128, 128, opencv.CV_8UC3) bwtile = opencv.cvCreateMat(128, 128, opencv.CV_8U) cvimg = img.asOpenCV() self.eye_time = time.time() for rect in rects: faceim = opencv.cvGetSubRect(cvimg, rect.asOpenCV()) opencv.cvResize(faceim, cvtile) affine = pv.AffineFromRect(rect, (128, 128)) opencv.cvCvtColor(cvtile, bwtile, cv.CV_BGR2GRAY) leye, reye, lcp, rcp = self.fel.locateEyes(bwtile) leye = pv.Point(leye) reye = pv.Point(reye) leye = affine.invertPoint(leye) reye = affine.invertPoint(reye) faces.append([rect, leye, reye]) self.eye_time = time.time() - self.eye_time self.current_faces = faces return 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 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 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 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 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 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 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 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 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