def doCanny(image, lowThresh, highThresh, aperture): # i have changed code a little bit from text book. With example in text book, you have to load grayscale image. But now you can load any image, no matter if RGB or GRAYSCALE # output image with single channel out = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) # canny only handles grayscale images. So convert RGB to grayscale if image.nChannels != 1: cv.CvtColor(image, out, cv.CV_RGB2GRAY) cv.Canny(out, out, lowThresh, highThresh, aperture) else: cv.Canny(image, out, lowThresh, highThresh, aperture) return out
def __refresh_canny(self): cv.Canny(self.res_smooth, self.canny, self.__canny_lo, self.__canny_hi, self.__canny_apeture * 2 + 3) #cv.Threshold(self.res_smooth, self.canny, self.__canny_lo * 2, 255, cv.CV_THRESH_BINARY) cv.ShowImage('Canny', self.canny) cv.Copy(self.canny, self.contour_in) self.contours = cv.FindContours(self.contour_in, self.c_storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE) self.__refresh_poly()
def canny_it(self): # create gray scale image of balls gray_image = cv.CreateImage((self.width, self.height), 8, 1) rospy.sleep(1) trans = cv.fromarray(self.cv_image) # (src,dst,模式:其中当code选用CV_BGR2GRAY时,dst需要是单通道图片) cv.CvtColor(trans, gray_image, cv.CV_BGR2GRAY) # create gray scale array of balls gray_array = self.cv2array(gray_image) # create a canny edge detection map of the greyscale image cv.Canny(gray_image, self.canny, 40, 200, 3) cv.ShowImage('canny', self.canny) # save Canny image file_name = self.image_dir + "egg_tray_canny.jpg" cv.SaveImage(file_name, self.canny) # 3ms wait cv.WaitKey(0)
def canny(im, low, high, aperture): assert im.channels == 1 out = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_8U, 1) cv.Canny(im, out, low, high, aperture) return out
def find_lines(frame): # Resize to 640x480 frame_small = cv.CreateMat(480, 640, cv.CV_8UC3) cv.Resize(frame, frame_small) # Threshold by distance: blank out all top pixels cv.Rectangle(frame_small, (0, 0), (640, 80), (0, 0, 0, 0), cv.CV_FILLED) frame_size = cv.GetSize(frame_small) frame_gray = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1) edges = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1) cv.CvtColor(frame_small, frame_gray, cv.CV_BGR2GRAY) cv.Canny(frame_gray, edges, 400, 400) cv.Dilate(edges, edges, cv.CreateStructuringElementEx(3, 3, 0, 0, cv.CV_SHAPE_RECT)) cv.Erode(edges, edges, cv.CreateStructuringElementEx(1, 1, 0, 0, cv.CV_SHAPE_RECT)) line_storage = cv.CreateMemStorage() lines = cv.HoughLines2(edges, line_storage, cv.CV_HOUGH_PROBABILISTIC, 1, cv.CV_PI / 180.0, 300, 100, 40) print len(lines), 'lines found' for i in range(len(lines)): line = lines[i] cv.Line(frame_small, line[0], line[1], hv2rgb(360.0 * i / len(lines), 1.0), 3, 8) cv.ShowImage('frame', frame_small) cv.ShowImage('edges', edges)
def getIris(frame): iris = [] copyImg = cv.CloneImage(frame) resImg = cv.CloneImage(frame) grayImg = cv.CreateImage(cv.GetSize(frame), 8, 1) mask = cv.CreateImage(cv.GetSize(frame), 8, 1) storage = cv.CreateMat(frame.width, 1, cv.CV_32FC3) cv.CvtColor(frame, grayImg, cv.CV_BGR2GRAY) cv.Canny(grayImg, grayImg, 5, 70, 3) cv.Smooth(grayImg, grayImg, cv.CV_GAUSSIAN, 7, 7) circles = getCircles(grayImg) iris.append(resImg) for circle in circles: rad = int(circle[0][2]) global radius radius = rad cv.Circle(mask, centroid, rad, cv.CV_RGB(255, 255, 255), cv.CV_FILLED) cv.Not(mask, mask) cv.Sub(frame, copyImg, resImg, mask) x = int(centroid[0] - rad) y = int(centroid[1] - rad) w = int(rad * 2) h = w cv.SetImageROI(resImg, (x, y, w, h)) cropImg = cv.CreateImage((w, h), 8, 3) cv.Copy(resImg, cropImg) cv.ResetImageROI(resImg) return (cropImg) return (resImg)
def find_corner_in_full_scale(self,point): point= self.m_d.scale_up(point) scale = self.m_d.scale self.m_d.set_scale(0) gray_img = self.m_d.gray_img canny_img = self.m_d.canny_img x,y = point cr = correct_rectangle((x-5,y-5,10,10), cv.GetSize(gray_img)) for img in [gray_img,canny_img]: cv.SetImageROI(img, cr) cv.Canny(gray_img, canny_img, 300, 500) conts = cv.FindContours(canny_img, cv.CreateMemStorage(), cv.CV_RETR_LIST,(cr[0],cr[1])) db.DrawContours(self.m_d.tmp_img, conts, (255,255,255), (128,128,128), 10) min =10 min_point = None while conts: for c in conts: vec = vector(point,c) len =length(vec) if len<min: min = len min_point = c conts.h_next() self.m_d.set_scale(scale) return min_point
def detect_card(grey_image, grey_base, thresh=100): diff = cv.CloneImage(grey_image) cv.AbsDiff(grey_image, grey_base, diff) edges = cv.CloneImage(grey_image) cv.Canny(diff, edges, thresh, thresh) contours = cv.FindContours(edges, cv.CreateMemStorage(0)) edge_pts = [] c = contours while c is not None: if len(c) > 10: edge_pts += list(c) if len(c) == 0: #'cus opencv is buggy and dumb break c = c.h_next() if len(edge_pts) == 0: return None hull = cv.ConvexHull2(edge_pts, cv.CreateMemStorage(0), cv.CV_CLOCKWISE, 1) lines = longest_lines(hull) perim = sum(l['len'] for l in lines) #print perim #likely to be a card. . . #if abs(perim - 1200) < 160: if perim > 700: #extrapolate the rectangle from the hull. #if our 4 longest lines make up 80% of our perimiter l = sum(l['len'] for l in lines[0:4]) #print "l = ",l if l / perim > 0.7: #we probably have a high-quality rectangle. extrapolate! sides = sorted(lines[0:4], key=lambda x: x['angle']) #sides are in _some_ clockwise order. corners = [None] * 4 # TODO: figure out why we can get an IndexError on xrange(4) try: for n in xrange(4): corners[n] = line_intersect(sides[n], sides[(n + 1) % 4]) if not all(corners): return None except IndexError: print >> sys.stderr, "detect_card() IndexError(), we should track down why this happens exactly" return None #rotate corners so top-left corner is first. #that way we're clockwise from top-left sorted_x = sorted(c[0] for c in corners) sorted_y = sorted(c[1] for c in corners) top_left = None for index, (x, y) in enumerate(corners): if sorted_x.index(x) < 2 and sorted_y.index(y) < 2: top_left = index if top_left is None: return None #return rotated list return corners[top_left:] + corners[:top_left] return None
def find_new_markers(self, do_canny=True): ''' Find markers totally without taking their previous position into consideration @param do_canny: perform edge recognition ''' for scale in range(self.max_scale, -1, -1): if len(self.not_found) == 0: return self.set_scale(scale) if do_canny: cv.Canny(self.gray_img, self.bw_img, 100, 300) cv.Copy(self.bw_img, self.tmp_img) self.tmp_img[1, 1] = 255 cont = cv.FindContours( self.tmp_img, cv.CreateMemStorage(), cv.CV_RETR_TREE) db.DrawContours( self.canny_img, cont, (255, 255, 255), (128, 128, 128), 10) # db.show([self.canny_img,self.tmp_img,self.bw_img], 'show', 0, 1) # cv.ShowImage("name", self.canny_img) # cv.ShowImage("name1", self.tmp_img) self.set_scale(0) self.scale_factor = 1 << scale self.test_contours(cont) #markers= filter(lambda sq:sq.check_square(self.img,self.gray_img),rects) return self.markers
def process_image(self, image, texture_type): spare = image #return image # get the size of the current image size = (image.width, image.height) cv.Smooth(spare, spare, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE) #out = cv.CreateImage( size, 8, 1) cannyB = cv.CreateImage(size, 8, 1) cannyR = cv.CreateImage(size, 8, 1) sobel = cv.CreateImage(size, 8, 1) yuv = cv.CreateImage(size, 8, 3) dest_canny = cv.CreateImage(size, 8, 3) gray = cv.CreateImage(size, 8, 1) cv.CvtColor(spare, yuv, cv.CV_BGR2YCrCb) cv.Split(yuv, gray, None, None, None) cv.Canny(gray, cannyB, 5, 50, 3) cv.Canny(gray, cannyR, 5, 150, 3) cv.Sobel(gray, sobel, 1, 0, 3) #cv.ConvertScale(sobel, sobel, -1, 255 ) #cv.ConvertScale(cannyR, cannyR, -1, 155 ) #cv.ConvertScale(gray, gray, -1, 255 ) #cv.ConvertScale(cannyB, cannyB, -1, 255 ) cv.Smooth(cannyR, cannyR, cv.CV_GAUSSIAN, 3, 3) cv.Smooth(cannyB, cannyB, cv.CV_GAUSSIAN, 3, 3) #cv.CvtColor( canny, canny, cv.CV_YCrCb2BGR ) #cv.Merge(sobel, gray, sobel, None, dest) cv.Merge(cannyR, cannyB, gray, None, dest_canny) #cv.Merge(sobel, sobel, sobel, None, dest_sobel) #cv.Smooth( dest, dest, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE ) #cv.ShowImage( 'canny', dest) #cv.Merge #cv.Smooth( dest_canny, dest_canny, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE ) #success = True self.prevImage = image options = {'normal': dest_canny, 'post': dest_canny} #return yuv return options[texture_type]
def processFrames(self): self.vidcap = cv2.VideoCapture(self.path) count = 0 success, image = self.vidcap.read() print success self.createWindows() while True: success, image = self.vidcap.read() if not success: return spare = cv.fromarray(image) size = (spare.width / 2, spare.height / 2) cv.Smooth(spare, spare, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE) out = cv.CreateImage(size, 8, 3) cv.PyrDown(spare, out) yuv = cv.CreateImage(size, 8, 3) gray = cv.CreateImage(size, 8, 1) canny = cv.CreateImage(size, 8, 1) sobel = cv.CreateImage(size, 8, 1) harris = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1) cv.CvtColor(out, yuv, cv.CV_BGR2YCrCb) cv.Split(yuv, gray, None, None, None) cv.Canny(gray, canny, 50, 200, 3) cv.CornerHarris(gray, harris, 3) cv.Sobel(gray, sobel, 1, 0, 3) cv.ConvertScale(canny, canny, -1, 255) cv.ConvertScale(sobel, sobel, -1, 255) for y in range(0, out.height): for x in range(0, out.width): harr = cv.Get2D(sobel, y, x) if harr[0] < 10e-06: cv.Circle(out, (x, y), 2, cv.RGB(155, 0, 25)) #cv2.imwrite("frame%d.jpg" % count, np.asarray(canny[:,:])) cv.ShowImage('canny', canny) #cv.ShowImage( 'harris' , harris ) cv.ShowImage('sobel', sobel) cv.ShowImage('output', out) if cv2.waitKey(1) == 27: break count += 1 return
def findLines(src): dst = cv.CreateImage(cv.GetSize(src), 8, 1) color_dst = cv.CreateImage(cv.GetSize(src), 8, 3) storage = cv.CreateMemStorage(0) cv.Canny(src, dst, 50, 200, 3) cv.CvtColor(dst, color_dst, cv.CV_GRAY2BGR) return cv.HoughLines2(dst, storage, cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0)
def get_elements(filename, treshold=50, minheight=15, minarea=200, elements=6): src = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE) test = cv.CreateImage(cv.GetSize(src), 32, 3) dst = cv.CreateImage(cv.GetSize(src), 8, 1) storage = cv.CreateMemStorage(0) cv.Canny(src, dst, treshold, treshold * 3, 3) storage = cv.CreateMemStorage(0) seqs = cv.FindContours(dst, storage, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_NONE, (0, 0)) res = [] c = seqs.h_next() while True: if not c: break box = cv.BoundingRect(c) area = box[2] * box[3] #and (area > minarea) if (box[3] > minheight): res.append(box) c = c.h_next() if len(res) < elements: while len(res) < elements: m = 0 c = 0 for i, e in enumerate(res): if e[3] > m: m = e[3] c = i big = res.pop(c) res.append((big[0], big[1], int(big[2] * 1.0 / 2), big[3])) res.append((big[0] + int(big[2] * 1.0 / 2), big[1], int(big[2] * 1.0 / 2), big[3])) #for box in res: # cv.Rectangle(dst, (box[0],box[1]), (box[0]+box[2],box[1]+box[3]), cv.RGB(255,255,255)) #cv.ShowImage('Preview2',dst) #cv.WaitKey() imgs = [] print len(res) for box in res: cv.SetImageROI(src, box) tmp = cv.CreateImage((box[2], box[3]), 8, 1) cv.Copy(src, tmp) hq.heappush(imgs, (box[0], tmp)) cv.ResetImageROI(src) res = [hq.heappop(imgs)[1] for i in xrange(len(res))] return res
def edge_detect(image): gray = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1) cv.CvtColor(image, gray, cv.CV_BGR2GRAY) edges = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1) threshold1 = 100 threshold2 = 50 #aperature_size = 7 cv.Canny(gray, edges, threshold1, threshold2) #,aperature_size) return edges
def getCanny(img): yuv = CreateImage(GetSize(img), 8, 3) gray = CreateImage(GetSize(img), 8, 1) CvtColor(img, yuv, CV_BGR2YCrCb) Split(yuv, gray, None, None, None) canny = cv.CreateImage(cv.GetSize(img), 8, 1) cv.Canny(gray, canny, 50, 200) cv.SaveImage('canny.png', canny) return canny
def getCannyEdges(imgFile): image = cv.LoadImage(os.path.join('./', 'data', imgFile), cv.CV_LOAD_IMAGE_COLOR) edgeImage = cv.CreateImage((250, 250), 8, 1) grayImage = cv.CreateImage((250, 250), 8, 1) cv.CvtColor(image, grayImage, cv.CV_BGR2GRAY) cv.Canny(grayImage, edgeImage, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) return numpy.asarray(edgeImage[:, :])
def edgeConv(StrImg): image = cv.LoadImageM(StrImg) gray = cv.CreateImage(cv.GetSize(image), 8, 1) edges = cv.CreateImage(cv.GetSize(image), 8, 1) cv.CvtColor(image, gray, cv.CV_BGR2GRAY) cv.Canny(gray, edges, 50, 200) #print edges[399,699] return edges
def canny_it(self, iteration): print "24 canny_it" #called by 27# #called by 24 (iterative)# #called by 23# if self.save_images: # save raw image of chess board file_name = self.image_dir + "chess_board_" + str( iteration) + ".jpg" self.cv_image = self.cv_image cv.SaveImage(file_name, cv.fromarray(self.cv_image)) # create an empty image variable, the same dimensions as our camera feed. gray = cv.CreateImage((cv.GetSize(cv.fromarray(self.cv_image))), 8, 1) # convert the image to a grayscale image cv.CvtColor(cv.fromarray(self.cv_image), gray, cv.CV_BGR2GRAY) # display image on head monitor font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 1) position = (30, 60) cv.PutText(cv.fromarray(self.cv_image), "Buscando Tablero", position, font, self.white) msg = cv_bridge.CvBridge().cv2_to_imgmsg(self.cv_image, encoding="bgr8") self.pub.publish(msg) # create a canny edge detection map of the greyscale cv.Canny(gray, self.canny, self.canny_low, self.canny_high, 3) # display the canny transformation cv.ShowImage("Canny Edge Detection", self.canny) if self.save_images: # save Canny image of chess board file_name = self.image_dir + "canny_board_" + str( iteration) + ".jpg" cv.SaveImage(file_name, self.canny) # flood fill edge of image to leave only objects "go to 9" self.flood_fill_edge(self.canny) #//back from 9, "go to 8" chess_board_centre = self.look_for_chess_board(self.canny) #//back from 8 # 3ms wait cv.WaitKey(3) while chess_board_centre[0] == 0: if random.random() > 0.6: self.baxter_ik_move(self.limb, self.dither()) #"go to 24" (iterate) chess_board_centre = self.canny_it(iteration) #//back from 24 return chess_board_centre
def edge_trackbar(position): #cv.Smooth(threshold_img,edge_img,cv.CV_BLUR,3,3,0) cv.Smooth(src, edge_img, cv.CV_BLUR, 3, 3, 0) #cv.Not(src,edge_img) #run the edge detector on threshold scale cv.Canny(src, edge_img, position, position * 3, 3) #show the image cv.ShowImage(window_edge, edge_img)
def edgedetect(I_np, LOW_T=75, RATIO=3): I_cv = cv.fromarray(I_np) I_cv8U = cv.CreateMat(I_cv.rows, I_cv.cols, cv.CV_8U) cv.Convert(I_cv, I_cv8U) edges = cv.CreateMat(I_cv8U.rows, I_cv8U.cols, cv.CV_8U) cv.Canny(I_cv8U, edges, LOW_T, LOW_T * RATIO) edges_32f = cv.CreateMat(edges.rows, edges.cols, cv.CV_32F) cv.Convert(edges, edges_32f) edges_np = np.array(edges_32f) return edges_np
def main(argv): default_file = 'images/board.JPEG' filename = argv[0] if len(argv) > 0 else default_file # Loads an image src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE) # Check if image is loaded fine if src is None: print('Error opening image!') print('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n') return -1 dst = cv.Canny(src, 50, 200, None, 3) # Copy edges to the images that will display the results in BGR cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR) cdstP = np.copy(cdst) lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0) if lines is not None: for i in range(0, len(lines)): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0 = a * rho y0 = b * rho pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) cv.line(cdst, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA) linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10) if linesP is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 3, cv.LINE_AA) y = int(round(1536 / 3)) x = int(round(2048 / 3)) src = cv.resize(cdst, (x, y)) # Resize image cdst = cv.resize(cdst, (x, y)) # Resize image cdstP = cv.resize(cdstP, (x, y)) # Resize image cv.imshow("Source", src) cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst) cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP) cv.imwrite("./houghLines.png", cdstP) cv.waitKey() return 0
def canny_it(self, iteration): ####图片格式转化################################# cv_image_trans = cv.fromarray(self.cv_image) #保存self.cv_image if self.save_images: # save raw image of ball tray file_name = self.image_dir + "ball_tray_" + str(iteration) + ".jpg" cv.SaveImage(file_name, self.cv_image) # create an empty image variable, the same dimensions as our camera feed. gray = cv.CreateImage((cv.GetSize(cv_image_trans)), 8, 1) # convert the image to a grayscale image #(src,dst,code) cv.CvtColor(cv_image_trans, gray, cv.CV_BGR2GRAY) # display image on head monitor font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 1) position = (30, 60) #cv.PutText(cv_image_trans, "Looking for ball tray", position, font, self.white) msg = cv_bridge.CvBridge().cv2_to_imgmsg(self.cv_image, encoding="bgr8") self.pub.publish(msg) # create a canny edge detection map of the greyscale image cv.Canny(gray, self.canny, self.canny_low, self.canny_high, 3) # display the canny transformation cv.ShowImage("Canny Edge Detection", self.canny) #保存self.canny if self.save_images: # save Canny image of ball tray file_name = self.image_dir + "canny_tray_" + str( iteration) + ".jpg" cv.SaveImage(file_name, self.canny) # flood fill edge of image to leave only objects self.flood_fill_edge(self.canny) ball_tray_centre = self.look_for_ball_tray(self.canny) #可能会返回(0,0) # 3ms wait cv.WaitKey(3) #第一次进来还未找到时起作用 while ball_tray_centre[0] == 0: if random.random() > 0.6: self.baxter_ik_move(self.limb, self.dither()) ball_tray_centre = self.canny_it(iteration) return ball_tray_centre
def getCannyMask(image, threshold): """convert image to gray scale run the edge dector returns mask """ res = cv.GetSize(image) gray = cv.CreateImage (res, 8, 1) mask = cv.CreateImage (res, 8, 1) cv.CvtColor(image, gray, cv.CV_BGR2GRAY) cv.Smooth (gray, mask, cv.CV_BLUR, 3, 3, 0) cv.Not(gray, mask) cv.Canny(gray, mask, threshold, threshold * 3, 3) return mask
def find_squares4(color_img): """ Finds multiple squares in image Steps: -Use Canny edge to highlight contours, and dilation to connect the edge segments. -Threshold the result to binary edge tokens -Use cv.FindContours: returns a cv.CvSequence of cv.CvContours -Filter each candidate: use Approx poly, keep only contours with 4 vertices, enough area, and ~90deg angles. Return all squares contours in one flat list of arrays, 4 x,y points each. """ #select even sizes only width, height = (color_img.width & -2, color_img.height & -2) timg = cv.CloneImage(color_img) # make a copy of input image gray = cv.CreateImage((width, height), 8, 1) # select the maximum ROI in the image cv.SetImageROI(timg, (0, 0, width, height)) # down-scale and upscale the image to filter out the noise pyr = cv.CreateImage((width / 2, height / 2), 8, 3) cv.PyrDown(timg, pyr, 7) cv.PyrUp(pyr, timg, 7) tgray = cv.CreateImage((width, height), 8, 1) squares = [] # Find squares in every color plane of the image # Two methods, we use both: # 1. Canny to catch squares with gradient shading. Use upper threshold # from slider, set the lower to 0 (which forces edges merging). Then # dilate canny output to remove potential holes between edge segments. # 2. Binary thresholding at multiple levels N = 11 for c in [0, 1, 2]: #extract the c-th color plane cv.SetImageCOI(timg, c + 1) cv.Copy(timg, tgray, None) cv.Canny(tgray, gray, 0, 50, 5) cv.Dilate(gray, gray) squares = squares + find_squares_from_binary(gray) # Look for more squares at several threshold levels for l in range(1, N): cv.Threshold(tgray, gray, (l + 1) * 255 / N, 255, cv.CV_THRESH_BINARY) squares = squares + find_squares_from_binary(gray) return squares
def canny(im,threshold1=40.0,threshold2=100.0,aperture_size=3,sigma=None): ''' void cvCanny( const CvArr* image, CvArr* edges, double threshold1, double threshold2, int aperture_size=3 ); ''' gray = im.asOpenCVBW() edges = cv.CreateImage( cv.GetSize(gray), 8, 1 ); if sigma!=None: cv.Smooth(gray,gray,cv.CV_GAUSSIAN,int(sigma)*4+1,int(sigma)*4+1,sigma,sigma) cv.Canny(gray,edges,threshold1,threshold2,aperture_size) return pv.Image(edges)
def processFrame(self): startTime = time.time() logging.debug("Frame %d at %s", self.N, self.formatTime(startTime)) self.N += 1 logging.debug("Capturing a frame") frame = self.capture.getFrame() logging.debug("Entering preprocessing") standard, bgsub_vals, bgsub_mask = self.pre.preprocess(frame) logging.debug("Entering feature extraction") hist_props_bgsub = self.histogram.calcHistogram(standard) hist_props_abs = self.histogram.calcHistogram(bgsub_vals) self.threshold.updateBGSubThresholds(hist_props_bgsub) #self.threshold.updateAbsThresholds(hist_props_abs) ents = self.featureEx.features(standard, self.threshold) #ents = self.featureEx.features(bgsub_vals, self.threshold) logging.debug("Detected entities:", ents) logging.debug("Entering interpreter") self.interpreter.interpret(ents) logging.debug("Entering interpreter") self.world.update(startTime, ents) try: bgsub = self.pre.remove_background(standard) self.gui.updateWindow('raw', frame) self.gui.updateWindow('mask', bgsub_mask) self.gui.updateWindow('foreground', bgsub_vals) self.gui.updateWindow('bgsub', bgsub) self.gui.updateWindow('standard', standard) canny = cv.CreateImage(self.pre.cropSize, 8, 1) # adaptive = cv.CreateImage(self.pre.cropSize, 32,3) # tmp = cv.CreateImage(self.pre.cropSize, 8,3) # cv.Convert(standard, adaptive) cv.CvtColor(bgsub, canny, cv.CV_BGR2GRAY) cv.Threshold(canny, canny, 150, 255, cv.CV_THRESH_OTSU) # cv.Threshold(canny, canny, 100, 255, cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C) # cv.Sobel(adaptive, adaptive, 1,1,1) # cv.Convert(adaptive, tmp) # cv.ConvertScale(tmp, tmp, 10) # cv.CvtColor(tmp, canny, cv.CV_BGR2GRAY) # cv.Threshold(canny,canny, 50, 255, cv.CV_THRESH_BINARY) cv.Canny(canny, canny, 100, 180, 3) self.gui.updateWindow('adaptive', canny) self.gui.draw(ents, startTime) except Exception, e: logging.error("GUI failed: %s", e) if self.debug: raise
def __init__(self, img): self.img = cv.fromarray(img) self.width = self.img.cols self.height = self.img.rows self.cannyImg = cv.CreateMat(self.height, self.width, cv.CV_8UC1) self.outputImg = cv.CreateMat(self.height, self.width, cv.CV_8UC3) self.integralImg = cv.CreateMat(self.height + 1, self.width + 1, cv.CV_32FC1) self.calculatedImg = cv.CreateMat(self.height, self.width, cv.CV_8UC1) cv.Canny(self.img, self.cannyImg, 40, 100) cv.Integral(self.cannyImg, self.integralImg) cv.Set(self.calculatedImg, 0) cv.Set(self.outputImg, 0)
def read_image_canny(filename): img = cv.LoadImage(filename, 1) gray = cv.CreateImage((img.width, img.height), 8, 1) edge = cv.CreateImage((img.width, img.height), 8, 1) cv.CvtColor(img, gray, cv.CV_BGR2GRAY) cv.Smooth(gray, edge, cv.CV_BLUR, 3, 3, 0) #cv.Not(gray, edge) # run the edge dector on gray scale cv.Canny(gray, edge, 80, 160, 3) cv.Not(edge, edge) cv.Smooth(edge, edge, cv.CV_GAUSSIAN, 3, 3, 0) a_img = cv2array(edge) return a_img.flatten(1)
def on_trackbar(position): cv.Smooth(gray, edge, cv.CV_BLUR, 3, 3, 0) cv.Not(gray, edge) # run the edge dector on gray scale cv.Canny(gray, edge, position, position * 3, 3) # reset cv.SetZero(col_edge) # copy edge points cv.Copy(im, col_edge, edge) # show the im cv.ShowImage(win_name, col_edge)
def get_canny_img(img): size = sizeOf(img) plate = cv.CreateImage(size, 8, 1) cv.Set(plate, 255) for k in (50, 100,150,200,250): # k=100 edges = cv.CreateImage(size, 8, 1) cv.Canny(img, edges, k-20, k) # show_image(edges) if k >= 100: cv.Dilate(edges,edges) else: k+=50 # cv.Erode(edges,edges) cv.Set(plate, 255 - k, edges) return plate