def redraw_monocular(self, drawable): width, height = cv.GetSize(drawable.scrib) display = cv.CreateMat(max(480, height), width + 100, cv.CV_8UC3) cv.Zero(display) cv.Copy(drawable.scrib, cv.GetSubRect(display, (0, 0, width, height))) cv.Set(cv.GetSubRect(display, (width, 0, 100, height)), (255, 255, 255)) self.buttons(display) if not self.c.calibrated: if drawable.params: for i, (label, lo, hi, progress) in enumerate(drawable.params): (w, _), _ = cv.GetTextSize(label, self.font) cv.PutText(display, label, (width + (100 - w) / 2, self.y(i)), self.font, (0, 0, 0)) color = (0, 255, 0) if progress < 1.0: color = (0, int(progress * 255.), 255) cv.Line(display, (int(width + lo * 100), self.y(i) + 20), (int(width + hi * 100), self.y(i) + 20), color, 4) else: cv.PutText(display, "lin.", (width, self.y(0)), self.font, (0, 0, 0)) linerror = drawable.linear_error if linerror < 0: msg = "?" else: msg = "%.2f" % linerror #print "linear", linerror cv.PutText(display, msg, (width, self.y(1)), self.font, (0, 0, 0)) self.show(display)
def find_avarage_colors(self): #+----+--C--+----+ # |--A--| | # |--B--| | # +---+-------+---+ #A_image rect = [0, 0, self.small_img.width, self.small_img.height] rect_A = (int(rect[2] * 0.25), int(rect[3] * 0.12), int(rect[2] * 0.5), int(rect[3] * 0.38)) #czolo nos rect_B = (int(rect[2] * 0.25), int(rect[3] * 0.5), int(rect[2] * 0.5), int(rect[3] * 0.38)) #nos broda rect_C = (int(rect[2] * 0.25), 0, int(rect[2] * 0.5), int(rect[3] * 0.12)) #wlosy A_image = cv.GetSubRect(self.small_img, rect_A) B_image = cv.GetSubRect(self.small_img, rect_B) C_image = cv.GetSubRect(self.small_img, rect_C) A_av, A_sd = cv.AvgSdv(A_image) B_av, B_sd = cv.AvgSdv(B_image) C_av, C_sd = cv.AvgSdv(C_image) A_av, B_av, C_av = sum(A_av) / float(len(A_av)), sum(B_av) / float( len(B_av)), sum(C_av) / float(len(C_av)) if B_av < 0.0001: B_av = 0.0001 self.A2B_ratio, self.A_B_color, self.C_color = A_av / float(B_av), ( A_av + B_av) / 510.0, C_av / 255.0 return self.A2B_ratio, self.A_B_color, self.C_color
def partitionImage(image_path, NUM_PARTITIONS=5): img = cv.LoadImageM(image_path) height = img.rows width = img.cols width_partition = width / NUM_PARTITIONS height_partition = height / NUM_PARTITIONS y_cur = 0 x_cur = 0 partitions = [] for i in range(0, NUM_PARTITIONS): #itera linhas for j in range(0, NUM_PARTITIONS): #itera colunas if j == NUM_PARTITIONS - 1: #se ultima coluna how_much_left_width = width - x_cur if i == NUM_PARTITIONS - 1: #se ultima coluna e ultima linha how_much_left_height = height - y_cur partitions.append( cv.GetSubRect(img, (x_cur, y_cur, how_much_left_width, how_much_left_height))) else: #so ultima coluna partitions.append( cv.GetSubRect(img, (x_cur, y_cur, how_much_left_width, height_partition))) else: partitions.append( cv.GetSubRect( img, (x_cur, y_cur, width_partition, height_partition))) x_cur = x_cur + width_partition #atualiza posicao da coluna (x) y_cur = y_cur + height_partition #atualiza posicao da linha (y) x_cur = 0 #reseta a posicao da coluna (x = 0) return partitions
def run(self): while True: frame = cv.QueryFrame( self.capture) # Run the cam-shift if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x,y,w,h = self.selection cv.Rectangle(frame, (x,y), (x+w,y+h), (255,255,255)) sel = cv.GetSubRect(self.hue, self.selection ) cv.CalcArrHist( [sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue( hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox( frame, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) cv.ShowImage("Output",frame) cv.WaitKey(0)
def blob_to_input_instance(image, blob, classification_window_width): patch_size = classification_window_width * 2 + 1 small_r = blob_to_rect( blob, classification_window_width=classification_window_width) big_r = blob_to_rect( blob, classification_window_width=classification_window_width * 2) if big_r == None or small_r == None: return None small_patch = cv.CloneMat(cv.GetSubRect(image, small_r.as_cv_rect())) big_patch = cv.CloneMat(cv.GetSubRect(image, big_r.as_cv_rect())) #cv.ShowImage('patch', small_patch) #cv.ShowImage('big_patch', big_patch) big_patch_rescaled = cv.CreateImage((int(classification_window_width / 2), int(classification_window_width / 2)), 8, 3) cv.Resize(big_patch, big_patch_rescaled, cv.CV_INTER_LINEAR) np_patch_small = np.asarray(small_patch) np_patch_big = ad.cv2array(big_patch_rescaled) np_resized_small = np.matrix( np_patch_small.reshape(patch_size * patch_size * 3, 1)) np_resized_big = np.matrix( np_patch_big.reshape(np_patch_big.shape[0] * np_patch_big.shape[1] * 3, 1)) return np.concatenate((np_resized_small, np_resized_big), axis=0)
def GetCornerType(x, y, img): """Get type of corner point: upper left, bottom left, upper right, bottom right, vertical half, horizontal half, also white on dark background or dark on white background. Greyscale image is presumed""" x1 = int(x) y1 = int(y) height = 5 width = 5 rect1 = cv.Avg(cv.GetSubRect(img, (x1, y1, width, height))) rect2 = cv.Avg(cv.GetSubRect(img, (x1 - width, y1, width, height))) rect3 = cv.Avg(cv.GetSubRect(img, (x1 - width, y1 - height, width, height))) rect4 = cv.Avg(cv.GetSubRect(img, (x1, y1 - height, width, height))) averages = [rect1[0], rect2[0], rect3[0], rect4[0]] clusters = cluster_points(averages) if (len(clusters[0]) == 2): return ("bad", "") else: fg, bg = None, None corner = None if (len(clusters[0]) == 1): fg = averages[clusters[0][0]] bg = averages[clusters[1][0]] corner = clusters[0][0] else: fg = averages[clusters[1][0]] bg = averages[clusters[0][0]] corner = clusters[1][0] if (fg > bg): return (corner, "w") else: return (corner, "b")
def update_mhi(img, dst, diff_threshold): global last global mhi global storage global mask global orient global segmask timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds size = cv.GetSize(img) # get current frame size idx1 = last if not mhi or cv.GetSize(mhi) != size: for i in range(N): buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Zero(buf[i]) mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) cv.Zero(mhi) # clear MHI at the beginning orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1) cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale idx2 = (last + 1) % N # index of (last - (N-1))th frame last = idx2 silh = buf[idx2] cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI cv.CvtScale(mhi, mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION) cv.Zero(dst) cv.Merge(mask, None, None, None, dst) cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) if not storage: storage = cv.CreateMemStorage(0) seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) for (area, value, comp_rect) in seq: if comp_rect[2] + comp_rect[3] > 100: # reject very small components color = cv.CV_RGB(255, 0,0) silh_roi = cv.GetSubRect(silh, comp_rect) mhi_roi = cv.GetSubRect(mhi, comp_rect) orient_roi = cv.GetSubRect(orient, comp_rect) mask_roi = cv.GetSubRect(mask, comp_rect) angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI if count < (comp_rect[2] * comp_rect[3] * 0.05): continue magnitude = 30. center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0) cv.Line(dst, center, (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), color, 3, cv.CV_AA, 0)
def concat_images(a, b): img_height = max(a.height, b.height) c = cv.CreateImage((a.width+b.width, img_height), a.depth, a.channels) a_area = cv.GetSubRect(c, (0,0, a.width, a.height)) b_area = cv.GetSubRect(c, (a.width, 0, b.width, b.height)) cv.Add(a, a_area, a_area) cv.Add(b, b_area, b_area) return c
def buttons(self, display): x = self.displaywidth self.button(cv.GetSubRect(display, (x, 180, 100, 100)), "CALIBRATE", self.c.goodenough) self.button(cv.GetSubRect(display, (x, 280, 100, 100)), "SAVE", self.c.calibrated) self.button(cv.GetSubRect(display, (x, 380, 100, 100)), "COMMIT", self.c.calibrated)
def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = False while True: frame = cv.QueryFrame(self.capture) # Convert to HSV and keep the hue hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1) print(self.hue) cv.Split(hsv, self.hue, None, None, None) # Compute back projection backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) # Run the cam-shift cv.CalcArrBackProject([self.hue], backproject, hist) if self.track_window and is_rect_nonzero(self.track_window): crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x, y, w, h = self.selection cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255)) sel = cv.GetSubRect(self.hue, self.selection) cv.CalcArrHist([sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) if not backproject_mode: #frame=cv.Flip(frame) cv.ShowImage("CamShiftDemo", frame) else: cv.ShowImage("CamShiftDemo", backproject) cv.ShowImage("Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(7) if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode
def do_camshift(self, cv_image): """ Get the image size """ image_size = cv.GetSize(cv_image) image_width = image_size[0] image_height = image_size[1] """ Convert to HSV and keep the hue """ hsv = cv.CreateImage(image_size, 8, 3) cv.CvtColor(cv_image, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(image_size, 8, 1) cv.Split(hsv, self.hue, None, None, None) """ Compute back projection """ backproject = cv.CreateImage(image_size, 8, 1) """ Run the cam-shift algorithm """ cv.CalcArrBackProject( [self.hue], backproject, self.hist ) if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect """ If mouse is pressed, highlight the current selected rectangle and recompute the histogram """ if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(cv_image, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(cv_image, cv_image, 0.5) cv.Copy(save, sub) x,y,w,h = self.selection cv.Rectangle(cv_image, (x,y), (x+w,y+h), (255,255,255)) sel = cv.GetSubRect(self.hue, self.selection ) cv.CalcArrHist( [sel], self.hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(self.hist) if max_val != 0: cv.ConvertScale(self.hist.bins, self.hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox( cv_image, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) roi = RegionOfInterest() roi.x_offset = int(min(image_width, max(0, track_box[0][0] - track_box[1][0] / 2))) roi.y_offset = int(min(image_height, max(0, track_box[0][1] - track_box[1][1] / 2))) roi.width = int(track_box[1][0]) roi.height = int(track_box[1][1]) self.ROI.publish(roi) cv.ShowImage("Histogram", self.hue_histogram_as_image(self.hist)) if not self.backproject_mode: return cv_image else: return backproject
def handle_stereo(self, qq): (lmsg, lcmsg, rmsg, rcmsg) = qq limg = CvBridge().imgmsg_to_cv(lmsg, "mono8") rimg = CvBridge().imgmsg_to_cv(rmsg, "mono8") if 0: cv.SaveImage("/tmp/l%06d.png" % self.i, limg) cv.SaveImage("/tmp/r%06d.png" % self.i, rimg) self.i += 1 scm = image_geometry.StereoCameraModel() scm.fromCameraInfo(lcmsg, rcmsg) bm = cv.CreateStereoBMState() if "wide" in rospy.resolve_name("stereo"): bm.numberOfDisparities = 160 if 0: disparity = cv.CreateMat(limg.rows, limg.cols, cv.CV_16SC1) started = time.time() cv.FindStereoCorrespondenceBM(limg, rimg, disparity, bm) print time.time() - started ok = cv.CreateMat(limg.rows, limg.cols, cv.CV_8UC1) cv.CmpS(disparity, 0, ok, cv.CV_CMP_GT) cv.ShowImage("limg", limg) cv.ShowImage("disp", ok) cv.WaitKey(6) self.track(CvBridge().imgmsg_to_cv(lmsg, "rgb8")) if len(self.tracking) == 0: print "No markers found" for code, corners in self.tracking.items(): corners3d = [] for (x, y) in corners: limr = cv.GetSubRect(limg, (0, y - bm.SADWindowSize / 2, limg.cols, bm.SADWindowSize + 1)) rimr = cv.GetSubRect(rimg, (0, y - bm.SADWindowSize / 2, rimg.cols, bm.SADWindowSize + 1)) tiny_disparity = cv.CreateMat(limr.rows, limg.cols, cv.CV_16SC1) cv.FindStereoCorrespondenceBM(limr, rimr, tiny_disparity, bm) if tiny_disparity[7, x] < 0: return corners3d.append(scm.projectPixelTo3d((x, y), tiny_disparity[7, x] / 16.)) if 0: cv.ShowImage("d", disparity) (a, b, c, d) = [numpy.array(pt) for pt in corners3d] def normal(s, t): return (t - s) / numpy.linalg.norm(t - s) x = PyKDL.Vector(*normal(a, b)) y = PyKDL.Vector(*normal(a, d)) f = PyKDL.Frame(PyKDL.Rotation(x, y, x * y), PyKDL.Vector(*a)) msg = pm.toMsg(f) # print "%10f %10f %10f" % (msg.position.x, msg.position.y, msg.position.z) print code, msg.position.x, msg.position.y, msg.position.z self.broadcast(lmsg.header, code, msg)
def setup(flipped, capture, thehandcolor): """Initializes camera and finds initial skin tone""" #creates initial window and prepares text color = (40, 0, 0) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0) textsize1 = (cv.GetSize(flipped)[0] / 2 - 150, cv.GetSize(flipped)[1] / 2 - 140) textsize2 = (cv.GetSize(flipped)[0] / 2 - 150, cv.GetSize(flipped)[1] / 2 - 110) point1 = (cv.GetSize(flipped)[0] / 2 - 25, cv.GetSize(flipped)[1] / 2 - 25) point2 = (cv.GetSize(flipped)[0] / 2 + 25, cv.GetSize(flipped)[1] / 2 + 25) #until Enter is pressed while (cv.WaitKey(10) != 10): #captures live video, and draws sub-box and text frame = cv.QueryFrame(capture) cv.Copy(frame, flipped) cv.Flip(flipped, flipped, 1) cv.Rectangle(flipped, point1, point2, color, 2) cv.PutText(flipped, "Put your hand in the box ", textsize1, font, color) cv.PutText(flipped, "and press enter", textsize2, font, color) cv.ShowImage("w2", flipped) #Creates sub-image inside box, and returns average color in box sub = cv.GetSubRect(flipped, (cv.GetSize(flipped)[0] / 2 - 25, cv.GetSize(flipped)[1] / 2 - 25, 50, 50)) cv.Set(thehandcolor, cv.Avg(sub)) return cv.Avg(sub)
def getData(): for i in range (0 , classes): for j in range (0, train_samples): if j < 10 : fichero = "OCR/"+str(i) + "/"+str(i)+"0"+str(j)+".pbm" else: fichero = "OCR/"+str(i) + "/"+str(i)+str(j)+".pbm" src_image = cv.LoadImage(fichero,0) prs_image = preprocessing(src_image, size, size) row = cv.GetRow(trainClasses, i*train_samples + j) cv.Set(row, cv.RealScalar(i)) row = cv.GetRow(trainData, i*train_samples + j) img = cv.CreateImage( ( size, size ), cv.IPL_DEPTH_32F, 1) cv.ConvertScale(prs_image,img,0.0039215, 0) data = cv.GetSubRect(img, (0,0, size,size)) row1 = cv.Reshape( data, 0, 1 ) cv.Copy(row1, row)
def classify(img): nearest=cv.CreateMat(1,K,cv.CV_32FC1) prs_image = preprocessing(img, size, size) img32 = cv.CreateImage( ( size, size ), cv.IPL_DEPTH_32F, 1 ) cv.ConvertScale(prs_image, img32, 0.0039215, 0) data = cv.GetSubRect(img32, (0,0, size,size)) row1 = cv.Reshape( data, 0, 1 ) result = knn.find_nearest(nearest,row1,K,0,0,0) result = 0 indices = cv.Mat(N, K, cv.CV_32S) dists = cv.Mat(N, K, cv.CV_32F) flann.knnSearch(m_object, indices, dists, K, cv.SearchParams(250)) accuracy=0 for i in range (0,K): # print nearest # if nearest.data.fl[i] == result: accuracy+=1 pre= 100*(float(accuracy)/float(K)) #print "r: ",result," pre ",pre," accu ",accuracy," K ",K return result
def crop_face(self, image, coordinates, image_filename): """ Crops all faces from a list of images and coordinates Returns a list with all faces""" logging.debug( 'Start method "crop_face" for file %s (face-detector.py)' % image_filename) cropped_faces = [] # list with all cropped faces (defined with ROI) for i in range(len(coordinates)): rectangle = coordinates[i][0] cropped_faces.append(cv.GetSubRect( image, rectangle)) # save faces (with ROI) in new list #check face for max image size if cropped_faces[i].height > parameter.max_facesize[ 0] or cropped_faces[i].width > parameter.max_facesize[ 1]: #start resize (cropped_faces[i], downsize_factor) = tools.downsize_image(cropped_faces[i]) logging.debug( 'Face in image %s has been downsized with factor %d (face-detector.py)' % (image_filename, downsize_factor)) logging.debug('%d faces successfully cropped (face-detector.py)', len(cropped_faces)) return cropped_faces # faces are defined with ROI
def __init__(self, frame, rect, time_makes_difference=False): self.rect = rect self.time_makes_difference = time_makes_difference self.face_photo_valid = False self.face_size = [rect[2], rect[3]] self.personID = -1 self.white_level = 6 try: sub_image = cv.GetSubRect(frame, rect) except: return None self.frame_copy = cv.CreateImage((rect[2], rect[3]), cv.IPL_DEPTH_8U, frame.nChannels) if frame.origin == cv.IPL_ORIGIN_TL: cv.Copy(sub_image, self.frame_copy) else: cv.Flip(sub_image, self.frame_copy, 0) self.find_eyes() if self.is_a_valid_face_photo(): self.find_wrinkles() self.find_avarage_colors() self.find_gender_age_emotions() #self.count_face_vector() self.create_face_vector()
def preprocessImage(inputPath, outputPath): image = cv.LoadImage(inputPath, 0) #Find the most likely face (x, y, w, h), n = mostLikelyHaar(image, haarFace) croppedImage = cv.CreateImage((w, h), image.depth, image.nChannels) scaledImage = cv.CreateImage((256, 256), image.depth, image.nChannels) src_region = cv.GetSubRect(image, (x, y, w, h)) cv.Copy(src_region, croppedImage) cv.Resize(croppedImage, scaledImage) image = scaledImage #Find each ficudial point leftEye = ImageObject(image, haarLeftEye, inPercentRect(image, 0, 0, .6, .5)) leftEyePoints = leftEye.getPoints([(.2, .5), (.8, .5)]) rightEye = ImageObject(image, haarRightEye, inPercentRect(image, .4, 0, 1, .5)) rightEyePoints = rightEye.getPoints([(.2, .5), (.8, .5)]) mouth = ImageObject(image, haarMouth, inPercentRect(image, 0, .6, 1, 1)) mouthPoints = mouth.getPoints([(0, .3), (.5, .3), (1, .3)]) nose = ImageObject(image, haarNose) nosePoints = nose.getPoints([(.2, .5), (.5, .5), (.8, .5)]) #rotate each set of points by the tilt of the face tiltAngle = math.atan2(rightEyePoints[0][1] - leftEyePoints[0][1], rightEyePoints[0][0] - leftEyePoints[0][0]) leftEyePoint = tiltPoints(tiltAngle, leftEyePoints) rightEyePoints = tiltPoints(tiltAngle, rightEyePoints) mouthPoints = tiltPoints(tiltAngle, mouthPoints) nosePoints = tiltPoints(tiltAngle, nosePoints) image = rotateImage(image, tiltAngle, (w / 2, h / 2)) leftEye = ImageObject(image, haarLeftEye, inPercentRect(image, 0, 0, .6, .5)) rightEye = ImageObject(image, haarRightEye, inPercentRect(image, .4, 0, 1, .5)) mouth = ImageObject(image, haarMouth, inPercentRect(image, 0, .6, 1, 1)) nose = ImageObject(image, haarNose) rotation = math.log(leftEye.w) - math.log(rightEye.w) print rotation info = { 'image': outputPath, 'lbp-left-eye': calcLBP(image, leftEye), 'left-eye': leftEye.getTuple(), 'lbp-right-eye': calcLBP(image, rightEye), 'right-eye': rightEye.getTuple(), 'lbp-mouth': calcLBP(image, mouth), 'mouth': mouth.getTuple(), 'tilt': tiltAngle, 'rotation': rotation } #save image of the cropped face cv.SaveImage(outputPath, image) return info
def crop(im, size, pos=(0, 0)): """ This function is very similar to :func:`sample()` in that it returns a specified subsection of the image. The main difference here is that if the region is too big to fit, the returned image is made smaller. There is no size default here and pos defaults to the top-left corner of the image. **Parameters:** * im (cvArr) - The source image. * size (tuple) - The cropped image size (w, h). * pos (tuple) - The position of the top-left corner of the cropped image. **Returns:** The cropped image. .. seealso:: :func:`sample()` """ warn = False if pos[0] + size[0] > im.width: size = (im.width - 1 - pos[0], size[1]) warn = True if pos[1] + size[1] > im.height: size = (size[0], im.height - 1 - pos[1]) warn = True if warn: warnings.warn( "Cropped region went off the edge of the image. The cropped size has been reduced to %dx%d." % (size[0], size[1]), stacklevel=2) rect = (pos[0], pos[1], size[0], size[1]) cropped = create(im, size=size) dst = cv.GetSubRect(im, rect) cv.Resize(dst, cropped) return cropped
def cropFrame(self, frame, lastMarkerLocationX, lastMarkerLocationY): if (not self.trackerIsInitialized): self.markerTracker.allocateSpaceGivenFirstFrame(self.originalImage) self.reducedImage = cv.CreateImage( (self.windowWidth, self.windowHeight), frame.depth, 3) xCornerPos = lastMarkerLocationX - self.windowWidth / 2 yCornerPos = lastMarkerLocationY - self.windowHeight / 2 # Ensure that extracted window is inside the original image. if (xCornerPos < 1): xCornerPos = 1 if (yCornerPos < 1): yCornerPos = 1 if (xCornerPos > frame.width - self.windowWidth): xCornerPos = frame.width - self.windowWidth if (yCornerPos > frame.height - self.windowHeight): yCornerPos = frame.height - self.windowHeight try: self.subImagePosition = (xCornerPos, yCornerPos, self.windowWidth, self.windowHeight) self.reducedImage = cv.GetSubRect(frame, self.subImagePosition) cv.ConvertScale(self.reducedImage, self.originalImage) cv.CvtColor(self.originalImage, self.frameGray, cv.CV_RGB2GRAY) except: print("frame: ", frame.depth) print("originalImage: ", self.originalImage.height, self.originalImage.width, self.originalImage) print("frameGray: ", self.frameGray.height, self.frameGray.width, self.frameGray.depth) print "Unexpected error:", sys.exc_info()[0] #quit(0) pass
def _selectTrackingPoints(self,frame): ''' This uses the OpenCV get good features to track to initialize a set of tracking points_b. ''' quality = 0.01 min_distance = 15 w,h = self.tile_size tw = w//self.grid th = h//self.grid for i in range(self.grid): for j in range(self.grid): ul = pv.Point(i*tw,j*th) rect = pv.Rect(i*tw,j*th,tw,th) count = 0 for pt in self.tracks: if rect.containsPoint(pt): count += 1 if count < self.min_points: gray = cv.CreateImage ((tw,th), 8, 1) faceim = cv.GetSubRect(frame, rect.asOpenCV()) cv.Resize(faceim,gray) eig = cv.CreateImage ((tw,th), 32, 1) temp = cv.CreateImage ((tw,th), 32, 1) # search the good points_b points_b = cv.GoodFeaturesToTrack (gray, eig, temp, 2*self.min_points, quality, min_distance, None, 3, 0, 0.04) for pt in points_b: self.tracks.append(ul+pv.Point(pt))
def DetectRedEyes(image, faceCascade, eyeCascade): min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round( image.width / image_scale), cv.Round(image.height / image_scale)), 8, 1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: print "face detected" for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) face_region = cv.GetSubRect(image, (x, int(y + (h / 4)), w, int(h / 2))) cv.SetImageROI( image, (pt1[0], pt1[1], pt2[0] - pt1[0], int((pt2[1] - pt1[1]) * 0.7))) eyes = cv.HaarDetectObjects(image, eyeCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, (15, 15)) # if eyes: # # For each eye found # for eye in eyes: # # Draw a rectangle around the eye # cv.Rectangle(image, # (eye[0][0], # eye[0][1]), # (eye[0][0] + eye[0][2], # eye[0][1] + eye[0][3]), # cv.RGB(255, 0, 0), 1, 8, 0) cv.ResetImageROI(image) return image
def sourires(src): res = [] img = cv.GetSubRect(src, (src.width*1/7, src.height*2/3, src.width*5/7, src.height/3)) cpt = 0 for s in all_s : temp = cv.HaarDetectObjects(img, s, cv.CreateMemStorage()) res.append(len(temp)) return res
def crop(src, sky): x,y = sky.center left = x - sky.radius top = y - sky.radius new_width = 2 * sky.radius new_height = 2 * sky.radius cropped = cv.GetSubRect(src, (left, top, new_width, new_height) ) return cropped
def crop(self, left, top, right, bottom): new_width = right - left new_height = bottom - top cropped = cv.CreateImage((new_width, new_height), 8, 3) src_region = cv.GetSubRect(self.image, (left, top, new_width, new_height) ) cv.Copy(src_region, cropped) self.image = cropped
def sourires(img): res = dict() img = cv.GetSubRect(img, (img.width * 1 / 7, img.height * 2 / 3, img.width * 5 / 7, img.height / 3)) cpt = 0 for s, smiles in zip(all_s, smile_list): res[smiles] = len(cv.HaarDetectObjects(img, s, cv.CreateMemStorage())) return res
def getSegment(channel, image, rect, bgcolor=96): seg = cap.getSubImage(channel, rect) original = cv.GetSubRect(image, rect) for x in xrange(original.width): for y in xrange(original.height): if original[y, x] != 0 and seg[y, x] != 255: seg[y, x] = bgcolor return seg
def run(self): self.objects[0] = list() while True: frame = cv.QueryFrame(self.capture) if frame == 0: break self.frameNumber = self.frameNumber + 1 if self.frameNumber % 10 == 0: self.save() #copy last frame objects self.objects[self.frameNumber] = deepcopy( self.objects[self.frameNumber - 1][:]) while True: frameSelection = cv.CloneImage(frame) for object in self.objects[self.frameNumber]: x, y, w, h = object.getRectangle() cv.Rectangle(frameSelection, (x, y), (x + w, y + h), (255, 255, 255)) # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frameSelection, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frameSelection, frameSelection, 0.5) cv.Copy(save, sub) x, y, w, h = self.selection cv.Rectangle(frameSelection, (x, y), (x + w, y + h), (255, 255, 255)) cv.ShowImage("Output", frameSelection) key = cv.WaitKey(1000 / 25) if key == ord("n"): break if key == ord("w"): self.save() if key == ord("s"): self.startFrame = self.frameNumber print "Set Starting frame: " + self.frameNumber if key == ord("c"): self.objects[self.frameNumber].extend( self.objects[self.frameNumber - 1][:]) if key == ord("d"): self.objects[self.frameNumber] = list() elif key == 65364: moveObjects(self.objects[self.frameNumber], (0, 1)) elif key == 65362: moveObjects(self.objects[self.frameNumber], (0, -1)) elif key == 65363: moveObjects(self.objects[self.frameNumber], (1, 0)) elif key == 65361: moveObjects(self.objects[self.frameNumber], (-1, 0))
def extractFace(image_path): """ If a face is detected in an image, the face is cropped and saved as a new image, and a rectangle is drawn over the area where the face was detected and that is saved as its own image. """ # Handle IO errors, which suck try: img = cv.LoadImage(image_path) io_err = False except IOError as e: print e io_err = True if not io_err: # Create a grayscale version of the image img_gray = cv.CreateImage(cv.GetSize(img), 8, 1) cv.CvtColor(img, img_gray, cv.CV_BGR2GRAY) # Equalize image histogram cv.EqualizeHist(img_gray, img_gray) # Show processed image cv.ShowImage('Processed', img_gray) # Create storage for detection storage = cv.CreateMemStorage(0) # Train face detector using the Haar cascade file cascade = cv.Load('data/haarcascade_frontalface_alt.xml') faces = cv.HaarDetectObjects(img_gray, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING) # Extract the region of interest detected by the classifier if faces: date = re.split("[./]", image_path)[1] face_count = 0 for i in faces: x = i[0][0] y = i[0][1] w = i[0][2] h = i[0][3] # Highlight face in original cv.Rectangle(img, (x, y), (x+w, y+h), cv.RGB(0, 255, 0), 2) # Extraqct face for skin tone crop = cv.CreateImage((w, h), 8, 3) roi = cv.GetSubRect(img, i[0]) cv.Copy(roi, crop) # Save images highlight_path = "faces/" + date + '_' + str(face_count) + '.jpg' crop_path = "faces/" + date + '_' + str(face_count) + '_crop.jpg' cv.SaveImage(highlight_path, img) cv.SaveImage(crop_path, crop) face_count += 1 else: print 'No face detected in: ' + image_path
def redraw_stereo(self, drawable): width, height = cv.GetSize(drawable.lscrib) display = cv.CreateMat(max(480, height), 2 * width + 100, cv.CV_8UC3) cv.Zero(display) cv.Copy(drawable.lscrib, cv.GetSubRect(display, (0, 0, width, height))) cv.Copy(drawable.rscrib, cv.GetSubRect(display, (width, 0, width, height))) cv.Set(cv.GetSubRect(display, (2 * width, 0, 100, height)), (255, 255, 255)) self.buttons(display) if not self.c.calibrated: if drawable.params: for i, (label, lo, hi, progress) in enumerate(drawable.params): (w, _), _ = cv.GetTextSize(label, self.font) cv.PutText(display, label, (2 * width + (100 - w) / 2, self.y(i)), self.font, (0, 0, 0)) color = (0, 255, 0) if progress < 1.0: color = (0, int(progress * 255.), 255) cv.Line(display, (int(2 * width + lo * 100), self.y(i) + 20), (int(2 * width + hi * 100), self.y(i) + 20), color, 4) else: cv.PutText(display, "epi.", (2 * width, self.y(0)), self.font, (0, 0, 0)) if drawable.epierror == -1: msg = "?" else: msg = "%.2f" % drawable.epierror cv.PutText(display, msg, (2 * width, self.y(1)), self.font, (0, 0, 0)) # TODO dim is never set anywhere. Supposed to be observed chessboard size? if drawable.dim != -1: cv.PutText(display, "dim", (2 * width, self.y(2)), self.font, (0, 0, 0)) cv.PutText(display, "%.3f" % drawable.dim, (2 * width, self.y(3)), self.font, (0, 0, 0)) self.show(display)