class image_converter: def __init__(self): self.bridge = CvBridge() self.image_sub = None def callback(self, data): self.image_sub.unregister() try: cv_image = self.bridge.imgmsg_to_cv(data, "bgr8") except CvBridgeError, e: print e rospy.loginfo("sum of pixels is %d at %s", max(cv.Sum(cv_image)), data.header.stamp.secs) if max(cv.Sum(cv_image)) == 0: rospy.logerr("Restart openni_node1") retcode = -1 try: retcode = subprocess.call('rosnode kill /openni/driver', shell=True) except Exception, e: rospy.logerr( 'Unable to kill kinect node, caught exception:\n%s', traceback.format_exc())
class image_converter: def __init__(self): self.bridge = CvBridge() self.image_sub = None def callback(self,data): if data.header.stamp < self.prev_time + rospy.Duration(60): return self.image_sub.unregister() self.prev_time = data.header.stamp try: cv_image = self.bridge.imgmsg_to_cv(data, "bgr8") except CvBridgeError, e: print e rospy.loginfo("sum of pixels is %d at %s", max(cv.Sum(cv_image)), data.header.stamp.secs) if max(cv.Sum(cv_image)) == 0 : #if True: rospy.logerr("Restart openni_node1") retcode = -1 try: #retcode = subprocess.call('rosnode kill /openni/driver', shell=True) # 3. usbreset... speak("resetting u s b") p = subprocess.Popen("lsusb", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() lines = stdout.split("\n") ms_line = [l for l in lines if "Microsoft" in l][0] # it should be like Bus 002 Device 013: ID 045e:02ad Microsoft Corp. Xbox NUI Audio bus_id = ms_line.split(' ')[1] bus_device_dir = "/dev/bus/usb/" + bus_id files = os.listdir(bus_device_dir) for f in files: full_path = os.path.join(bus_device_dir, f) if os.access(full_path, os.W_OK): retcode = subprocess.call('rosrun openni2_camera usb_reset ' + full_path, shell=True) time.sleep(10) # 1. kill nodelet manager speak("something wrong with kinect, I'll restart it, killing nodelet manager") retcode = subprocess.call('rosnode kill /openni/openni_nodelet_manager', shell=True) time.sleep(10) # 2. pkill speak("killing child processes") retcode = subprocess.call('pkill -f openni_nodelet_manager', shell=True) time.sleep(10) # 3 restarting speak("restarting processes") retcode = subprocess.call('roslaunch openni_launch openni.launch camera:=openni publish_tf:=false depth_registration:=true rgb_processing:=false ir_processing:=false depth_processing:=false depth_registered_processing:=false disparity_processing:=false disparity_registered_processing:=false hw_registered_processing:=true sw_registered_processing:=false', shell=True) except Exception, e: rospy.logerr('Unable to kill kinect node, caught exception:\n%s', traceback.format_exc())
def measure_focused_roi(im, roi, area, focus_points, debug=False): g = Grid(cv.GetSize(im)) canvas = image.new_from(im) cv.Set(canvas, 0) focus_in_roi = image.And(focus_points, roi) if debug: image.show(focus_in_roi, "ROI + Focused Points") densities = [] points = convert_to_points(focus_in_roi) groups = form_groups(points, estimated_size=24, iter=5) for group in groups: ch = ConvexHull(map(lambda x: (x[0], x[1]), group)) ppp = ch.points_per_pixel() a = int(ppp * 255) ch.draw_filled_hull(canvas, rgb=(a,a,a)) if debug: image.show(canvas, "Focused Regions in ROI") quadrants = g.split_in_four(canvas) sums = [] for i,quad in enumerate(quadrants): sums.append(cv.Sum(quad)[0] / float(area/4)) arr = array(sums) print arr.mean(), arr.std() diff = max(sums) - min(sums) return diff, arr.std()
def projectDown(image): proj = [] for x in range(image.width): col = cv.GetCol(image, x) sum = cv.Sum(col) proj.append(sum) return proj
def ccoeff_normed(img1, img2): size = cv.GetSize(img1) tmp1 = float_version(img1) tmp2 = float_version(img2) cv.SubS(tmp1, cv.Avg(tmp1), tmp1) cv.SubS(tmp2, cv.Avg(tmp2), tmp2) norm1 = cv.CloneImage(tmp1) norm2 = cv.CloneImage(tmp2) cv.Pow(tmp1, norm1, 2.0) cv.Pow(tmp2, norm2, 2.0) #cv.Mul(tmp1, tmp2, tmp1) return cv.DotProduct(tmp1, tmp2) / (cv.Sum(norm1)[0]*cv.Sum(norm2)[0])**0.5
def compute_glcm(img, d): order = 8 newimg = quantize(img, order) glcm = cv.CreateMat(order,order,cv.CV_8UC1) normglcm = cv.CreateMat(order, order, cv.CV_32FC1) cv.SetZero(glcm) div = 255/order for i in range(img.rows-d): for j in range(img.cols-d): val1 = cv.Get2D(newimg, i, j) val2 = cv.Get2D(newimg, i+d, j+d) p = int(val1[0]/div) q = int(val2[0]/div) if p>=order: p = order -1 if q>=order: q = order -1 #print p, q val3 = cv.Get2D(glcm, p, q) cv.Set2D(glcm, p, q, (val3[0]+1)) tot = cv.Sum(glcm) for i in range(glcm.rows): for j in range(glcm.cols): val3 = cv.Get2D(glcm, i , j) val = 1.0*val3[0]/tot[0] cv.Set2D(normglcm, i, j, (val)) #print round(float(cv.Get2D(normglcm, i, j)[0]), 3), #print "\n" return normglcm
def edge_threshold(image, roi=None, debug=0): thresholded = cv.CloneImage(image) horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1) magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) vertical = cv.CloneImage(horizontal) v_edge = cv.CloneImage(image) magnitude = cv.CloneImage(horizontal) storage = cv.CreateMemStorage(0) mag = cv.CloneImage(image) cv.Sobel(image, horizontal, 0, 1, 1) cv.Sobel(image, vertical, 1, 0, 1) cv.Pow(horizontal, horizontal, 2) cv.Pow(vertical, vertical, 2) cv.Add(vertical, horizontal, magnitude) cv.Convert(magnitude, magnitude32f) cv.Pow(magnitude32f, magnitude32f, 0.5) cv.Convert(magnitude32f, mag) if roi: cv.And(mag, roi, mag) cv.Normalize(mag, mag, 0, 255, cv.CV_MINMAX, None) cv.Threshold(mag, mag, 122, 255, cv.CV_THRESH_BINARY) draw_image = cv.CloneImage(image) and_image = cv.CloneImage(image) results = [] threshold_start = 17 for window_size in range(threshold_start, threshold_start + 1, 1): r = 20 for threshold in range(0, r): cv.AdaptiveThreshold(image, thresholded, 255, \ cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, window_size, threshold) contour_image = cv.CloneImage(thresholded) contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST) cv.Zero(draw_image) cv.DrawContours(draw_image, contours, (255, 255, 255), (255, 255, 255), 1, 1) if roi: cv.And(draw_image, roi, draw_image) cv.And(draw_image, mag, and_image) m1 = np.asarray(cv.GetMat(draw_image)) m2 = np.asarray(cv.GetMat(mag)) total = mag.width * mag.height #cv.Sum(draw_image)[0] coverage = cv.Sum(and_image)[0] / (mag.width * mag.height) if debug: print threshold, coverage cv.ShowImage("main", draw_image) cv.ShowImage("main2", thresholded) cv.WaitKey(0) results.append((coverage, threshold, window_size)) results.sort(lambda x, y: cmp(y, x)) _, threshold, window_size = results[0] cv.AdaptiveThreshold(image, thresholded, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, \ cv.CV_THRESH_BINARY, window_size, threshold) return thresholded
def doandgetAvgs(imgnames, rootdir, queue): for imgname in imgnames: imgpath = pathjoin(rootdir, imgname) I = cv.LoadImage(imgpath, cv.CV_LOAD_IMAGE_GRAYSCALE) w, h = cv.GetSize(I) result = cv.Sum(I)[0] / float(w * h) #data = shared.standardImread(pathjoin(rootdir, imgname), flatten=True) #result = 256 * (float(sum(map(sum, data)))) / (data.size) queue.put((imgname, result)) return 0
def ssdScore(f1, f2): global size #size o f SSD window #subtracts f2 from f1 sub_f1_f2 = cv.CreateMat(size, size, cv.CV_64FC1) cv.Sub(f1, f2, sub_f1_f2) #square and add f1_f2_square = cv.CreateMat(size, size, cv.CV_64FC1) cv.Pow(sub_f1_f2, f1_f2_square, 2) score = cv.Sum(f1_f2_square) return score[0] / (size * size)
def getpositions(im): leftmost = 0 rightmost = 0 topmost = 0 bottommost = 0 temp = 0 for i in range(im.width): col = cv.GetCol(im, i) if cv.Sum(col)[0] != 0.0: rightmost = i if temp == 0: leftmost = i temp = 1 for i in range(im.height): row = cv.GetRow(im, i) if cv.Sum(row)[0] != 0.0: bottommost = i if temp == 1: topmost = i temp = 2 return (leftmost, rightmost, topmost, bottommost)
def getpositions(im): ''' this function returns leftmost,rightmost,topmost and bottommost values of the white blob in the thresholded image''' leftmost = 0 rightmost = 0 topmost = 0 bottommost = 0 temp = 0 for i in range(im.width): col = cv.GetCol(im, i) if cv.Sum(col)[0] != 0.0: rightmost = i if temp == 0: leftmost = i temp = 1 for i in range(im.height): row = cv.GetRow(im, i) if cv.Sum(row)[0] != 0.0: bottommost = i if temp == 1: topmost = i temp = 2 return (leftmost, rightmost, topmost, bottommost)
def pixel_distance_matrix(images): buf = cv.CreateImage(cv.GetSize(images[0]), images[0].depth, images[0].channels) distances = np.zeros((len(images), len(images))) for i in xrange(len(images)): for j in xrange(i + 1, len(images)): cv.Sub(images[i], images[j], buf) cv.Pow(buf, buf, 2) distance = cv.Sum(buf)[0] distances[i, j] = distance distances[j, i] = distance del buf return distances
def test_crop_oriented_boxes(self): box_num = 0 for cropped_image in oriented_bounding_boxes.cropped_oriented_boxes( "test/test_case_bbox.xml", "test/test_case.png"): box_num += 1 gold_standard = cv.LoadImage("test/test_case_%03d.png" % box_num) self.assertEquals(cv.GetElemType(cropped_image), cv.GetElemType(gold_standard)) self.assertEquals(cv.GetSize(cropped_image), cv.GetSize(gold_standard)) diff = cv.CloneImage(gold_standard) cv.AbsDiff(cropped_image, gold_standard, diff) self.assertEquals(cv.Sum(diff), (0, 0, 0, 0))
def image_to_weight(self, image): midx = image.width // 2 midy = image.height // 2 # up left ROI cv.SetImageROI(image, (0, 0, midx, midy)) sum_up_left = cv.Sum(image) cv.ShowImage("upleft", image) # up right ROI cv.SetImageROI(image, (midx, 0, image.width, midy)) sum_up_right = cv.Sum(image) cv.ShowImage("upright", image) # down left ROI cv.SetImageROI(image, (0, midy, midx, image.height)) sum_down_left = cv.Sum(image) cv.ShowImage("downleft", image) # down right ROI cv.SetImageROI(image, (midx, midy, image.width, image.height)) sum_down_right = cv.Sum(image) cv.ShowImage("downright", image) # sum to weight self.sum_to_weight((sum_up_left[0], sum_up_right[0], sum_down_left[0], sum_down_right[0]))
def test2(): path = "/Users/soswow/Documents/Face Detection/gray_test" for file_path, name in directory_files(path): if name.endswith(".jpg"): img = cv.LoadImage(file_path, iscolor=False) N = sizeOf(img)[0] * sizeOf(img)[1] I_bar = cv.Sum(img)[0] / N tmp = image_empty_clone(img) cv.SubS(img, I_bar, tmp) tmp2 = image_empty_clone(img) cv.AddS(tmp, 128, tmp2) merged = merge_images(img, tmp2, vertical=True) cv.SaveImage(join(path, "_%s" % name), merged)
def findY(imgSrc): mini = 0 maxi = 0 minFound = 0 maxVal=cv.RealScalar(imgSrc.width * 255) for i in range(0,imgSrc.height): data = cv.GetRow(imgSrc, i); val = cv.Sum(data); if(val[0] < maxVal[0]): maxi = i if(not minFound): mini = i minFound= 1 return mini,maxi
def measure(im, blur, noise=None, debug=False): focus_points = blur[0] #is_noisy = noise[2] size = cv.GetSize(im) npixels = size[0] * size[1] #if focused_regions is None: # focused_regions = image.new_from(im) # cv.Set(focused_regions, 0) # groups = form_groups(focus_points, # estimated_size=min(max(int(len(npixels) / 1000), 2), 15)) # #groups = form_groups(points, threshold=max(cv.GetSize(im))/16) # #print 'groups', len(groups) # draw_groups(groups, focused_regions) im2 = cv.CloneImage(im) g = Grid(cv.GetSize(im2)) if debug: image.show(g.draw(im2), "Image with Grid + ROI") roi = image.new_from(im, nChannels=1) cv.Set(roi, 0) #g.draw_lines(roi, thickness=int(max(min((size[0] + size[1]) * 1/100.0, 255), 1))) g.draw_regions(roi) area = cv.Sum(roi)[0] (_, face_rects), face_score = faces.measure(im) face_block = image.new_from(im, nChannels=1) cv.Set(face_block, 0) for r in face_rects: r.draw(face_block, color=cv.RGB(255,255,255), thickness=cv.CV_FILLED) if debug: face_roi = cv.CloneImage(im) cv.Set(face_roi, 0, image.invert(roi)) cv.Set(face_roi, 0, image.invert(image.threshold(face_block, threshold=1))) image.show(face_block, "Faces in Binary") image.show(g.draw(face_roi), "Face + ROI") return (im, ( measure_focused_roi(im, roi, area, focus_points, debug), #measure_color_roi(im, roi, area, focus_points, debug), measure_contrast(im, debug), measure_saturation(im, debug), faces.measure(im, debug)[1], ))
def backgroundDiff(img, Imask): cv.CvtScale(img, Iscratch, 1, 0) cv.Split(Iscratch, Igray1, Igray2, Igray3, None) cv.InRange(Igray1, Ilow1, Ihi1, Imask) cv.InRange(Igray2, Ilow2, Ihi2, Imaskt) cv.Or(Imask, Imaskt, Imask) cv.InRange(Igray3, Ilow3, Ihi3, Imaskt) cv.Or(Imask, Imaskt, Imask) cv.SubRS(Imask, 255, Imask) cv.SaveImage('/home/mkillpack/Desktop/mask.png', Imask) #cv.Erode(Imask, Imask) print "here is the sum of the non-zero pixels", cv.Sum(Imask) return Imask
class CheckOpenNINode: def __init__(self): self.bridge = CvBridge() self.image_sub = None self.camera = rospy.get_param('~camera', 'kinect_head') self.sleep_cycle = rospy.get_param('~sleep_cycle', 60) self.speak_enabled = rospy.get_param("~speak", True) self.speak_pub = rospy.Publisher("/robotsound", SoundRequest) self.restart_srv = rospy.Service('~restart', Empty, self.restart_service_callback) def speak(self, speak_str): rospy.logerr("[%s] %s", self.__class__.__name__, speak_str) if self.speak_enabled: msg = SoundRequest() msg.sound = SoundRequest.SAY msg.command = SoundRequest.PLAY_ONCE msg.arg = speak_str self.speak_pub.publish(msg) def restart_service_callback(self, req): self.restart_openni_node() return EmptyResponse() def image_callback(self, msg): self.image_sub.unregister() try: cv_image = self.bridge.imgmsg_to_cv(msg, "bgr8") except CvBridgeError, e: rospy.logerr("[%s] failed to convert image to cv", self.__class__.__name__) return sum_of_pixels = max(cv.Sum(cv_image)) rospy.loginfo("[%s] sum of pixels is %d at %s", self.__class__.__name__, sum_of_pixels, msg.header.stamp.secs) if sum_of_pixels == 0: self.restart_openni_node()
class RobotPass(object): def __init__(self, name, left, right): self.result = PassObjectResult() self.l_gripper_pos = 0 self.r_gripper_pos = 0 self.hit_started = False #Positions database for arms self.position_high = [[0.0, -0.350, 0.0, -1.225, 3.14159, -1.65, 0.0], [0.0, -0.350, 0.0, -1.625, 3.14159, -1.65, 0.0]] self.position_medium = [[0.0, 0.640, 0.0, -1.925, 3.14159, -1.25, 0.0], [0.0, 0.640, 0.0, -2.325, 3.14159, -1.25, 0.0]] self.position_low = [[0.0, 0.900, 0.0, -0.825, 3.14159, 0, 0.0], [0.0, 1.200, 0.0, -1.225, 3.14159, 0, 0.0]] self.position_db = { 0: self.position_low, 1: self.position_medium, 2: self.position_high } self.stash_low = { 0: [ 2.1339572013257304, -0.091108732183497729, -0.32299417263835628, -2.1179687143792689, 18.241037622294144 - math.pi * 2, -0.89756419129607556, 0.13109237626572012 ], } self.stash_high = { 0: [ 2.1255836329726749, -0.3337269716470736, -0.24746704941813968, -1.8277034380552961, 11.906203130338264, -0.97553217611455001, 0.12960064186230591 ], } self.stash_back = { 0: [ 2.0678806272922126, 0.051856687863030673, -0.29605456180821554, -1.9498899184629934, -0.47689138482042087, -1.1624609998357744, -0.13265248181800637 ], } self.action_name = name self.action_server = actionlib.SimpleActionServer( self.action_name, PassObjectAction, execute_cb=self.execute_cb, auto_start=False) self.bridge = CvBridge() self.process_gripper_right = False self.process_gripper_left = False self.gripper_image_right = False self.gripper_image_left = False self.gripper_object_right = False self.gripper_object_left = False wait_for = [] self.l_arm = False self.r_arm = False self.l_arm_pos = False self.r_arm_pos = False self.arm_goal = False if (left): self.l_arm = actionlib.SimpleActionClient( "l_arm_controller/joint_trajectory_action", JointTrajectoryAction) self.l_arm_names = [ "l_shoulder_pan_joint", "l_shoulder_lift_joint", "l_upper_arm_roll_joint", "l_elbow_flex_joint", "l_forearm_roll_joint", "l_wrist_flex_joint", "l_wrist_roll_joint" ] self.l_gripper = actionlib.SimpleActionClient( "l_gripper_sensor_controller/gripper_action", Pr2GripperCommandAction) self.l_sensor = actionlib.SimpleActionClient( "l_gripper_sensor_controller/event_detector", PR2GripperEventDetectorAction) self.l_camera = rospy.Subscriber("/l_forearm_cam/image_rect_color", Image, self.process_image_left) wait_for += [self.l_arm, self.l_gripper, self.l_sensor] if (right): self.r_arm = actionlib.SimpleActionClient( "r_arm_controller/joint_trajectory_action", JointTrajectoryAction) self.r_arm_names = [ "r_shoulder_pan_joint", "r_shoulder_lift_joint", "r_upper_arm_roll_joint", "r_elbow_flex_joint", "r_forearm_roll_joint", "r_wrist_flex_joint", "r_wrist_roll_joint" ] self.r_gripper = actionlib.SimpleActionClient( "r_gripper_sensor_controller/gripper_action", Pr2GripperCommandAction) self.r_sensor = actionlib.SimpleActionClient( "r_gripper_sensor_controller/event_detector", PR2GripperEventDetectorAction) self.r_camera = rospy.Subscriber("/r_forearm_cam/image_rect_color", Image, self.process_image_right) wait_for += [self.r_arm, self.r_gripper, self.r_sensor] rospy.Subscriber("joint_states", JointState, self.joint_state_callback) for actionclient in wait_for: rospy.loginfo("Wait for item") actionclient.wait_for_server() self.action_server.start() def process_image_right(self, data): if (self.process_gripper_right): self.gripper_object_right, self.gripper_image_right = self.process_gripper_image( data, self.gripper_image_right, (0, 0, 100, 100)) def process_image_left(self, data): if (self.process_gripper_left): self.gripper_object_left, self.gripper_image_left = self.process_gripper_image( data, self.gripper_image_left, (244, 160, 160, 180)) def process_gripper_image(self, data, current_image, sub_rect): try: bimg = self.bridge.imgmsg_to_cv(data, "bgr8") except CvBridgeError, e: print e sub_data = cv.GetSubRect(bimg, sub_rect) if (current_image == False): #print "No current image" return (False, sub_data) else: cv.AbsDiff(sub_data, current_image, sub_data) delta = cv.Sum(sub_data) total = 0 for i in delta: total = total + i if (total > 1000000.0): result = True else: result = False #print result, delta return (result, current_image)
def is_empty_image(I): """ Try to infer if this image is just an empty white image. """ w, h = cv.GetSize(I) ratio = cv.Sum(I)[0] / float(w * h) return ratio >= 230.0
def segment_rect(image, rect, debug=False, display=None, target_size=None, group_range=(3, 25)): global next skip = False best_chars = [] best_threshold = None thresholded = cv.CloneImage(image) contour_image = cv.CloneImage(image) edges = cv.CloneImage(image) min_x, min_y, width, height = rect # cv.SetImageROI(thresholded, rect) cv.SetImageROI(contour_image, rect) cv.SetImageROI(image, rect) cv.SetImageROI(edges, rect) horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1) magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) vertical = cv.CloneImage(horizontal) magnitude = cv.CloneImage(horizontal) cv.Sobel(image, horizontal, 0, 1, 3) cv.Sobel(image, vertical, 1, 0, 3) cv.Pow(horizontal, horizontal, 2) cv.Pow(vertical, vertical, 2) cv.Add(vertical, horizontal, magnitude) cv.Convert(magnitude, magnitude32f) cv.Pow(magnitude32f, magnitude32f, 0.5) cv.Convert(magnitude32f, edges) original_rect = rect if display: cv.SetImageROI(display, rect) for threshold in range(1, 20, 1): cv.SetImageROI(thresholded, original_rect) #for i in range(30, 60, 1): if display: cv.Merge(image, image, image, None, display) cv.Copy(image, thresholded) #cv.Threshold(thresholded, thresholded, i, 255, cv.CV_THRESH_BINARY_INV) cv.AdaptiveThreshold(thresholded, thresholded, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, 17, threshold) #cv.AdaptiveThreshold(thresholded, thresholded, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 5, i) # skip rects greater than 50% thresholded summed = cv.Norm(thresholded, None, cv.CV_L1, None) / 255 / thresholded.width / thresholded.height if summed > 0.5: continue if debug: cv.ShowImage("edge", thresholded) storage = cv.CreateMemStorage(0) cv.Copy(thresholded, contour_image) contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, (0, 0)) ext.filter_contours(contours, 20, ext.LESSTHAN) groups = [] rects = [] edge_counts = [] overlappings = {} if contours: seq = contours while seq: c = ext.as_contour(ext.wrapped(seq)) r = (c.rect.x, c.rect.y, c.rect.width, c.rect.height) rects.append(r) seq = seq.h_next() similarity = 0.45 #0.3 rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3])) for rect in rects: if debug: print print "R", rect, len(groups) cv.SetImageROI(edges, (original_rect[0] + rect[0], original_rect[1] + rect[1], rect[2], rect[3])) edge_count = cv.Sum(edges)[0] / 255 / (rect[2] * rect[3]) edge_counts.append(edge_count) # cv.ShowImage("edges", edges) # cv.WaitKey(0) if debug and target_size: print "X", target_size, rect print(target_size[0] - rect[2]) / target_size[0] print(target_size[1] - rect[3]) / target_size[1] if rect[2] > rect[3] or float(rect[3])/rect[2] < 3./3 or edge_count < 0.1\ or (rect[2] == image.width and rect[3] == image.height) \ or (target_size and not 0 < (target_size[0] - rect[2]) / target_size[0] < 0.3 \ and not 0 < (target_size[1] - rect[3]) / target_size[1] < 0.05): if debug: print "rej", rect[2], ">", rect[3], "edge=", edge_count cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 0, 255), 1) cv.ShowImage("main", display) if not skip and not next: c = cv.WaitKey(0) if c == ord("a"): skip = True if c == ord("z"): next = True continue added = False for group_id, group in enumerate(groups): avg_width, avg_height, avg_y = 0, 0, 0 overlap = None c = 0 for r in group: avg_y += r[1] + r[3] / 2.0 avg_width += r[2] avg_height += r[3] irect = intersect(r, rect) if irect[2] * irect[3] > 0.2 * r[2] * r[3]: overlappings.setdefault(group_id, []).append([r, rect]) avg_y /= float(len(group)) avg_width /= float(len(group)) avg_height /= float(len(group)) if debug: print group if (abs(avg_width - rect[2]) / avg_width < similarity or \ (rect[2] < avg_width)) and \ abs(avg_height - rect[3])/ avg_height < similarity and \ abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity: group.append(rect) added = True else: pass if not added: # first char in group groups.append([rect]) if debug: print "now:" for g in groups: print g cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 0), 1) cv.ShowImage("main", display) if not skip and not next: c = cv.WaitKey(0) if c == ord("a"): skip = True if c == ord("z"): next = True if groups: #handle overlapping regions, default to average width match for group_id, over in overlappings.items(): group = groups[group_id] avg_width = 0 avg_height = 0 for r in group: avg_width += r[2] avg_height += r[3] avg_width /= float(len(group)) avg_height /= float(len(group)) for r1, r2 in over: if r2 not in group or r1 not in group: continue if debug: print "over", r1, r2, r1[2] * r1[3], r2[2] * r2[ 3], avg_width d1 = abs(r1[2] - avg_width) + abs(r1[3] - avg_height) d2 = abs(r2[2] - avg_width) + abs(r2[3] - avg_height) if d1 < d2: group.remove(r2) else: group.remove(r1) #group = max(groups, key=len) # from longest groups, find largest area groups.sort(key=len) groups.reverse() max_area = 0 mad_index = -1 for i, g in enumerate(groups[:5]): area = 0 for r in g: area += r[2] * r[3] if area > max_area: max_area = area max_index = i group = groups[max_index] # vertical splitting avg_width, avg_height, avg_y = 0, 0, 0 if debug: print "G", group for r in group: avg_y += r[1] + r[3] / 2.0 avg_width += r[2] avg_height += r[3] avg_y /= float(len(group)) avg_width /= float(len(group)) avg_height /= float(len(group)) band_rects = [] bound = bounding_rect(group) for i, rect in enumerate(rects): if edge_counts[i] < 0.1: continue if (abs(avg_width - rect[2]) / avg_width < similarity or \ (rect[2] < avg_width)) and \ (abs(avg_height - rect[3]) / avg_height < similarity or \ (rect[3] < avg_height)) and \ abs(avg_y - (rect[1] + rect[3]/2.0)) < avg_height/2: band_rects.append(rect) band_rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3])) for i, rect_a in enumerate(band_rects[:-1]): if rect_a[2] * rect_a[3] < 0.2 * avg_width * avg_height: continue merge_rects = [] for rect_b in band_rects[i + 1:]: w = avg_width m1 = rect_a[0] + rect_a[2] / 2 m2 = rect_b[0] + rect_b[2] / 2 if abs(m1 - m2) < w: merge_rects.append(rect_b) if debug: print "M", merge_rects if merge_rects: merge_rects.append(rect_a) rect = bounding_rect(merge_rects) area = 0 for r in merge_rects: area += r[2] * r[3] if (abs(avg_width - rect[2]) / avg_width < similarity or \ (rect[2] < avg_width)) and \ abs(avg_height - rect[3])/ avg_height < similarity and \ area > 0.5*(avg_width*avg_height) and \ abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity: for r in merge_rects: if r in group: group.remove(r) # merge into group new_group = [] merged = False for gr in group: area2 = max(gr[2] * gr[3], rect[2] * rect[3]) isect = intersect(gr, rect) if isect[2] * isect[3] > 0.4 * area2: x = min(gr[0], rect[0]) y = min(gr[1], rect[1]) x2 = max(gr[0] + gr[2], rect[0] + rect[2]) y2 = max(gr[1] + gr[3], rect[1] + rect[3]) new_rect = (x, y, x2 - x, y2 - y) new_group.append(new_rect) merged = True else: new_group.append(gr) if not merged: new_group.append(rect) group = new_group cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 255), 2) # avoid splitting split = False # select higher threshold if innovates significantly best_width = 0.0 if best_chars: best_area = 0.0 for rect in best_chars: best_area += rect[2] * rect[3] best_width += rect[2] best_width /= len(best_chars) area = 0.0 overlapped = 0.0 avg_width = 0.0 avg_height = 0.0 for rect in group: area += rect[2] * rect[3] avg_width += rect[2] avg_height += rect[3] for char in best_chars: section = intersect(rect, char) if section[2] * section[3] > 0: overlapped += section[2] * section[3] avg_width /= len(group) avg_height /= len(group) quotient = overlapped / area quotient2 = (area - overlapped) / best_area if debug: print area, overlapped, best_area print group print "QUO", quotient print "QUO2", quotient2 else: quotient = 0 quotient2 = 1 best_area = 0 group.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2)) best_chars.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2)) if group_range[0] <= len(group) <= group_range[1] and avg_width > 5 and avg_height > 10 and \ ((quotient2 > 0.05 and (best_area == 0 or abs(area - best_area)/best_area < 0.4)) or (quotient2 > 0.3 and area > best_area)): if debug: print "ASSIGNED", group best_chars = group best_threshold = threshold #get_patch(thresholded, original_rect) else: if debug: print "not", quotient2, len( group), avg_width, avg_height, area, best_area # best_chars = groups if debug: for rect in best_chars: cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1) cv.ShowImage("main", display) if not skip and not next: c = cv.WaitKey(0) if c == ord("a"): skip = True if c == ord("z"): next = True best_chars.sort(lambda x, y: cmp(x[0], y[0])) cv.ResetImageROI(thresholded) cv.ResetImageROI(contour_image) cv.ResetImageROI(image) cv.ResetImageROI(edges) if display: cv.ResetImageROI(display) return best_chars, best_threshold
mask = cv.LoadImage(folder + 'mask.png', 0) ha = HistAnalyzer(background_noise, mask) ha.calc_hist() ha.calc_stats() cv.ShowImage("Source", ha.avg_noise) cv.WaitKey(-1) back_sum_ls = deque() #[] for i in xrange(130): try: img = cv.LoadImage(folder + 'file' + str(i).zfill(3) + '.png') result = ha.compare_imgs(img, ha.background_noise[0]) back_sum_ls.append(float(cv.Sum(result)[0])) except: print "no training file # ", i avg = np.mean(back_sum_ls) std = np.std(back_sum_ls) print "avg and std are :", avg, std n = 0 sum_val = 0 for i in xrange(9): file_h = open( opt.batch_folder + '/object' + str(i).zfill(3) + '.pkl', 'r') res_dict = cPickle.load(file_h)
def sum_squared(img1, img2): tmp = cv.CreateImage(cv.GetSize(img1), 8, 1) cv.Sub(img1, img2, tmp) cv.Pow(tmp, tmp, 2.0) return cv.Sum(tmp)[0]
#cv.Sub(back_proj_img2, avg_noise, back_proj_img2) #cv.Sub(scratch,, back_proj_img2) cv.ShowImage("final", scratch) #cv.Sub(scratch, avg_noise, scratch2) #cv.And(scratch, back_proj_img2, scratch2) #cv.SubRS(scratch2, 255, scratch) #cv.ShowImage("final", back_proj_img) print cv.CompareHist(hist1, hist2, cv.CV_COMP_BHATTACHARYYA) #making a mask #mask = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1) #cv.SubRS(back_proj_img2, 255, back_proj_img2) #cv.SubRS(back_proj_img, 255, back_proj_img, mask=back_proj_img2) #cv.SubRS(back_proj_img, 255, back_proj_img) #cv.MorphologyEx(back_proj_img,back_proj_img, None, None, cv.CV_MOP_OPEN, 8) #cv.MorphologyEx(back_proj_img,back_proj_img, None, None, cv.CV_MOP_CLOSE, 8) #cv.ShowImage("back_projection", back_proj_img2) #cv.WaitKey(0) cv.Scale(back_proj_img, back_proj_img, 1/255.0) print "here's the sum :", cv.Sum(scratch2)
def measure(im, debug=False): size = cv.GetSize(im) npixels = size[0] * size[1] #print 'np', npixels focused = get_focus_points(im, debug) points = convert_to_points(focused) if debug: print "\t" + str( len(points)), '/', npixels, '=', len(points) / float(npixels) print "\tlen(points) =", len(points) image.show(focused, "4. Focused Points") saturation_score = 0 if not image.is_grayscale(im): edges = image.auto_edges(im) _, saturation, _ = image.split(image.rgb2hsv(im)) if debug: image.show(saturation, "5. Saturation") #saturation = image.laplace(image.gaussian(saturation, 3)) saturation = image.invert(saturation) mask = image.invert(image.threshold(im, threshold=16)) if debug: image.show(saturation, "5.3. Laplace of Saturation") cv.Set(saturation, 0, mask) cv.Set(saturation, 0, focused) if debug: image.show(mask, "5.6. Mask(focused AND invert(threshold(im, 16)))") image.show(saturation, "6. Set(<5.3>, 0, <5.6>)") saturation_score = cv.Sum(saturation)[0] / float(npixels * 255) print "\tSaturation Score:", saturation_score # light exposure h, s, v = image.split(image.rgb2hsv(im)) if debug: image.show(h, "7. Hue") image.show(s, "7. Saturation") image.show(v, "7. Value") diff = cv.CloneImage(v) cv.Set(diff, 0, image.threshold(s, threshold=16)) diff = image.dilate(diff, iterations=10) if debug: thres_s = image.threshold(s, threshold=16) image.show(thres_s, "8.3. Mask(threshold(<7.Saturation>, 16))") image.show(diff, "8.6. Dilate(Set(<7.Value>, 0, <8.3>), 10)") cdiff = cv.CountNonZero(diff) if cdiff > 0 and cdiff / float(npixels) > 0.01: test = cv.CloneImage(v) cv.Set(test, 0, image.invert(diff)) s = cv.Sum(test)[0] / float(cdiff * 255) if debug: print '\tLight Exposure Score:', s else: s = 0 if image.is_grayscale(im): return focused, (1, 1, 1, saturation_score, s) # we want to short circuit ASAP to avoid doing KMeans 50% of the image's pixels if len(points) > npixels / 2: return focused, (1, 1, 1, saturation_score, s) # we're so blurry we don't have any points! if len(points) < 1: return focused, (0, 0, 0, saturation_score, s) if debug: im2 = cv.CloneImage(im) focused_regions = image.new_from(im) cv.Set(focused_regions, 0) r = lambda x: random.randrange(1, x) groups = form_groups(points, estimated_size=min(max(int(len(points) / 1000), 2), 15)) #groups = form_groups(points, threshold=max(cv.GetSize(im))/16) #print 'groups', len(groups) hulls = draw_groups(groups, focused_regions) focused_regions = image.threshold(focused_regions, threshold=32, type=cv.CV_THRESH_TOZERO) min_area = npixels * 0.0005 densities = [h.points_per_pixel() for h in hulls if h.area() >= min_area] if debug: #image.show(focused, "Focused Points") image.show(focused_regions, "9. Focused Regions from <4>") cv.Sub( im2, image.gray2rgb( image.invert(image.threshold(focused_regions, threshold=1))), im2) image.show(im2, "10. threshold(<9>)") focused_regions = image.rgb2gray(focused_regions) densities = array(densities) c = cv.CountNonZero(focused_regions) c /= float(npixels) score = (c, densities.mean(), densities.std(), saturation_score, s) return focused, score