def _decode_rect(self, rect): ''' Reads binary code within rect using draw_img image Returns orientation of the code and decoded code @param rect: rectangle to use ''' gs = self.GRID_SIZE + 2 codel, _ = self._extract_code(rect, gs, .5, self.m_d.gray_img) code = self._get_code_matrix(codel, gs) if code is None: return self.FAILED, 0 bor = 0 for b in self.border: if code[b[0], b[1]] == 0: bor += 1 if bor < len(self.border) - 1: return self.FAILED, 0 first = self._find_first_corner(code, 1) if first == self.FAILED: return self.FAILED, 0 self._rotate_code(code, first) dec = 0 if self.m_d.flip_H: cv.Transpose(code, code) for (x, y) in reversed(self.code_points): dec *= 2 dec += cv.Get2D(code, y + 1, x + 1)[0] return first, dec
def getReadableWordSegment(self, wordNumber): 'returns transposed form of line segment Image' image = self.segmenter.getSegment(wordNumber) transImg = cv.CreateImage((image.height, image.width), cv.IPL_DEPTH_8U, 1) cv.Transpose(image, transImg) return transImg
def step(self, show): r_image = cv.QueryFrame(self.capture) if Camera.EXTERNAL: self.image = cv.CreateImage((r_image.height, r_image.width), r_image.depth, r_image.channels) cv.Transpose(r_image, self.image) cv.Flip(self.image, self.image, flipMode=0) else: self.image = r_image self.detect_faces() for (x,y,w,h) in self.faces: cv.PutText(self.image, "LOOK AT THIS IDIOT", (0, 30), Camera.FONT, Camera.RED) cv.PutText(self.image, str(time.time()), (self.image.width - 275, self.image.height - 20), Camera.FONT, Camera.RED) cv.Line(self.image, (0, y+h/2), (self.image.width, y+h/2), Camera.RED) cv.Line(self.image, (x+w/2, 0), (x+w/2, self.image.height), Camera.RED) cv.Circle(self.image, (x+w/2, y+h/2), min(h/2, w/2), Camera.RED, thickness=2) cv.Circle(self.image, (x+w/2, y+h/2), min(h/8, w/8), Camera.RED) #arrow(self.image, (200, 40), (x, y), Camera.WHITE) if show: print self.faces cv.ShowImage("w1", self.image) cv.WaitKey(1) return self.image
def _rotate_code(self, cod, ori): ''' Rotates matrix cod depending on the value of ori @param cod: @param ori: ''' if ori == self.TOP_LEFT: pass elif ori == self.TOP_RIGHT: cv.Transpose(cod, cod) cv.Flip(cod) return cod elif ori == self.BOT_RIGHT: cv.Flip(cod, cod, -1) else: cv.Transpose(cod, cod) cv.Flip(cod, cod, 1)
def rotateImage(image): # transposed image timg = cv.CreateImage((image.height, image.width), image.depth, image.channels) # rotate clockwise cv.Transpose(image, timg) cv.Flip(timg, timg, flipMode=1) return timg
def FormatImage(img, oimg, off, scale, correction): global i01 #print img.height,img.width #print oimg.height,oimg.width cv.Transpose(img,oimg) cv.Flip(oimg,None,0) if(correction): cv.AddS(oimg, off, oimg) cv.Div(oimg, i01, oimg, scale)
def test_diebold(): testdir = 'diebold_test' # 0.) Gather test data test_data = {} # maps {str imgpath: (int orient, str decoding)} for dirpath, dirnames, filenames in os.walk(testdir): for imgname in [f for f in filenames if is_img_ext(f)]: orient = VERTICAL if 'vert' in imgname else HORIZONTAL imgpath = os.path.join(dirpath, imgname) foo = os.path.splitext(imgname)[0].split( '_')[0] if orient == VERTICAL else os.path.splitext(imgname)[0] resname = foo + '.res' respath = os.path.join(dirpath, resname) decoding = open(respath, 'r').readlines()[0].strip() test_data[imgpath] = (orient, decoding) markpath = 'diebold_mark_v2.png' mark = cv.LoadImage(markpath, cv.CV_LOAD_IMAGE_GRAYSCALE) w, h = cv.GetSize(mark) mark_rot = cv.CreateImage((h, w), mark.depth, mark.channels) cv.Transpose(mark, mark_rot) for i, (imgpath, (orient, decoding)) in enumerate(test_data.iteritems()): I = cv.LoadImage(imgpath, cv.CV_LOAD_IMAGE_GRAYSCALE) syms = parse_patch( I, cv.GetSize(mark) if orient == HORIZONTAL else cv.GetSize(mark_rot), orient=orient) decoding_guess = ''.join(t[0] for t in syms) print decoding == decoding_guess print " {0}".format(imgpath) # Draw marks w_mark, h_mark = cv.GetSize( mark) if orient == HORIZONTAL else cv.GetSize(mark_rot) Icolor = cv.LoadImage(imgpath, cv.CV_LOAD_IMAGE_COLOR) for idx, (sym, x1) in enumerate(syms): if orient == HORIZONTAL: cv.Line(Icolor, (x1, 0), (x1, h_mark - 1), cv.CV_RGB(230, 0, 0)) cv.Line(Icolor, (x1 + w_mark, 0), (x1 + w_mark, h_mark - 1), cv.CV_RGB(0, 0, 230)) else: y1 = x1 cv.Line(Icolor, (0, y1), (w_mark - 1, y1), cv.CV_RGB(230, 0, 0)) cv.Line(Icolor, (0, y1 + h_mark - 1), (w_mark - 1, y1 + h_mark - 1), cv.CV_RGB(0, 0, 230)) cv.SaveImage("_Icolor_res_{0}.png".format(i), Icolor) return True
def get_frame(self): # get frame frame = cv.QueryFrame(self.cam) # crop frame cv.SetImageROI(frame, (CROP_X, CROP_Y, CROP_H, CROP_W)) raw = cv.CreateImage(cv.GetSize(frame), frame.depth, frame.channels) cv.Copy(frame, raw) cv.ResetImageROI(frame) # rotate frame - only LCD needed lcd = cv.CreateImage((raw.height, raw.width), raw.depth, raw.channels) cv.Transpose(raw, lcd) cv.Flip(lcd, lcd, flipMode=1) # filtering cv.Smooth(lcd, lcd, cv.CV_GAUSSIAN, 3, 0) return lcd
##GUI #Enable these lines to allow mouse interaction with the polar transform ##GUI cv.SetMouseCallback('cam', on_mouse) cv.SetMouseCallback('unwrapped', on_mouse) #on_mouse(cv.CV_EVENT_LBUTTONDOWN, centerX, centerY, None, None) # The magic number M determines how deep the polar transformation goes. M = 69 #This is the main loop while True: cam = cv.QueryFrame(capture) cv.LogPolar(cam, polar, (centerX, centerY), M+1, cv.CV_INTER_NN) #possible speedup - get subrect src #unwrapped = cv.GetSubRect(polar,(280,0,40,360)) #cv.Transpose(unwrapped, unwrapped) cv.Transpose(cv.GetSubRect(polar,(280,0,40,360)), unwrapped) cv.Flip(unwrapped) #just for viewing (possible speedup) cv.InRangeS(unwrapped, lower, upper, cones) cv.Erode(cones, cones) # just once might be too much, but unavoidable k = cv.CreateStructuringElementEx(3, 43, 1, 1, cv.CV_SHAPE_RECT) # create a 3x43 rectangular dilation element k cv.Dilate(cones, cones, k) #Display (should probably be disabled with a usage flag) cv.ShowImage('cam', cam) cv.ShowImage('unwrapped', unwrapped) cv.ShowImage('cones', cones) #cv.ShowImage('polar', polar) #cv.ShowImage('hsvcopy', hsvcopy)
def __init__(self): self.pixels_per_scanline = rospy.get_param('~pixels_per_scanline') self.scanner_info_file_name = rospy.get_param( '~scanner_info_file_name') self.threshold = rospy.get_param('~threshold') self.mutex = threading.RLock() self.image_update_flag = threading.Event() self.bridge = CvBridge() rospy.Subscriber("image_stream", sensor_msgs.msg.Image, self.update_image) rospy.loginfo("Waiting for camera info...") self.camera_info = rospy.wait_for_message('camera_info', sensor_msgs.msg.CameraInfo) rospy.loginfo("Camera info received.") rospy.loginfo("Waiting for projector info service...") rospy.wait_for_service('get_projector_info') rospy.loginfo("Projector info service found.") get_projector_info = rospy.ServiceProxy('get_projector_info', projector.srv.GetProjectorInfo) self.projector_info = get_projector_info().projector_info self.projector_dimensions = (self.projector_info.width, self.projector_info.height) self.number_of_scanlines = [] for i in range(2): self.number_of_scanlines.append( get_number_of_scanlines(self.projector_dimensions[i], self.pixels_per_scanline)) self.read_scanner_info() rospy.loginfo("Generating projection patterns...") graycodes = [] for i in range(max(self.number_of_scanlines)): graycodes.append(graycodemath.generate_gray_code(i)) self.number_of_projection_patterns = [] for i in range(2): self.number_of_projection_patterns.append( int(math.ceil(math.log(self.number_of_scanlines[i], 2)))) self.positive_projections = [] self.negative_projections = [] for i in range(2): self.positive_projections.append([]) self.negative_projections.append([]) for j in range(self.number_of_projection_patterns[i]): cross_section = cv.CreateMat(1, self.projector_dimensions[i], cv.CV_8UC1) #Fill in cross section with the associated bit of each gray code for pixel in range(self.projector_dimensions[i]): scanline = pixel // self.pixels_per_scanline scanline_value = graycodemath.get_bit( graycodes[scanline], j) * 255 cross_section[0, pixel] = scanline_value #If we're doing horizontal scanning, transpose the cross section if i is 1: cross_section_transpose = cv.CreateMat( self.projector_info.height, 1, cv.CV_8UC1) cv.Transpose(cross_section, cross_section_transpose) cross_section = cross_section_transpose #Repeat the cross section over the entire image positive_projection = cv.CreateMat(self.projector_info.height, self.projector_info.width, cv.CV_8UC1) cv.Repeat(cross_section, positive_projection) #Create a negative of the pattern for thresholding negative_projection = cv.CreateMat(self.projector_info.height, self.projector_info.width, cv.CV_8UC1) cv.Not(positive_projection, negative_projection) #Fade the borders of the patterns to avoid artifacts at the edges of projection positive_projection_faded = fade_edges(positive_projection, 20) negative_projection_faded = fade_edges(negative_projection, 20) self.positive_projections[i].append(positive_projection_faded) self.negative_projections[i].append(negative_projection_faded) rospy.loginfo("Waiting for projection setting service...") rospy.wait_for_service('set_projection') rospy.loginfo("Projection setting service found.") self.set_projection = rospy.ServiceProxy('set_projection', projector.srv.SetProjection) self.point_cloud_msg = None rospy.Service("~get_point_cloud", graycode_scanner.srv.GetPointCloud, self.handle_point_cloud_srv) point_cloud_pub = rospy.Publisher('~point_cloud', sensor_msgs.msg.PointCloud) rospy.loginfo("Ready.") rate = rospy.Rate(1) while not rospy.is_shutdown(): if self.pixel_associations_msg is not None: pixel_associations_pub.publish(self.pixel_associations_msg) if self.point_cloud_msg is not None: point_cloud_pub.publish(self.point_cloud_msg) rate.sleep()
def __countVertically(self,img,pixelValue): tempImg=cv.CreateImage((img.height,img.width), cv.IPL_DEPTH_8U, 1) cv.Transpose(img, tempImg) countArr=self.__countHorizontally(tempImg, pixelValue) return countArr
cascade = cv.Load(HAAR_CASCADE_PATH) faces = [] print 'foo' i = 0 last_center = (0, 0) def delta_to((x, y, w, h)): center = (x + w / 2, y + h / 2) return abs(center[0] - last_center[0]) + abs(center[1] - last_center[1]) while True: image = cv.QueryFrame(capture) img = image timg = cv.CreateImage((img.height, img.width), img.depth, img.channels) cv.Transpose(image, timg) image = timg # Only run the Detection algorithm every 5 frames to improve performance if i % 5 == 0: faces = detect_faces(image) if faces: faces.sort(key=delta_to) x, y, w, h = faces[0] center = (x + w / 2, y + h / 2) exp = (200, 350) d = center[0] - exp[0], -(center[1] - exp[1]) def mkp(v): if abs(v) < 50:
def __rotateImage(self,image): transImg=cv.CreateImage((image.height,image.width), cv.IPL_DEPTH_8U, 1) cv.Transpose(image, transImg) return transImg
def segment(self, image): transImg = cv.CreateImage((image.height, image.width), cv.IPL_DEPTH_8U, 1) cv.Transpose(image, transImg) 'returns Number of available line segments' return self.segmenter.segmentHorizontally(transImg)
def rotate(img): timg = cv.CreateImage((img.height, img.width), img.depth, img.channels) cv.Transpose(img, timg) cv.Flip(timg, timg, flipMode=1) return timg