Ejemplo n.º 1
1
def approx4pts( poly ):
  ret = cv2.approxPolyDP( poly, epsilon = 30, closed=True )
  if len(ret) == 4:
    return ret
  if len(ret) < 4:
    return cv2.approxPolyDP( poly, epsilon = 20, closed=True )
  return cv2.approxPolyDP( poly, epsilon = 50, closed=True )
Ejemplo n.º 2
1
    def __test_safety_zone_pic(pic):
        hsv = cv2.cvtColor(pic.img, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, Tester.__LOWER, Tester.__UPPER)
        mask = cv2.erode(mask, None, iterations=2)
        mask = cv2.GaussianBlur(mask, (1, 1), 0)

        contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)[1:3]

        i = -1
        for c in contours:
            i += 1
            perimeter = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, 0.03 * perimeter, True)
            if hierarchy[0][i][3] != -1 or cv2.contourArea(
                    c) < 200 or not cv2.isContourConvex(approx):
                continue
            if len(approx) == 4:
                children = Tester.__get_children(hierarchy, i, contours)
                if len(children) > 0:
                    bigchild = Tester.__biggest_child(children)
                    cv2.drawContours(frame, [bigchild], -1, (0, 255, 0), 10)
                    blurred = cv2.GaussianBlur(hsv, (5, 5), 0)
                    centerpt = Tester.__center_of_contour(bigchild)
                    if Tester.__is_point_red(centerpt, blurred):
                        perimeterchild = cv2.arcLength(bigchild, True)
                        approxchild = cv2.approxPolyDP(bigchild,
                                                       0.01 * perimeter,
                                                       True)
                        if len(approxchild) > 6:
                            return Tester.SAFETY_ZONE_DETECTED, centerpt
        return Tester.NOTHING_DETECTED, (0, 0)
Ejemplo n.º 3
0
def detect(image): #returns list of detected stickers
	global borders
	ret = []
	image = cv2.erode(image,kernel(2))
	contours, hierarchy = cv2.findContours(image,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
	for i in range(len(contours)):
		cnt = contours[i]
		sticker = h & 0
		cv2.drawContours(sticker, contours, i, 255 ,-1)
		border = cv2.dilate(sticker,kernel(6)) & white
		borderSize = cv2.countNonZero(border)
		borders = borders | border
		center, radius = cv2.minEnclosingCircle(cnt)
		if (radius>2 and borderSize>radius*2):
			hull = cv2.convexHull(cnt)
			hull = sharpen(hull)
			epsilon=0;
			approx=cv2.approxPolyDP(hull,epsilon,1)
			while (len(approx)>4):
				epsilon+=0.1
				approx=cv2.approxPolyDP(hull,epsilon,1)
			if (len(approx)==4 and epsilon<4):
				cv2.drawContours(tetragons, [approx], 0, 255 ,-1)
				cv2.drawContours(selected, contours, i, 255 ,-1)
				st = stickerinfo(approx, epsilon)
				ret.append(st)
			cv2.drawContours(hullimage, [hull], 0, 255 ,-1)
	return ret
Ejemplo n.º 4
0
def find_contour(ctx):
	contour = False
	max_area = 0.0
	ctx.temp_image1 = toIpl(ctx.temp_image1)
	ctx.thr_image = toIpl(ctx.thr_image)

	cv.Copy(ctx.thr_image, ctx.temp_image1)
	ctx.temp_image1 = toNumpy(ctx.temp_image1)
	contours, hierarchy = cv2.findContours(ctx.temp_image1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

	#cv.FindContours(ctx.temp_image1, ctx.temp_st, contours, sizeof(cv.Contour), cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE, cv.cvPoint(0, 0));
	
	#cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])  contours, hierarchy
	#cv.FindContours(image, storage, mode=CV_RETR_LIST, method=CV_CHAIN_APPROX_SIMPLE, offset=(0, 0))  contours


	for tmp in contours:
		area = fabs(cv2.contourArea(tmp))

		if area > max_area:
			max_area = area
			contour = tmp

	'''this doesnt run '''

	if type(contour) != 'bool':
		print type(contour)
		#Python: cv2.approxPolyDP(curve, epsilon, closed[, approxCurve])  approxCurve
		#C: CvSeq* cvApproxPoly(const void* src_seq, int header_size, CvMemStorage* storage, int method, double eps, int recursive=0 )
		#contour = cv.cvApproxPoly(contour, sizeof(cv.CvContour), ctx.contour_st, cv.CV_POLY_APPROX_DP, 2, 1)
		try:
			cv2.approxPolyDP(contour, 2, False, ctx.contour)
		except ValueError:
			print 'error, unknown'
Ejemplo n.º 5
0
    def buildMapWithAllFilter(self, frame, map):
        blurMapImage = cv2.GaussianBlur(frame, (5, 5), 0)
        for gray in cv2.split(blurMapImage):
            for threshold in xrange(0, 255, 24):
                if threshold == 0:
                    binary = cv2.Canny(gray, 0, 100, apertureSize=5)
                    binary = cv2.dilate(binary, None)
                else:
                    retval, binary = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY)
                contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
                for contour in contours:
                    contour_len = cv2.arcLength(contour, True)
                    lessPreciseContour = cv2.approxPolyDP(contour, 0.05*contour_len, True)
                    contour = cv2.approxPolyDP(contour, 0.02*contour_len, True)

                    if cv2.contourArea(contour) > self.MIN_SHAPE_SIZE and cv2.isContourConvex(contour) and cv2.contourArea(contour) < 3000:
                        myShape = self.shapeFactory.ConstructShape(contour)
                        if myShape.isEqualEdges() and myShape.checkAngleValue():
                            map.addShape(myShape)

                    if cv2.contourArea(contour) > self.LIMIT_SIZE and cv2.isContourConvex(contour):
                        if len(contour) == 4:
                            map.setMapLimit(contour)

                    if cv2.contourArea(lessPreciseContour) > self.LIMIT_SIZE and cv2.isContourConvex(lessPreciseContour):
                        if len(lessPreciseContour) == 4:
                            map.setMapLimit(lessPreciseContour)

        self.buildByColorClosing(frame, map)
        self.buildByColorOpening(frame, map)
        map.setShapesColor(frame)
        map.filterRobot()
        map.deleteBlackShapes()

        return map
Ejemplo n.º 6
0
    def search_min_area_rect(self, image_path):
        contours = self.get_contours(image_path)
        regions = []
        for con in contours:
            cv2.approxPolyDP(con, 3, True)
            #box = region.rect(cv2.boundingRect(con))
            box = region.rotated_rect(cv2.minAreaRect(con))
            regions.append(box)

        return regions
Ejemplo n.º 7
0
def generate_file():

	if request.method == 'POST':
		bw_image = cv2.imread(request.json['url'],0)
		kernel = np.ones((1.5,1.5),np.uint8)
		er_image = cv2.erode(bw_image,kernel)
		kernel = np.ones((2,2),np.uint8)
		di_image = cv2.dilate(er_image,kernel, iterations=1)

		#find contours
		mo_image = di_image.copy()
		contour0 = cv2.findContours(mo_image.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
		contours = [cv2.approxPolyDP(cnt,3,True) for cnt in contour0[0]]

		maxArea = 0
		rect=[]

		for ctr in contours:
		    maxArea = max(maxArea,cv2.contourArea(ctr))

		areaRatio = 0.05

		for ctr in contours:    
		    if cv2.contourArea(ctr) > maxArea * areaRatio: 
		        rect.append(cv2.boundingRect(cv2.approxPolyDP(ctr,1,True)))

		symbols=[]
		for i in rect:
		    x = i[0]
		    y = i[1]
		    w = i[2]
		    h = i[3]
		    p1 = (x,y)
		    p2 = (x+w,y+h)
		    cv2.rectangle(mo_image,p1,p2,255,2)
		    image = cv2.resize(mo_image[y:y+h,x:x+w],(32,32))
		    symbols.append(image.reshape(1024,).astype("uint8"))

		#segment images and export them
		testset_data = np.array(symbols)
		#plt.show()
		# show glyphs
		img_name_base = str(time()) + '_'
		for i in range(len(symbols)):
			image = np.zeros(shape=(64,64))
			image[15:47,15:47] = symbols[i].reshape((32,32))
			segment_name = img_name_base + str(i) + '.jpg'
			cv2.imwrite(os.path.join(SEGMENTED_INSCRIPTIONS, segment_name), image)
		return jsonify({'error': 'asd'})
	def detect(self, c):
		# initialize the shape name and approximate the contour
		shape = "unidentified"
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, 0.04 * peri, True)

		# if the shape is a triangle, it will have 3 vertices
		if len(approx) == 3:
			shape = "triangle"

		# if the shape has 4 vertices, it is either a square or
		# a rectangle
		elif len(approx) == 4:
			# compute the bounding box of the contour and use the
			# bounding box to compute the aspect ratio
			(x, y, w, h) = cv2.boundingRect(approx)
			ar = w / float(h)

			# a square will have an aspect ratio that is approximately
			# equal to one, otherwise, the shape is a rectangle
			shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"

		# if the shape is a pentagon, it will have 5 vertices
		elif len(approx) == 5:
			shape = "pentagon"

		# otherwise, we assume the shape is a circle
		else:
			shape = "circle"

		# return the name of the shape
		return shape
Ejemplo n.º 9
0
def detect_possible_buttons(image, source_image):
    contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    buttons = []
    for cnt in contours:
        if 850 < cv2.contourArea(cnt) < 3000:  # remove small and large areas like noise etc
            hull = cv2.convexHull(cnt)    # find the convex hull of contour
            hull = cv2.approxPolyDP(hull, 0.1 * cv2.arcLength(hull, True), True)
            min = [10000.0, 10000.0]
            max = [0.0, 0.0]
            #print '%d,%d' % (point[0][0], point[0][1])
            if len(hull) == 4:
                margin = 2
                for point in hull:
                    x = point[0][0]
                    y = point[0][1]
                    if x < min[0]:
                        min[0] = x - margin
                    if y < min[1]:
                        min[1] = y - margin
                    if x > max[0]:
                        max[0] = x + margin
                    if y > max[1]:
                        max[1] = y + margin
                points = [[min[0], min[1]], [max[0], min[1]], [max[0], max[1]], [min[0], max[1]]]
                points = np.array(points,np.int0)
                button = {'image': source_image[min[0]:max[0], min[1]:max[1]],
                          'x': 0.5*(max[0]+min[0]), 'y': 0.5*(max[1]+min[1]),
                          'w': max[0]-min[0], 'h': max[1]-min[1]}
                buttons.append(button)
                cv2.polylines(source_image, [points], True, (255, 0, 0), 2)
                cv2.drawContours(source_image, [hull], 0, (0, 255, 0), 1)
    return buttons
Ejemplo n.º 10
0
    def build_correspondance(self, visible_markers,camera_calibration,min_marker_perimeter):
        """
        - use all visible markers
        - fit a convex quadrangle around it
        - use quadrangle verts to establish perpective transform
        - map all markers into surface space
        - build up list of found markers and their uv coords
        """

        all_verts = [m['verts'] for m in visible_markers if m['perimeter']>=min_marker_perimeter]
        if not all_verts:
            return
        all_verts = np.array(all_verts)
        all_verts.shape = (-1,1,2) # [vert,vert,vert,vert,vert...] with vert = [[r,c]]
        # all_verts_undistorted_normalized centered in img center flipped in y and range [-1,1]
        all_verts_undistorted_normalized = cv2.undistortPoints(all_verts, camera_calibration['camera_matrix'],camera_calibration['dist_coefs'])
        hull = cv2.convexHull(all_verts_undistorted_normalized,clockwise=False)

        #simplify until we have excatly 4 verts
        if hull.shape[0]>4:
            new_hull = cv2.approxPolyDP(hull,epsilon=1,closed=True)
            if new_hull.shape[0]>=4:
                hull = new_hull
        if hull.shape[0]>4:
            curvature = abs(GetAnglesPolyline(hull,closed=True))
            most_acute_4_threshold = sorted(curvature)[3]
            hull = hull[curvature<=most_acute_4_threshold]


        # all_verts_undistorted_normalized space is flipped in y.
        # we need to change the order of the hull vertecies
        hull = hull[[1,0,3,2],:,:]

        # now we need to roll the hull verts until we have the right orientation:
        # all_verts_undistorted_normalized space has its origin at the image center.
        # adding 1 to the coordinates puts the origin at the top left.
        distance_to_top_left = np.sqrt((hull[:,:,0]+1)**2+(hull[:,:,1]+1)**2)
        bot_left_idx = np.argmin(distance_to_top_left)+1
        hull = np.roll(hull,-bot_left_idx,axis=0)

        #based on these 4 verts we calculate the transformations into a 0,0 1,1 square space
        m_from_undistored_norm_space = m_verts_from_screen(hull)
        self.detected = True
        # map the markers vertices in to the surface space (one can think of these as texture coordinates u,v)
        marker_uv_coords =  cv2.perspectiveTransform(all_verts_undistorted_normalized,m_from_undistored_norm_space)
        marker_uv_coords.shape = (-1,4,1,2) #[marker,marker...] marker = [ [[r,c]],[[r,c]] ]

        # build up a dict of discovered markers. Each with a history of uv coordinates
        for m,uv in zip (visible_markers,marker_uv_coords):
            try:
                self.markers[m['id']].add_uv_coords(uv)
            except KeyError:
                self.markers[m['id']] = Support_Marker(m['id'])
                self.markers[m['id']].add_uv_coords(uv)

        #average collection of uv correspondences accros detected markers
        self.build_up_status = sum([len(m.collected_uv_coords) for m in self.markers.values()])/float(len(self.markers))

        if self.build_up_status >= self.required_build_up:
            self.finalize_correnspondance()
Ejemplo n.º 11
0
def get_squares(contours, min_length=1000):
    """
    >>> contours = detect_contours(test_image)
    >>> isinstance(get_squares(contours), list)
    True

    >>> get_squares(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Contours can't be None.

    >>> get_squares([])
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Contours can't be void.

    >>> get_squares(contours, min_length=-100)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Min_length value must be greater than 0.
    """

    # Checking arguments
    check_contours(contours)
    check_min_length(min_length)

    results = []

    for cnt in contours:
        approx = cv.approxPolyDP(cnt, 0.01*cv.arcLength(cnt, True), True)
        if len(approx) >= 4 and abs(get_contour_dimension(approx)) >= min_length:
            results = [approx] + results

    return results
Ejemplo n.º 12
0
def find_squares(img):
    img = cv2.GaussianBlur(img, (5, 5), 0)
    lists = []
    for thrs in xrange(0, 255, 26):
        if thrs == 0:
            bin = cv2.Canny(img, 0, 50, apertureSize=5)  #边缘化
            bin = cv2.dilate(bin, None) #膨胀
        else:
            retval, bin = cv2.threshold(img, thrs, 255, cv2.THRESH_BINARY)

        contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) #得到轮廓

        for cnt in contours:

            cnt_len = cv2.arcLength(cnt, True) #计算轮廓长度
            cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
            if len(cnt) == 4 and cv2.contourArea(cnt) > 500 and cv2.isContourConvex(cnt):
                cnt = cnt.reshape(-1, 2)

                max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)]) #计算矩阵夹角是否90°

                if max_cos < 0.1 and horizontal(cnt[0],cnt[1]) <5 and isDigit(cnt[0],cnt[2]) :
                    a = cnt.tolist()
                    if a not in lists:
                        lists.append(cnt.tolist())
                        rect = rectu(cnt[0],cnt[2])
                        roi = img[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]

                    if len(lists) > 0:
                        break
        if len(lists) > 0:
            break


    return roi
Ejemplo n.º 13
0
def getShape(contour):
    p = cv2.arcLength(contour, True)
    aV = cv2.approxPolyDP(contour, 0.04 * p, True)

    vertices = len(aV)
    if vertices == 3:
        return 'Triangle'
    elif vertices == 4:
        rect = cv2.minAreaRect(contour)
        contourArea = cv2.contourArea(contour)
        fittedArea = rect[1][0] * rect[1][1]
        #print "Countor Area:", contourArea , " Fiited A:", fittedArea
        (x, y, w, h) = cv2.boundingRect(aV)
        ar = w / float(h)
        if .95 * fittedArea <= contourArea and ar >= 0.95 and ar <= 1.05:
            return 'Square'
        else:
            return 'Rectangle'
    elif vertices == 5:
        return 'Pentagon'
    elif vertices == 6:
        return 'Hexagon'
    elif vertices == 7:
        return 'Heptagon'
    else:
        (xC, yC), radius  = cv2.minEnclosingCircle(contour)
        contourArea = cv2.contourArea(contour)
        fittedArea = radius*radius*3.14
        # print "Countor Area:", contourArea , " Circle A:", fittedArea
        if abs(contourArea-fittedArea) / max(contourArea, fittedArea) < 0.10:
            return 'Circle'
        else:
            return str(str(len(aV))+'-Polygon')
    return 'Unknown'
Ejemplo n.º 14
0
    def _find_contours(self, img, filtered):
        """
        Find contours in the thresholded image.

        Keep only the largest ones and from these choose one
        with best hash score.
        """
        _, cnts, _ = cv2.findContours(filtered.copy(), cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)
        cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:4]

        for contour in cnts:
            # approximate the contour
            perimeter = cv2.arcLength(contour, True)
            approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True)

            # if our approximated contour has four points, then
            # we can assume that we have found our screen
            if len(approx) >= 4:
                # take screen from origin picture
                # which belongs to contour
                x, y, w, h = cv2.boundingRect(approx)
                if (self._is_big_enough(img, w, h)):
                    extract = img[y:y + h, x:x + w]
                    if (self._has_best_score(extract)):
                        self.best_image = extract
Ejemplo n.º 15
0
    def detect_color_blob(self, img, lower, upper, color):
	og_img = img
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, lower, upper)

        mask = cv2.erode(mask, (3,3), iterations=1)

        contours, h = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        #if self.debugging:
            #cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
                                  
        sorted_contours = sorted(contours, key = lambda c: cv2.contourArea(c), reverse=True)

        if len(sorted_contours) < 1:
            return None

        c = sorted_contours[0]

        area = cv2.contourArea(c)
        if area < 1000: # minimum area threshold
            return None
        
        perim = cv2.arcLength(c, True) # perimeter
        approx = cv2.approxPolyDP(c, 0.05 * perim, True)
        
        #detecting circles
        (x,y), radius = cv2.minEnclosingCircle(c)
        
        if len(approx) == 4:
            (x, y, w, h) = cv2.boundingRect(approx)
	        ar = w / float(h)
	        self.shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
def get_shape(contours, image_to_color, show_shape=False):
    min_shape_size = 200
    shape = None
 
    for cnt in contours:
        approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True)
        # print len(approx)
        if len(approx) == 4 and cv2.contourArea(approx) > min_shape_size:
            shape = "square"
            cv2.drawContours(image_to_color, [cnt], 0, 255,-1)
        elif len(approx) > 7 and cv2.contourArea(approx) > min_shape_size:
            shape = "circle"
            cv2.drawContours(image_to_color, [cnt], 0, 255,-1)
        elif len(approx) == 3 and cv2.contourArea(approx) > min_shape_size:
            shape = "triangle"
            cv2.drawContours(image_to_color, [cnt], 0, 255,-1)  
        elif len(approx) == 5 and cv2.contourArea(approx) > min_shape_size:
            shape = "pentagon"
            cv2.drawContours(image_to_color, [cnt], 0, 255,-1)
        elif len(approx) == 6 and cv2.contourArea(approx) > min_shape_size:
            shape = "hexagon"
            cv2.drawContours(image_to_color, [cnt], 0, 255,-1)  
    if show_shape: 
        cv2.imshow('image_to_color', image_to_color)
         # When you're done looking at the image, use this to close all windows:            cv2.destroyAllWindows()
         
    return shape
Ejemplo n.º 17
0
    def findContours(self, shapesImg):
        self.img = cv2.imread(shapesImg)
        self.gray = cv2.imread(shapesImg,0)

        ret,thresh = cv2.threshold(self.gray,100,255,cv2.THRESH_BINARY )

        contours,h = cv2.findContours(thresh,1,2)

        for cnt in contours:
            approx = cv2.approxPolyDP(cnt,0.1*cv2.arcLength(cnt,True),True)
            print len(approx)
            if len(approx)==5:
                print "pentagon"
                cv2.drawContours(self.img,[cnt],0,(255,0,0),-1)
            elif len(approx)==3:
                print "triangle"
                cv2.drawContours(self.img,[cnt],0,(0,255,0),-1)
            elif len(approx)==4:
                print "square"
                cv2.drawContours(self.img,[cnt],0,(0,0,255),-1)
            elif len(approx) == 9:
                print "half-circle"
                cv2.drawContours(self.img,[cnt],0,(255,255,0),-1)
            elif len(approx) > 9:
                print "circle"
                cv2.drawContours(self.img,[cnt],0,(0,255,255),-1)
        return self.img
Ejemplo n.º 18
0
 def color_detect(self):
     while True:
         ret,frame = self.cap.read()
         imhsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
         lower_range = np.array([125, 80, 80], dtype=np.uint8)
         upper_range = np.array([255, 255, 255], dtype=np.uint8)
         mask = cv2.inRange(imhsv,lower_range,upper_range)
         ret,thresh = cv2.threshold(mask,127,155,1)
         contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
         max_area = 0
         for cnt in contours:
             approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
             area = cv2.contourArea(approx)
             if area > max_area and area < 250000:
                 max_area = area
                 max_contour = cnt
         cv2.drawContours(frame,[max_contour],0,(0,255,0),2)
         if cv2.contourArea(max_contour)>15000:
             self.pub.publish('Find an Obstale')
         cv2.imshow('mask',mask)
         cv2.imshow('frame',frame)
         if cv2.waitKey(1) & 0XFF == ord('q'):
             break
     cap.release()
     cv2.destroyAllWindows()
def getCards(im, numcards=4):
  gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  blur = cv2.GaussianBlur(gray,(1,1),1000)
  flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY) 
       
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #Haal de contouren van het plaatje op, bestaande uit een paar honderd punten per contour.

  contours = sorted(contours, key=cv2.contourArea,reverse=True)[:numcards] #Sorteer deze op grootte, en behoud slechts 1 contour ( [:numcards] ).

  for card in contours:
    peri = cv2.arcLength(card,True)
    squareContour = cv2.approxPolyDP(card,0.02*peri,True) #Benader de contour bestaande uit honderden punten met een contour die slechts 4 punten bevat, een rechthoek

    if squareContour.shape[0] != 4: #Vang een error op, en laat aan de gebruiker zien waar het fout gaat.
        print "Contour gevonden met punten ongelijk aan 4! Punten: %d" % squareContour.shape[0]
        box = np.int0(squareContour)
        cv2.drawContours(im,[box],0,(255,255,0),6)
        imx = cv2.resize(im,(1000,600))
        cv2.imshow("foute contour" ,imx)
        cv2.waitKey(0)
        continue

    approx = rectify(squareContour) #Zet de contour in een plaatje van 450x450 pixels

    h = np.array([ [0,0],[449,0],[449,449],[0,449] ],np.float32)

    transform = cv2.getPerspectiveTransform(approx,h)
    warp = cv2.warpPerspective(im,transform,(450,450))

    yield warp
Ejemplo n.º 20
0
Archivo: ocr.py Proyecto: oyajiro/l2bot
def findTarget(img):    
    template_tg = cv2.imread('template_target2.png', 0)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret,th1 = cv2.threshold(gray,253,255,cv2.THRESH_TOZERO_INV)
    ret,th3 = cv2.threshold(th1,251,255,cv2.THRESH_BINARY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 15))
    closed = cv2.morphologyEx(th3, cv2.MORPH_CLOSE, kernel)
    closed = cv2.erode(closed, None, iterations = 3)
    closed = cv2.dilate(closed, None, iterations = 2)
    (cnts, hierarchy) = cv2.findContours(closed,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    approxes = []
    hulls = []
    for cnt in cnts:
        approxes.append(cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True))
        hulls.append(cv2.convexHull(cnt))
        left = list(cnt[cnt[:,:,0].argmin()][0])        
        right = list(cnt[cnt[:,:,0].argmax()][0])
        print 'left x' + str(left[0])+ 'y '+ str(left[1])
        print 'right x' + str(right[0])+ 'y '+ str(right[1])
        center = round((right[0]+left[0])/2)
        center = int(center)
        moveMouse(center-10,left[1]+70)
        if (findFromTargeted(template_tg, left, right)):
            autoit.mouse_click('left', center-10, left[1]+70)
            return True
        pyautogui.moveTo(center,left[1]+70)
        moveMouse(center,left[1]+70)
        if (findFromTargeted(template_tg, left, right)):
            autoit.mouse_click('left', center+10, left[1]+70)
            return True
Ejemplo n.º 21
0
 def polyFn(s,img):
     contours,hierarchy = s[contourKey]
     # hulls = (cv2.convexHull(c) for c in contours)
     polys = (cv2.approxPolyDP(c,s['polyApproxK'],True) for c in
             contours)
     s[contourKey] = np.asarray(list(polys))
     return img
Ejemplo n.º 22
0
def imgproc(frame):
    # convert color to gray scale and show it
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('gray', gray)

    blur = cv2.blur(gray, (5, 5))
    edge = cv2.Canny(blur, 10, 100)
    edge = cv2.blur(edge, (2, 2))
    cv2.imshow('blured edge', edge)

    # convert image to black and white and show it
    thresh1, thresh = cv2.threshold(edge, 60, 120, cv2.THRESH_BINARY)
    cv2.imshow('thresh', thresh)

    # find contours!
    contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # draw all the contours
    cpframe = frame.copy()
    cv2.drawContours(cpframe, contours, -1, (0, 255, 0), 3)
    cv2.imshow('cpframe', cpframe)

    # ================== TODO ===================

    # Modify these code to suit your need
    contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100]
    contours = [cv2.approxPolyDP(ctr, 5, True) for ctr in contours]
    contours = [ctr for ctr in contours if len(ctr) == 4]
    contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)]

    # ============================================

    # draw on the frame
    cv2.drawContours(frame, contours, -1, (0, 255, 0), 3)
    return frame
Ejemplo n.º 23
0
def approxPoly(contours) :
    cnt_approx = np.zeros((4, 2), dtype = "float32")
    contours_len = cv2.arcLength(contours, True)
    
    
    cnt_approx = cv2.approxPolyDP(contours, 0.001*contours_len, True)
    nb_cnt = len(cnt_approx)
    angles_distances = []

    for i in range(0, nb_cnt) :
        pt1 = cnt_approx[(i - 1) % nb_cnt]
        pt2 = cnt_approx[i]
        pt3 = cnt_approx[(i+1) % nb_cnt]
        angle = vectorsAngle( pt2 - pt1, pt3 - pt2)
        length = norm(pt2 - pt1) + norm(pt3 - pt2)
        angles_distances.append([pt1, pt2, pt3, angle, length])

    angles_distances = approxPoly_removeSmallAngle(angles_distances)
    angles_distances = approxPoly_removeSmallDistance(angles_distances, contours_len)
    cnt_approx = [elt[1] for elt in angles_distances]
    angles_distances = approxPoly_fuseBigDistance(angles_distances, contours_len)

    cnt_approx = [elt[1] for elt in angles_distances]
    cnt_approx = approxPoly_lineIntersection(cnt_approx)

    return cnt_approx
def _get_magic_card_crop(original_image):
    scale_mult = 0.15
    mat = cv2.resize(original_image, (0, 0), fx=scale_mult, fy=scale_mult)
    contour = _get_magic_card_contour(mat)

    # Obtain shape approximation
    card = contour[0]
    peri = cv2.arcLength(card, True)
    approx = cv2.approxPolyDP(card, 0.02 * peri, True)

    x, y, w, h = cv2.boundingRect(card)
    w /= scale_mult
    h /= scale_mult

    w = int(w)
    h = int(h)

    # Test to see if our approximation is a quadrilateral
    if len(approx) != 4:
        rect = rectify(cv2.boxPoints(cv2.minAreaRect(card)))
    else:
        # Great! We can safely apply our perspective correction technique
        rect = rectify(approx)

    for i in range(4):
        # Compute all 4 corners on original image (re-apply scaling)
        rect[i][0] /= scale_mult
        rect[i][1] /= scale_mult
        
    cv2.imwrite(os.path.join(LATEST_DIR, "steps_contours_bon.jpg"), original_image)

    # Output perspective correction to an image of about the same size
    output = np.array([[0, 0], [w-1, 0], [w-1, h-1], [0, h-1]], np.float32)
    transform = cv2.getPerspectiveTransform(rect, output)
    return cv2.warpPerspective(original_image, transform, (w, h))
Ejemplo n.º 25
0
    def find_color(self, im, label_color, mask):
        contours = cv2.findContours(mask, cv2.cv.CV_RETR_TREE, cv2.cv.CV_CHAIN_APPROX_SIMPLE)[0]
        #cv2.drawContours(im, contours, -1, (255, 255, 255), 2)
        approx_contours = []
        for c in contours:
	    area = cv2.contourArea(c)
            if area < 100: continue
            perim = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, .05*perim, True)
            #if len(approx) == 4 and len(cv2.convexityDefects(c, cv2.convexHull(c))) <= 1:
            if len(approx) == 4:
                approx_contours.append(approx)
                moments = cv2.moments(c)
                center = (int(moments['m10']/moments['m00']), int(moments['m01']/moments['m00']))
                cv2.circle(im, center, 3, (255, 100, 100), 4)
                print "Moment:  ({}, {})".format(center[0], center[1])

                msg_color = ColorRGBA()
                msg_color.r, msg_color.g, msg_color.b = label_color
                self.msg.colors.append(msg_color)
                print "Label color:  {}".format(label_color)
                msg_size = Float64()
                #msg_size.data = max(math.sqrt((approx[1][0][0]-approx[0][0][0])**2+(approx[1][0][1]-approx[0][0][1])**2), math.sqrt((approx[2][0][0]-approx[1][0][0])**2+(approx[2][0][1]-approx[2][0][0])**2))
                msg_size.data = float((max(approx, key=lambda x: x[0][0])[0][0] - min(approx, key=lambda x: x[0][0])[0][0])) / len(im[0])
                print "Width:  {}".format(msg_size.data)
                self.msg.sizes.append(msg_size)
                msg_loc = Point()
                msg_loc.x, msg_loc.y = float(center[0]) / len(im[0]), float(center[1]) / len(im)
                self.msg.locations.append(msg_loc)
Ejemplo n.º 26
0
def approximate_contour(contour, epsilon, closed=True):
    """
    returns an approximated contour based on the contour passed to it, uses the approxPolyDP function from opencv2.
    """
    epsilon2 = epsilon*cv2.arcLength(contour, closed)
    approx = cv2.approxPolyDP(contour, epsilon2, closed)
    return approx
Ejemplo n.º 27
0
    def find_board(self, image):
        """
        Finds a board by calling openCV function to find contures in image.
        Than it sorts those contures and stores the biggest one.
        In case there is more than one we go over all found contures and
        keep only one with 4 points

        Args:
            image(numpy.ndarray): Image to find contures from

        Returns:
            Found conture in given image
        """
        im = image.copy()
        im = cv.dilate(im, ((5, 5)), iterations=8)
        (cnts, _) = cv.findContours(im,
                                    cv.RETR_TREE,
                                    cv.CHAIN_APPROX_SIMPLE)
        cnts = sorted(cnts, key=cv.contourArea, reverse=True)[:10]
        our_cnt = None
        for c in cnts:
            peri = cv.arcLength(c, True)
            approx = cv.approxPolyDP(c, 0.1 * peri, True)
            if len(approx) == 4:
                # The board needs to be at least 1/3 of the image size
                min_size = np.array([self.height * 1/3.0, self.height * 1/3.9])

                a = np.abs(approx[0] - approx[2])[0] > min_size
                b = np.abs(approx[1] - approx[3])[0] > min_size
                true = [True, True]
                if np.array_equal(a, true) or np.array_equal(b, true):
                    our_cnt = approx
                break

        return our_cnt
Ejemplo n.º 28
0
def getCards(im, numcards=4):
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (1, 1), 1000)
    flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)

    contours, hierarchy = cv2.findContours(
        thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:numcards]

    for card in contours:
        peri = cv2.arcLength(card, True)
        approx = rectify(cv2.approxPolyDP(card, 0.02 * peri, True))

        # box = np.int0(approx)
        # cv2.drawContours(im,[box],0,(255,255,0),6)
        # imx = cv2.resize(im,(1000,600))
        # cv2.imshow('a',imx)

        h = np.array([[0, 0], [449, 0], [449, 449], [0, 449]], np.float32)

        transform = cv2.getPerspectiveTransform(approx, h)
        warp = cv2.warpPerspective(im, transform, (450, 450))

        yield warp
Ejemplo n.º 29
0
    def detect_screen(self, blur_pars=(21, 17, 17), draw_contours=True):
        gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.bilateralFilter(gray, *blur_pars)
        edged = cv2.Canny(gray, 30, 200)

        (_, contours, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
        screen_contour = []

        for c in contours:
            # approximate the contour
            peri = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, 0.02 * peri, True)

            if len(approx) == 4:
                screen_contour = approx
                break

        if len(screen_contour):
            global global_rect

            coords = np.array([[i[0][0], i[0][1]] for i in screen_contour])
            if draw_contours:
                cv2.drawContours(self.frame, [screen_contour], -1, (0, 0, 255), 3)

            global_rect = self.order_points(coords)
Ejemplo n.º 30
0
def getCornerPoints(cont):
    peri = cv2.arcLength(cont, True)
    approx = cv2.approxPolyDP(cont, 0.02 * peri, True)
    return approx
Ejemplo n.º 31
0
def signal():
    camera = PiCamera()
    camera.resolution = (320, 240)
    camera.framerate = 40
    rawCapture = PiRGBArray(camera, size=(320, 240))
    camera.shutter_speed = 50
    camera.iso = 1600
    camera.rotation = 180

    time.sleep(1)

    ret = True
    counter = 0
    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
        # Capture frame-by-frame

        img = frame.array[:120][:][::]
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        cont = img.copy()
        canny = cv2.Canny(gray, 30, 250)

        contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        new_contour = []
        counter += 1
        hm = 50
        get_number = 0

        for i in range(0, len(contours)):
            epsilon = 0.1 * cv2.arcLength(contours[i], True)
            approx = cv2.approxPolyDP(contours[i], epsilon, closed=True)

            if (abs(cv2.contourArea(contours[i])) > 0):
                # if(abs(cv2.contourArea(contours[i])) > 0 and hierarchy[0][i][0] > -1):

                # xm, ym, wm, hm = cv2.boundingRect(contours[hierarchy[0][i][0]])
                if ((hm) > 40):
                    x, y, w, h = cv2.boundingRect(contours[i])
                    rel = h / w
                    addh = int(h / 8)
                    addw = int(w / 8)
                    if (rel > 1.5 and h > 20 and w > 10 and w < 40 and h < 55 and y > 5 and (y + h) < 235 and x < 220):
                        # if(rel > 1.3):
                        # cv2.polylines(cont, [contours[i]], False, (0,255,0), 2)
                        number = \
                            cv2.threshold(gray[y - addh:y + h + addh, x - addw:x + w + addw], 30, 255,
                                          cv2.THRESH_BINARY)[1]
                        try:
                            if number[0][0].all() == 0:
                                number = cv2.bitwise_not(number)

                            nonzeroes = np.count_nonzero(number) / np.prod(number.shape)
                            if number[0][0].all() != 0 and number[:][0].all() and nonzeroes > 0.65 and nonzeroes < 0.88:
                                get_number = tesseract(number)

                                if (show_pictures == "yes"):
                                    cv2.rectangle(cont, (x - addw, y - addh), (x + w + addw, y + h + addh), (0, 0, 255),
                                                  1)

                                    cv2.imshow('number', cv2.resize(number, None, fx=5, fy=5))
                                    cv2.putText(cont, "h={}  w={} c={}".format(h, w, cv2.isContourConvex(contours[i])),
                                                (x + w + addw + 5, y), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 255), 1)
                                    cv2.putText(cont,
                                                "r={:.2f} a={} n={}".format(rel, abs(cv2.contourArea(contours[i])), i),
                                                (x + w + addw + 5, y + 15), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 255), 1)
                                    cv2.putText(cont, "x={} y={}".format(x, y),
                                                (x + w + addw + 5, y + 30), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 255), 1)
                                    cv2.putText(cont, "number={} l={}".format(get_number, len(approx)),
                                                (x + w + addw + 5, y + 45), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 0), 1)
                                    print(str(get_number) + " " + str(
                                        np.count_nonzero(number) / np.prod(number.shape)) + " " + str(
                                        np.count_nonzero(number)) + " " + str(np.prod(number.shape)))

                        except:
                            count = 1
        if (show_pictures == "yes"):
            cv2.imshow('canny', canny)
            cv2.imshow('cont', cont)

        #    cv2.destroyAllWindows()

        try:
            if int(get_number) > 0:
                sound.play(str(int(get_number)))
        except:
            print("not a number")

        rawCapture.truncate(0)
        #waitkey is needed, else picture wont display with imshow
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Ejemplo n.º 32
0
def prueba(imagen):
    DIGITS_LOOKUP = {
        (1, 1, 1, 0, 1, 1, 1): 0,
        (0, 0, 1, 0, 0, 1, 0): 1,
        (1, 0, 1, 1, 1, 1, 0): 2,
        (1, 0, 1, 1, 0, 1, 1): 3,
        (0, 1, 1, 1, 0, 1, 0): 4,
        (1, 1, 0, 1, 0, 1, 1): 5,
        (1, 1, 0, 1, 1, 1, 1): 6,
        (1, 0, 1, 0, 0, 1, 0): 7,
        (1, 1, 1, 1, 1, 1, 1): 8,
        (1, 1, 1, 1, 0, 1, 1): 9
    }

    a = False
    #imagen = _abrir_archivo()
    height, width, channels = imagen.shape
    print("valores originales A H C: ")
    print(height, width, channels)
    fc = 1
    if width > 1000:
        fc = .1
    elif width > 500:
        fc = .5
    imagen2 = imutils.resize(imagen, height=500)
    #imagen2 = cv2.resize(imagen,None,fx=fc, fy=fc, interpolation = cv2.INTER_CUBIC)

    gray = cv2.cvtColor(imagen2, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(blurred, 50, 200, 255)

    height, width, channels = imagen2.shape
    print("valores de alto, ancho, canales: ")
    print(height, width, channels)

    # Encuentra los contornos y los ennumera
    # en orden decendiente de tamaño
    cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
    displayCnt = None

    # para cada contorno en el la lista de contornos
    for c in cnts:
        # verifica
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        print("contornos de corte:")
        print(approx)
        # si cuatro lados lo desplegara
        if len(approx) == 4:
            print("<------Existe una funcional 4 lados")
            displayCnt = approx
            break

    #marcas de corte
    warped = four_point_transform(gray, displayCnt.reshape(4, 2))
    output = four_point_transform(imagen2, displayCnt.reshape(4, 2))

    height, width, channels = output.shape
    print("valores recortado de alto, ancho, canales: ")
    print(height, width, channels)
    if height < 50:
        output = imutils.resize(output, height=75)
        warped = imutils.resize(warped, height=75)

    if height > 80:
        output = imutils.resize(output, height=75)
        warped = imutils.resize(warped, height=75)
    #ajuste de brillo
    alpha = float(2.5)
    #cv2.equalizeHist(
    adjust = cv2.multiply(warped, np.array([alpha]))

    thresh2 = cv2.threshold(adjust, 0, 255,
                            cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
    #thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    thresh3 = cv2.morphologyEx(thresh2, cv2.MORPH_OPEN, kernel)
    #/////////

    thresh = cv2.threshold(warped, 0, 255,
                           cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

    #///////////////////aproxima2
    estirado = cv2.inRange(adjust, 10, 140)

    cnts = cv2.findContours(thresh2.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    print("encuentra contornos: ")
    print(len(cnts))
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    print("encuentra contornos imutils.is_cv2: ")
    print(len(cnts))
    digitCnts = []

    # loop over the digit area candidates
    try:
        for c in cnts:
            # compute the bounding box of the contour
            (x, y, w, h) = cv2.boundingRect(c)
            if w >= 15 and (h >= 30 and h <= 40):
                digitCnts.append(c)
        digitCnts = contours.sort_contours(digitCnts,
                                           method="left-to-right")[0]
        digits = []

        for c in digitCnts:

            (x, y, w, h) = cv2.boundingRect(c)
            roi = thresh[y:y + h, x:x + w]

            # compute the width and height of each of the 7 segments
            # we are going to examine
            (roiH, roiW) = roi.shape
            (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))
            dHC = int(roiH * 0.05)

            # define the set of 7 segments
            segments = [
                ((0, 0), (w, dH)),  # top
                ((0, 0), (dW, h // 2)),  # top-left
                ((w - dW, 0), (w, h // 2)),  # top-right
                ((0, (h // 2) - dHC), (w, (h // 2) + dHC)),  # center
                ((0, h // 2), (dW, h)),  # bottom-left
                ((w - dW, h // 2), (w, h)),  # bottom-right
                ((0, h - dH), (w, h))  # bottom
            ]
            on = [0] * len(segments)

            # loop over the segments
            for (i, ((xA, yA), (xB, yB))) in enumerate(segments):
                # extract the segment ROI, count the total number of
                # thresholded pixels in the segment, and then compute
                # the area of the segment
                segROI = roi[yA:yB, xA:xB]
                total = cv2.countNonZero(segROI)
                area = (xB - xA) * (yB - yA)

                # if the total number of non-zero pixels is greater than
                # 50% of the area, mark the segment as "on"
                if area == 0:
                    area = 0.001

                if total / float(area) > 0.5:
                    on[i] = 1

            # lookup the digit and draw it on the image
            digit = DIGITS_LOOKUP[tuple(on)]
            digits.append(digit)
            cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 1)
            cv2.putText(output, str(digit), (x - 10, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 255, 0), 2)

        # display the digits

        print(u"{}{}.{} \x57 \x50 \x77".format(*digits))

        print("respuesta: ")
        print(len(digitCnts))
        print(len(digitCnts[0]))

    except Exception as e:
        print(e)

    cv2.imshow("original", imagen2)
    cv2.imshow("recortado", output)
    cv2.imshow("ajustado", adjust)
    cv2.imshow("estirado", estirado)
    cv2.imshow("ventana4", thresh3)
    cv2.imshow("ventana2", thresh)
    cv2.imshow("ventana3", thresh2)

    cv2.waitKey(0)
Ejemplo n.º 33
0
    def detectShape(cnts):
        shape = 'unknown'
        peri = cv2.arcLength(cnts, True)

        vertices = cv2.approxPolyDP(cnts, 0.03 * peri, True)
        img = cv2.drawContours(image, vertices, -1, (0, 0, 255), 3)

        #check if circle
        circles = cv2.HoughCircles(thresh,
                                   cv2.HOUGH_GRADIENT,
                                   1,
                                   20,
                                   param1=50,
                                   param2=30,
                                   minRadius=0,
                                   maxRadius=0)
        if circles is not None:
            shape = "circle"
        else:
            #triangle
            if len(vertices) == 3:
                shape = 'triangle'

            #square/ reactangle/trapezoid?
            elif len(vertices) == 4:
                if (log_hu[0][0] > 0.4 and log_hu[0][0] < 1.0) and (
                        log_hu[1][0] > 2.0
                        and log_hu[1][0] < 3.0) and (log_hu[2][0] > 3.0
                                                     and log_hu[2][0] < 3.6):
                    shape = "quater circle"
                elif (log_hu[0][0] > 0.4 and log_hu[0][0] < 1.0) and (
                        log_hu[1][0] > 1.0 and log_hu[1][0] < 2.5) and (
                            log_hu[2][0] > 5.0 and log_hu[2][0] < 7.0) and (
                                log_hu[3][0] > 6.0 and log_hu[3][0] < 8.0):
                    shape = "rectangle"
                else:
                    x, y, width, height = cv2.boundingRect(vertices)
                    aspectRatio = float(width) / height
                    print "aspect ratio=" + str(aspectRatio)
                    if aspectRatio >= 0.4 and aspectRatio <= 0.6:
                        shape = "square"
                    else:
                        shape = "trapezoid"

            elif len(vertices) == 5:
                if (log_hu[0][0] > 0.4 and log_hu[0][0] < 1.0) and (
                        log_hu[1][0] > 1.0 and log_hu[1][0] < 2.5) and (
                            log_hu[2][0] > 5.0 and log_hu[2][0] < 7.0) and (
                                log_hu[3][0] > 6.0 and log_hu[3][0] < 8.0):
                    shape = "rectangle"
                elif ratio > 0.4 and ratio < 0.62:

                    if (log_hu[0][0] > 0.4 and log_hu[0][0] < 1.0) and (
                            log_hu[1][0] > 2.0 and log_hu[1][0] < 3.0) and (
                                log_hu[2][0] > 3.0 and log_hu[2][0] < 3.6):
                        shape = "quater circle"
                    else:
                        shape = "semicircle"
                else:
                    shape = "pentagon"

            elif len(vertices) == 6:
                if ratio > 0.4 and ratio < 0.62:
                    if (log_hu[0][0] > 0.4 and log_hu[0][0] < 1.0) and (
                            log_hu[1][0] > 2.0 and log_hu[1][0] < 3.0) and (
                                log_hu[2][0] > 3.0 and log_hu[2][0] < 3.6):
                        shape = "quater circle"
                    else:
                        shape = "semicircle"
                else:
                    shape = "hexagon"

            elif len(vertices) == 7:
                shape = "heptagon"

            elif len(vertices) == 8:
                if ratio > 0. and ratio < 1.1:
                    shape = "circle"
                else:
                    shape = "octagon"

            elif len(vertices) == 13 or (len(vertices) == 12
                                         or len(vertices) == 11):
                shape = "cross"
            elif (log_hu[0][0] > 0.4 and log_hu[0][0] < 1.0) and (
                    log_hu[1][0] > 2.0
                    and log_hu[1][0] < 3.0) and (log_hu[2][0] > 3.0
                                                 and log_hu[2][0] < 3.6):
                shape = "quater circle"
            elif ratio > 0.4 and ratio < 0.6 and log_hu[1][0] < 2.1:
                shape = "semicircle"
            elif ratio > 0.9 and ratio < 1.1:
                shape = "circle"

            else:
                if (log_hu[0][0] > 0.0 and log_hu[0][0] < 1.0) and (
                        log_hu[1][0] > 4.0 and log_hu[1][0] < 5.0) and (
                            log_hu[2][0] > 5.0 and log_hu[2][0] < 6.0) and (
                                log_hu[3][0] > 6.0 and log_hu[3][0] < 7.0):
                    shape = "cross"
                elif (log_hu[0][0] > 0.6 and log_hu[0][0] < 1.0) and (
                        log_hu[1][0] > 4.2 and log_hu[1][0] < 6.0) and (
                            log_hu[2][0] > 5.0 and log_hu[2][0] < 6.0) and (
                                log_hu[3][0] > 6.0 and log_hu[3][0] < 8.0):
                    shape = "star"
                else:
                    shape = "UNKNOWN"

        return shape
                p2 = [sqeeze[:, 0][xMaxIndex], sqeeze[:, 1][xMaxIndex]]

                yMinIndex = np.argmin(sqeeze[:, 1])
                p3 = [sqeeze[:, 0][yMinIndex], sqeeze[:, 1][yMinIndex]]

                yMaxIndex = np.argmax(sqeeze[:, 1])
                p4 = [sqeeze[:, 0][yMaxIndex], sqeeze[:, 1][yMaxIndex]]

                if p1[0] == 0 or p2[0] == width - 1 or p3[1] == 0 or p4[1] == height -1:
                    continue

                area = cv2.contourArea(cnt)
                perimeter = cv2.arcLength(cnt,True)
                # k = cv2.isContourConvex(cnt)
                epsilon = perimeter * 0.1
                approx = cv2.approxPolyDP(cnt,epsilon,True)
                if len(approx) != 4:
                    continue
                # print("approx ,", approx)
                cv2.drawContours(green,[approx],-1,[0,255,0],2)
                cv2.imwrite('green_' + str(m) + '_' + str(j) + '.jpg', green)
                if area >= maxArea/4 and area <= maxArea or True:

                    # print(sqeeze)
                    # epsilon = 0.01 * perimeter
                    # approx = cv2.approxPolyDP(cnt,epsilon,True)
                    # cv2.drawContours(img_org,[approx],-1,[0,255,0],2)
                    # j = 0
                    # p1=[-1, -1]
                    # p2=[-1, -1]
                    # p3=[-1, -1]
Ejemplo n.º 35
0
    def process(self, inframe, outframe):
        # Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR by default. If
        # you need a grayscale image instead, just use getCvGRaAY() instead of getCvBGR(). Also supported are getCvRGB()
        # and getCvRGBA():
        inimg = inframe.getCvBGR()

        # Start measuring image processing time (NOTE: does not account for input conversion time):
        self.timer.start()

        ## Split into red, green, and blue, subtract red from the green
        b, g, r = cv2.split(inimg)
        binImage = g - r
        #Otsu's is a dynamic threshold that does a good job of isolating. Not as fast as fixed
        ret3, binImage = cv2.threshold(binImage, 0, 255, cv2.THRESH_OTSU)

        # Erode image to remove noise if necessary.
        binImage = cv2.erode(binImage, None, iterations=errode)
        #Dilate image to fill in gaps
        binImage = cv2.dilate(binImage, None, iterations=dilate)

        ##Finds contours (like finding edges/sides), 'contours' is what we are after
        im2, contours, hierarchy = cv2.findContours(binImage, cv2.RETR_CCOMP,
                                                    cv2.CHAIN_APPROX_TC89_KCOS)

        ##arrays to will hold the good/bad polygons
        squares = []
        badPolys = []

        ## Parse through contours to find targets
        for c in contours:
            if (contours != None) and (len(contours) > 0):
                cnt_area = cv2.contourArea(c)
                hull = cv2.convexHull(c, 1)
                p = cv2.approxPolyDP(hull, approx, 1)
                x, y, w, h = cv2.boundingRect(c)
                aspect_ratio = float(w) / h
                if (cv2.isContourConvex(p) != False) and (len(p) == 4) and (
                        cv2.contourArea(p) >=
                        area):  #p=3 triangle,4 rect,>=5 circle
                    filled = cnt_area / (w * h)
                    if filled >= solidity:
                        if aspect_ratio >= ratio:
                            squares.append(p)

                else:
                    badPolys.append(p)

        if len(squares) > 0:
            i = 1
            cv2.putText(inimg, "Tracking", (3, 20), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (255, 255, 255), 1, cv2.LINE_AA)
            pixels = {
                "Trk": len(squares)
            }  #Start of pixels array, values added in the loop for each contour
            for s in squares:
                br = cv2.boundingRect(s)
                #Target "x" and "y" center
                x = br[0] + (br[2] / 2)
                y = br[1] + (br[3] / 2)
                #Draw a tracking rectangle on the image
                cv2.rectangle(inimg, (br[0], br[1]),
                              ((br[0] + br[2]), (br[1] + br[3])), (0, 0, 255),
                              2, cv2.LINE_AA)
                #Convert from coordinates from pixels to unit square, only valid for 320x240 resolution
                nx = (1 / 160) * (x - 159.5)
                ny = (1 / 120) * (119.5 - y)
                #convert back to x and y with compensation
                x = (vpw / 2) * nx
                y = (vph / 2) * ny
                #calculate angle
                x_angle = math.degrees(math.atan2(x, 1))
                y_angle = math.degrees(math.atan2(y, 1))
                #Add info for this paticular target to the dictionary 'pixels'
                pixels['Tx_' + str(i)] = x_angle
                pixels['Ty_' + str(i)] = y_angle
                i = i + 1

        if not squares:
            pixels = {"Trk": 0, "Tx_1": 0, "Ty_1": 0}
            cv2.putText(inimg, "Not Tracking", (3, 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1,
                        cv2.LINE_AA)

        outimg = inimg

        # Write frames/s info from our timer into the edge map (NOTE: does not account for output conversion time):
        fps = self.timer.stop()
        height, width, channels = outimg.shape  # if outimg is grayscale, change to: height, width = outimg.shape
        cv2.putText(outimg, fps, (3, height - 6), cv2.FONT_HERSHEY_SIMPLEX,
                    0.5, (255, 255, 255), 1, cv2.LINE_AA)

        # Convert our BGR output image to video output format and send to host over USB. If your output image is not
        # BGR, you can use sendCvGRAY(), sendCvRGB(), or sendCvRGBA() as appropriate:

        outframe.sendCvBGR(outimg, 50)
        json_pixels = json.dumps(pixels)
        jevois.sendSerial(json_pixels)
                frame_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

                #Finding the edges using canny edge detection.
                canny = cv2.Canny(frame_gray, 170, 255)

                contours, _ = cv2.findContours(canny.copy(), cv2.RETR_LIST,
                                               cv2.CHAIN_APPROX_NONE)
                contours = sorted(contours, key=cv2.contourArea,
                                  reverse=True)[:35]

                #iterating through those contours to find the number plate in the frame.
                for cnt in contours:

                    perimeter = cv2.arcLength(cnt, True)
                    approx = cv2.approxPolyDP(cnt, 0.01 * perimeter, True)
                    if len(approx) == 4:
                        cnt_with_plate = approx
                        #geting the coordinates of the number plate in the frame
                        x, y, w, h = cv2.boundingRect(cnt)
                        break

                #applying filters to the number plate to remove any useless information.
                num_plate = cv2.bilateralFilter(frame_gray[y:y + h, x:x + w],
                                                5, 17, 17)
                thresh, num_plate = cv2.threshold(num_plate, 95, 255,
                                                  cv2.THRESH_BINARY)

                cv2.imshow('res', num_plate)
                #extracting the number from the number plate in the form of text.
                num_plate_text = pytesseract.image_to_string(num_plate)
Ejemplo n.º 37
0
    cv2.waitKey(0)

    contours = cv2.findContours(result.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if imutils.is_cv2() else contours[1]

    # loop over the contours
    biggest_size = 0
    biggest = None

    for c in contours:
        cv2.drawContours(image, [c], -1, (0, 0, 255), 4)

        # Approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.05 * peri, True)
        cv2.drawContours(image, [approx], -1, (0, 255, 0), 4)

        # Draw box around the contour
        rect = cv2.minAreaRect(c)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(image, [box], -1, (255, 0, 0), 2)

        # Calculate size
        new_size = cv2.contourArea(box)
        if new_size > biggest_size:
            biggest_size = new_size
            biggest = box

    # Draw the biggest box
Ejemplo n.º 38
0

def min_rect(contour):
    rect = cv2.minAreaRect(contour)
    rect = ((rect[0][0], rect[0][1]), (rect[1][0], rect[1][1]), rect[2])
    (width, height) = (rect[1][0], rect[1][1])
    print str(width) + " " + str(height)
    box = cv2.cv.BoxPoints(rect)
    box = np.int0(box)
    return box


for contour in good_contours:
    box = contour
    print box
    # box = cv2.approxPolyDP(contour, 10.0, True)
    # box = min_rect(contour)
    perim = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.02 * perim, True)
    box = approx
    boxes = filter_contour(box)
    cv2.drawContours(im, boxes, -1, (250, 0, 0), 2)

print "num contours: ", len(good_contours)

cv2.imwrite("outfile.jpg", im)

cv2.imshow('your_image.jpg', im)
cv2.waitKey()
cv2.destroyAllWindows()
Ejemplo n.º 39
0
time.sleep(3)

while(1):
      ret, img = cap.read()
      height, width, _ = img.shape
      #img = cv2.imread('sudoku.jpg')
      gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
      hierarchy,contours,_ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      biggest = None
      max_area = 0
      for i in contours:
          area = cv2.contourArea(i)
          if area > 100:
                  peri = cv2.arcLength(i,True)
                  approx = cv2.approxPolyDP(i,0.02*peri,True)
                  if area > max_area and len(approx)==4:
                          biggest = approx
                          max_area = area
      cv2.drawContours(img, biggest, -1, (0,0,255), 40)
      font = cv2.FONT_HERSHEY_SIMPLEX
      cv2.putText(img,'Press Esc If the 4 dots are at the corners of puzzle',(height/2-100,width/2-200), font, 0.7,(0,0,255),2,cv2.LINE_AA)
      cv2.imshow('img',img)
      k = cv2.waitKey(10) & 0xFF
      if k == 27:
            break


biggest=rectify(biggest)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
h = np.array([ [0,0],[449,0],[449,449],[0,449] ],np.float32)
Ejemplo n.º 40
0
import numpy as np
import cv2

img = cv2.imread('jjtp.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

thresh = cv2.Canny(img, 140, 200)

contours, h = cv2.findContours(thresh, 1, 2)
i = 0
for cnt in contours:
    pre = cv2.arcLength(cnt, True)
    if pre < 500:
        continue
    approx = cv2.approxPolyDP(cnt, 0.01 * pre, True)
    if len(approx) == 4:
        i += 1
        cv2.drawContours(img, [cnt], 0, (0, 255, 100), -1)
print(i)
cv2.imwrite('res.png', img)
Ejemplo n.º 41
0
 gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
 cv.imshow("grayscale", gray)
 # this is to detect all the edges in the frame (https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123)
 edge = cv.Canny(gray, 100, 200)
 # this is to give all the boundaries of closed objects(contours) in the image.It uses binary image to process it.Since the image form edge detection algorithm is an binary image , there is no need for converting it.
 contour, heire = cv.findContours(
     edge, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
 for cnt in contour:
     # calculates the area of contours
     area = cv.contourArea(cnt)
     # this is avoid detecting very small objects in the frame
     if area > 200:
         # this outlines the contours in the frame with green color
         cv.drawContours(frame, [cnt], 0, (0, 255, 0), 2)
         # this is to approximate a polygon with another polygon with less vertices
         approx = cv.approxPolyDP(cnt, 0.02*cv.arcLength(cnt, True), True,)
         x = approx.ravel()[0]
         y = approx.ravel()[1]
         if len(approx) == 3:
             cv.putText(frame, "Triangle", (x, y), font, 1, (255, 0, 0))
         elif len(approx) == 4:
             cv.putText(frame, "rectangle", (x, y), font, 1, (255, 0, 0))
         elif len(approx) == 5:
             cv.putText(frame, "pentagon", (x, y), font, 1, (0, 255, 0))
         elif len(approx) == 6:
             cv.putText(frame, "hexagon", (x, y), font, 1, (0, 255, 0))
         else:
             cv.putText(frame, "circle", (x, y), font, 1, (0, 255, 0))
 cv.imshow("edge", edge)
 cv.imshow("Final", frame)
 key = cv.waitKey(1)
Ejemplo n.º 42
0
    ratio = image.shape[0] / 720
    image = imutils.resize(image, height=720)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    final = imutils.resize(image.copy(), height=720)

    gray = cv2.GaussianBlur(gray, (11, 11), 0)
    #   for x in range(180, 254):
    thresh = cv2.threshold(gray, 190, 255, cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_LIST,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:7]
    if (len(cnts) > 0):
        for cardContour in cnts:
            peri = cv2.arcLength(cardContour, True)
            approx = cv2.approxPolyDP(cardContour, 0.02 * peri, True)
            if len(approx) == 4 and cv2.contourArea(cardContour) >= 90000:
                cv2.drawContours(image, [cardContour], -1, (0, 255, 0), 2)
                flat = getPerspective.four_point_transform(
                    original.copy(),
                    approx.reshape(4, 2) * ratio)
                if (flat.shape[0] < flat.shape[1]):
                    flat = imutils.rotate_bound(flat, 90)
                gray = cv2.cvtColor(flat, cv2.COLOR_BGR2GRAY)
                flatThresh = cv2.threshold(gray, 150, 255,
                                           cv2.THRESH_BINARY_INV)[1]

                cropped = flatThresh[:int(flatThresh.shape[0] /
                                          2.5), :int(flatThresh.shape[1] /
                                                     3.7)]
                #cv2.imshow("flat", imutils.resize(flat, height = 720))
Ejemplo n.º 43
0
def find_ellipse(img, mx=400, app=5, blr=10):

    gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)

    #331/360
    #128/260
    #C_MIN = np.array([0,0,0],np.uint8)
    #C_MAX = np.array([0,0,0],np.uint8)

    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(blurred, 0, 50, apertureSize=3)

    if False:
        # interesting approach but difficult to make circle large enough
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        hsv = cv2.blur(hsv, (15, 15))
        C = 128 / 2
        spikes = cv2.inRange(hsv.copy(), (C - 10, 5, 20), (C + 10, 255, 185))
        C = 331 / 2
        spikes2 = cv2.inRange(hsv.copy(), (C - 10, 5, 20), (C + 10, 255, 185))

        result = cv2.bitwise_or(spikes, spikes2)

    threshed = edged
    threshed = cv2.blur(edged, (5, 5))
    threshed = cv2.blur(threshed, (5, 5))
    threshed = cv2.blur(threshed, (5, 5))
    threshed = cv2.blur(threshed, (5, 5))

    #cv2.imshow('file1', threshed)

    (contours, c2) = cv2.findContours(threshed.copy(), cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:10]

    ccnts = []
    for cont in cnts:
        arr = cv2.contourArea(cont)
        #print('cont', cv2.contourArea(cont))
        if (len(cont) < 10):
            continue
        (x, y), (MA, ma), angle = cv2.fitEllipse(cont)

        if (arr < 2500):
            continue

        ell_arr = 3.14 * MA * ma / 4
        print('  ' + str(arr / ell_arr) + ' ; ' + str(cv2.contourArea(cont)))
        if arr / ell_arr < 0.9 or arr / ell_arr > 1.10:
            continue

        epsilon = 0.001 * cv2.arcLength(cont, True)
        ccnts.append(cv2.approxPolyDP(cont, 0.5, True))

    if len(ccnts) < 1:
        return (0, 0), (0, 0), None
    cont = sorted(ccnts, key=cv2.contourArea, reverse=True)[0]
    #cv2.drawContours(img, [cont], -1, (0,255,0), 3)

    return cv2.fitEllipse(cont)
Ejemplo n.º 44
0
def make_segments(count, thresh, blur, image, path, newpath,
                  actual_contours_path, segment_path):
    # print image.shape
    # fo = open("processing.txt", "a")
    # print path
    coordinates_of_segments = {}
    temp_coordinates = []
    ret, th = cv2.threshold(blur, thresh, 255, cv2.THRESH_BINARY)

    # individual = 'croppedSegments/'
    xpath = newpath
    imsave(newpath + '/thresholded_image.jpg', th)
    filename = list(path.split('/'))
    filename = filename[-1]
    filename = list(filename.split('\\'))
    filename = filename[-1]
    # print filename
    imsave(thresholded_path + filename, th)

    cnts, hierarchy = cv2.findContours(th, 1, 2)

    thresholded_image = cv2.cv.LoadImage(newpath + '/thresholded_image.jpg')
    pathses = newpath + "/size.txt"
    # delete_file(newpath + '/thresholded_image.jpg')
    actual_image = cv2.imread(path)
    file_name = []
    file_name = path.split("\\")
    file = file_name[-1]
    coordinates_of_segments[file] = []
    # print file,thresh
    loaded_image = cv2.cv.LoadImage(path)
    rect_image = actual_image
    contour_list = []
    mask = np.ones(image.shape[:2], dtype="uint8") * 255

    # loop over the contours
    number = 0
    red_number = 0
    green_number = 0
    global mc_and_dc_list
    segment_list = []
    original_segment_list = []
    coordinates_list = []
    xmin = 10000
    ymin = 10000
    xmax = 0
    ymax = 0
    w1 = 0
    h1 = 0
    counters = 0
    for c in cnts:

        approx = cv2.approxPolyDP(c, 0.009 * cv2.arcLength(c, True), True)
        area = cv2.contourArea(c)

        if ((len(approx) > 8) & (area < 4000) & (area > 100)):

            number += 1
            global w, h
            center, angle, w, h, x1, y1, x2, y2 = get_coordinates(
                actual_image, c, coordinates_list)
            if (x1 - w < xmin):
                xmin = x1 - w
            if (x2 + w > xmax):
                xmax = x2 + w
            if (y1 - h < ymin):
                ymin = y1 - h
            if (y2 + h > ymax):
                ymax = y2 + h
            w1 += w
            h1 += h
            counters = counters + 1
            #            print(x1-w,y1-h,x2+w,y2+h,counters,h1,h)
            crop_th = crop_image(thresholded_image, center, angle, w, h)
            crop = crop_image(loaded_image, center, angle, w, h)
            image = crop
            # create_dir(individual+file+'/')
            cv2.cv.SaveImage(newpath + '/' + 'contour_' + str(number) + '.jpg',
                             crop_th)
            cv2.cv.SaveImage(
                actual_contours_path + 'contour_' + str(number) + '.jpg', crop)
            # cv2.cv.SaveImage(individual+ file+'/' + str(random.randint(1,50000))  + '.jpg',crop)
            temp_image = PIL.Image.open(newpath + '/' + 'contour_' +
                                        str(number) + '.jpg')
            original_temp_image = PIL.Image.open(actual_contours_path +
                                                 'contour_' + str(number) +
                                                 '.jpg')
            segment_list.append(temp_image)
            original_segment_list.append(original_temp_image)

            # image = original_temp_image
            image = skimage.color.rgb2gray(
                skimage.io.imread(actual_contours_path + 'contour_' +
                                  str(number) + '.jpg'))
            delete_file(newpath + '/' + 'contour_' + str(number) + '.jpg')
            delete_file(actual_contours_path + 'contour_' + str(number) +
                        '.jpg')
            total = []
            h = image.shape[0]
            w = image.shape[1]

            for x in xrange(h):
                s = 0
                for y in xrange(w):
                    s += image[x][y]
                total.append(s)

            avg = [sum(total) / len(total)] * len(total)
            T = list(range(len(total)))
            t = np.array(T)
            power = np.array(total)
            totalnew = np.linspace(t.min(), t.max(), len(total))
            power_smooth = spline(t, power, totalnew)

            # ax = axs[1]
            sigma = 3
            x_g1d = gaussian_filter1d(totalnew, sigma)
            y_g1d = gaussian_filter1d(power_smooth, sigma)

            index = []
            temp = 0
            for i in xrange(1, len(y_g1d) - 1):
                if y_g1d[i] > y_g1d[i - 1] and y_g1d[i] > y_g1d[i + 1]:
                    index.append(i)

            if len(index) == 0:
                x_g1d = totalnew
                y_g1d = power_smooth
                for i in xrange(1, len(y_g1d) - 1):
                    if y_g1d[i] > y_g1d[i - 1] and y_g1d[i] > y_g1d[i + 1]:
                        index.append(i)

            cm = []

            for x in xrange(1, len(index)):
                for y in xrange(x):
                    if y_g1d[index[y]] < y_g1d[index[x]]:
                        temp = index[y]
                        index[y] = index[x]
                        index[x] = temp

            if len(index) > 0:
                mx = [y_g1d[index[0]]] * len(total)
                # plt.plot(t,mx)
                cent1 = index[0]
                # ax=axs[0]
                cm1 = (w / 2, cent1)
                cv2.circle(image, cm1, 3, (0, 1, 0), -1)
                cm.append(cm1)
            DCcount = 0

            if len(index) > 1 and y_g1d[index[1]] > avg[0] and abs(
                    y_g1d[cent1] - y_g1d[index[1]]) < abs(y_g1d[index[1]] -
                                                          y_g1d[int(avg[0])]):
                # if len(index)>1 and total[index[1]]>avg[0] and abs(cent1-index[1])>h/a and abs(total[cent1]-total[index[1]])<abs(total[index[1]]-total[int(avg[0])]):
                mx2 = [y_g1d[index[1]]] * len(total)
                # plt.plot(t,mx2)
                cent2 = index[1]
                cm2 = (w / 2, cent2)
                cv2.circle(image, cm2, 3, (0, 1, 0), -1)
                cm.append(cm2)
                DCcount += 1

            if len(cm) == 2:
                red_number += 1
                rect = cv2.minAreaRect(c)
                box = cv2.cv.BoxPoints(rect)
                temp_box = list(box)
                temp_box.append((1, 1))
                box = np.int0(box)

                temp_coordinates.append(temp_box)
                # print "coordinates of dc"
                # print box
                cv2.drawContours(actual_image, [box], 0, (0, 0, 255), 2)
            else:
                green_number += 1
                rect = cv2.minAreaRect(c)
                box = cv2.cv.BoxPoints(rect)
                temp_box = list(box)
                temp_box.append((0, 0))
                box = np.int0(box)

                temp_coordinates.append(temp_box)
                # print "coordinates of mc"
                # print box
                cv2.drawContours(actual_image, [box], 0, (0, 255, 0), 2)
    f = open(pathses, 'w')
    ##print(w1,h1,counters,w1/(counters),h1/(counters))
    #print(pathses)
    #print("dsfj sdjfhjdsfdsfjdsjf jdsfjds jfdsjjf sjfdjsfjdsh")
    f.write('{}  {}  {}  {}  {}  {}'.format(xmin, ymin, xmax, ymax,
                                            w1 / (counters), h1 / (counters)))
    f.close()
    coordinates_of_segments[file] = temp_coordinates
    cv2.imwrite(segment_path + filename, actual_image)
    time.sleep(0.30)
    #cv2.waitKey(50)
    print "^^^^^^^^^^^^^^", time.time()
    i = count + 1
    # while soft_sheet['A'+str(i)]==None:
    soft_sheet['A' + str(i)] = file
    soft_sheet['B' + str(i)] = number
    if not DCcount == 0:
        soft_sheet['C' + str(i)] = DCcount
    pathses, tail = os.path.split(pathses)
    pathses, tail = os.path.split(pathses)
    soft_data.save('data.xlsx')
    mc_and_dc_list.append([red_number, green_number, number, tail[8:]])
    return mc_and_dc_list, number, segment_list, original_segment_list, coordinates_list, coordinates_of_segments
Ejemplo n.º 45
0
def detect_markers(gray_img,grid_size,min_marker_perimeter=40,aperture=11,visualize=False):
    edges = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, aperture, 9)

    contours, hierarchy = cv2.findContours(edges,
                                    mode=cv2.RETR_TREE,
                                    method=cv2.CHAIN_APPROX_SIMPLE,offset=(0,0)) #TC89_KCOS

    # remove extra encapsulation
    hierarchy = hierarchy[0]
    contours = np.array(contours)
    # keep only contours                        with parents     and      children
    contained_contours = contours[np.logical_and(hierarchy[:,3]>=0, hierarchy[:,2]>=0)]
    # turn on to debug contours
    # cv2.drawContours(gray_img, contours,-1, (0,255,255))
    # cv2.drawContours(gray_img, aprox_contours,-1, (255,0,0))

    # contained_contours = contours #overwrite parent children check

    #filter out rects
    aprox_contours = [cv2.approxPolyDP(c,epsilon=2.5,closed=True) for c in contained_contours]

    # any rectagle will be made of 4 segemnts in its approximation
    # also we dont need to find a marker so small that we cannot read it in the end...
    # also we want all contours to be counter clockwise oriented, we use convex hull fot this:
    rect_cand = [cv2.convexHull(c,clockwise=True) for c in aprox_contours if c.shape[0]==4 and cv2.arcLength(c,closed=True) > min_marker_perimeter]
    # a non convex quadrangle is not what we are looking for.
    rect_cand = [r for r in rect_cand if r.shape[0]==4]

    if visualize:
        cv2.drawContours(gray_img, rect_cand,-1, (255,100,50))


    markers = {}
    size = 10*grid_size
    #top left,bottom left, bottom right, top right in image
    mapped_space = np.array( ((0,0),(size,0),(size,size),(0,size)) ,dtype=np.float32).reshape(4,1,2)
    for r in rect_cand:
        if correct_gradient(gray_img,r):
            r = np.float32(r)
            M = cv2.getPerspectiveTransform(r,mapped_space)
            flat_marker_img =  cv2.warpPerspective(gray_img, M, (size,size) )#[, dst[, flags[, borderMode[, borderValue]]]])

            # Otsu documentation here :
            # https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html#thresholding
            _ , otsu = cv2.threshold(flat_marker_img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

            # getting a cleaner display of the rectangle marker
            kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
            cv2.erode(otsu,kernel,otsu, iterations=3)
            # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
            # cv2.dilate(otsu,kernel,otsu, iterations=1)

            marker = decode(otsu, grid_size)
            if marker is not None:
                angle,msg = marker

                # define the criteria to stop and refine the marker verts
                criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
                cv2.cornerSubPix(gray_img,r,(3,3),(-1,-1),criteria)

                centroid = r.sum(axis=0)/4.
                centroid.shape = (2)
                # angle is number of 90deg rotations
                # roll points such that the marker points correspond with oriented marker
                # rolling may not make the verts appear as you expect,
                # but using m_screen_to_marker() will get you the marker with proper rotation.
                r = np.roll(r,angle+1,axis=0) #np.roll is not the fastest when using these tiny arrays...

                marker = {'id':msg,'verts':r,'perimeter':cv2.arcLength(r,closed=True),'centroid':centroid,"frames_since_true_detection":0}
                if visualize:
                    marker['img'] = np.rot90(otsu,-angle/90)
                if markers.has_key(marker['id']) and markers[marker['id']]['perimeter'] > marker['perimeter']:
                    pass
                else:
                    markers[marker['id']] = marker


    return markers.values()
Ejemplo n.º 46
0
def get_grid_dimensions(image):
    """
    Tries to locate the grid dimension from a raw image

    Process:
    1. Converts the given image into greyscale
    2. Apply gaussian blur to smooth the image
    3. Apply thresholding to obtain a inverted binary image (Lines will be white while background will be black)
    4. Apply contour detection and sort the contours from largest to smallest
    5. Loop and filter the contour. Finding the largest and square contour (Assume the largest square contour will be the grid)
    6. Returns the grid dimensions

    :param image: type: numpy.ndarray
    The raw sudoku image in color format

    :return: type: tuple if grid is found, else None
    Returns the grid coordinates in top left, top right, bottom right, bottom left order. If no grid is found, returns None
    """
    # Reduce noise
    # Convert image to greyscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Gaussian blur
    blur = cv2.GaussianBlur(gray, (5, 5), 0)

    # Thresholding
    thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY_INV, 11, 3)

    # Contour detection, assuming grid is the biggest contour in the image
    cnts, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)

    # Sort from descending order & loop
    for cnt in sorted(cnts, key=lambda x: cv2.contourArea(x), reverse=True):
        # Use contours to transform grid into "Top down view/bird's eye view
        # Find perimeter
        peri = cv2.arcLength(cnt, True)
        # Find corners
        approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)

        # Filter only square/rect for grid
        if len(approx) == 4:
            # Unpack & Identify corners
            corners = sorted([(corner[0][0], corner[0][1])
                              for corner in approx],
                             key=lambda x: x[0],
                             reverse=False)
            top_corners, bottom_corners = corners[:-2], corners[2:]
            top_left, bottom_left = sorted(top_corners,
                                           key=lambda x: x[1],
                                           reverse=False)
            top_right, bottom_right = sorted(bottom_corners,
                                             key=lambda x: x[1],
                                             reverse=False)

            # Return detected grid dimensions
            return top_left, top_right, bottom_right, bottom_left

    # Unable to find grid
    return None
Ejemplo n.º 47
0
    def identify_colors(self, ros_image):

        try:

            cv_image = self.bridge.imgmsg_to_cv2(ros_image, "bgr8")

            #turn image to hsv image
            hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)

            #setting up lower and upper hsv bounds
            #these bounds were found using the hue_adjust script
            lower_green = np.array([22, 88, 0])
            upper_green = np.array([65, 255, 255])

            lower_blue = np.array([66, 47, 0])
            upper_blue = np.array([149, 255, 255])

            lower_yellow = np.array([15, 159, 164])
            upper_yellow = np.array([107, 255, 255])

            #erode rectangle elementof size 3x3 pixels
            erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

            #dilate rectangle element of size 8x8 pixels
            dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))

            mask_green = cv2.inRange(hsv, lower_green, upper_green)

            mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)

            mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)

            #erode hsv image to get rid of outliers
            mask_green = cv2.erode(mask_green, erode_element)
            #dilate hsv to recover lost points of interest
            mask_green = cv2.dilate(mask_green, dilate_element)

            #erode hsv image to get rid of outliers
            mask_blue = cv2.erode(mask_blue, erode_element)
            #dilate hsv to recover lost points of interest
            mask_blue = cv2.dilate(mask_blue, dilate_element)

            #erode hsv image to get rid of outliers
            mask_yellow = cv2.erode(mask_yellow, erode_element)
            #dilate hsv to recover lost points of interest
            mask_yellow = cv2.dilate(mask_yellow, dilate_element)

            ###################### FIND YELLOW ###################################

            (cnts, nah) = cv2.findContours(mask_yellow.copy(),
                                           cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
            cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:1]

            for c in cnts:

                #get the perimeter of the countour
                peri = cv2.arcLength(c, True)
                #approximate perimeter to generate smooth shape
                approx = cv2.approxPolyDP(c, 0.01 * peri, True)
                screenCnt = approx
                #get the area bounded by the contour
                area = cv2.contourArea(c)

                #if the area is bigger than 30x30 pixels
                if area >= 30 * 30:

                    moment = cv2.moments(c)

                    #getting centroid coordinates
                    cx = int(moment['m10'] / moment['m00'])
                    cy = int(moment['m01'] / moment['m00'])
                    h = int(peri / 5)
                    w = int(peri / 20)

                    #Draw yellow rectangle around yellow block
                    cv2.rectangle(cv_image, (cx - h, cy - w), (cx + h, cy + w),
                                  (10, 240, 240), 2)

                    # Write on image
                    #write 'yellow block' and the coordinates of the crentroid
                    yellow_caption = "YELLOW BLOCK (%s,%s)" % (cx, cy)
                    cv2.putText(cv_image, yellow_caption, (cx, cy),
                                cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 0, 0), 1,
                                1)
                    #cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)

            ######################################################################################################

            ############################### FIND BLUE  #################################

            (blue_cnts, nah2) = cv2.findContours(mask_blue.copy(),
                                                 cv2.RETR_EXTERNAL,
                                                 cv2.CHAIN_APPROX_SIMPLE)
            cnts = sorted(blue_cnts, key=cv2.contourArea, reverse=True)[:1]

            for c in blue_cnts:

                #get the perimeter of the countour
                peri = cv2.arcLength(c, True)
                #approximate perimeter to generate smooth shape
                approx = cv2.approxPolyDP(c, 0.01 * peri, True)
                screenCnt = approx
                #get the area bounded by the contour
                area = cv2.contourArea(c)

                #if the area is bigger than 30x30 pixels
                if area >= 30 * 30:

                    moment = cv2.moments(c)

                    #getting centroid coordinates
                    cx = int(moment['m10'] / moment['m00'])
                    cy = int(moment['m01'] / moment['m00'])
                    h = int(peri / 5)
                    w = int(peri / 20)

                    #Draw yellow rectangle around yellow block
                    cv2.rectangle(cv_image, (cx - h, cy - w), (cx + h, cy + w),
                                  (255, 0, 0), 2)

                    # Write on image
                    #write 'yellow block' and the coordinates of the crentroid
                    blue_caption = "BLUE BLOCK (%s,%s)" % (cx, cy)
                    cv2.putText(cv_image, blue_caption, (cx, cy),
                                cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 0, 0), 1,
                                1)

            ############################################################################################################

            ######################### FIND GREEN ###############################################

            (green_cnts, nah3) = cv2.findContours(mask_green.copy(),
                                                  cv2.RETR_EXTERNAL,
                                                  cv2.CHAIN_APPROX_SIMPLE)
            green_cnts = sorted(green_cnts, key=cv2.contourArea,
                                reverse=True)[:1]

            for c in green_cnts:

                #get the perimeter of the countour
                peri = cv2.arcLength(c, True)
                #approximate perimeter to generate smooth shape
                approx = cv2.approxPolyDP(c, 0.01 * peri, True)
                screenCnt = approx
                #get the area bounded by the contour
                area = cv2.contourArea(c)

                #if the area is bigger than 30x30 pixels
                if area >= 30 * 30:

                    moment = cv2.moments(c)

                    #getting centroid coordinates
                    cx = int(moment['m10'] / moment['m00'])
                    cy = int(moment['m01'] / moment['m00'])
                    h = int(peri / 5)
                    w = int(peri / 20)

                    #Draw yellow rectangle around yellow block
                    cv2.rectangle(cv_image, (cx - h, cy - w), (cx + h, cy + w),
                                  (0, 255, 0), 2)

                    # Write on image
                    #write 'yellow block' and the coordinates of the crentroid
                    green_caption = "GREEN BLOCK (%s,%s)" % (cx, cy)
                    cv2.putText(cv_image, green_caption, (cx, cy),
                                cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 0, 0), 1,
                                1)

            ####################################################################################################################
            # Why is it not publishing??
            #self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))

            cv2.imshow("COLORS", cv_image)

            key = cv2.waitKey(20)

            if key == 27:
                exit()

        except CvBridgeError, e:
            print e
Ejemplo n.º 48
0
    frame = cv2.drawContours(frame, contours, -1, (0, 0, 250), 1)
    cv2.fillPoly(frame, pts=contours, color=(255, 0, 255))

    cv2.imshow('CONTOUR', frame)
    cv2.moveWindow('CONTOUR', 1400, 0)

    #get conture centers
    contours_poly = len(contours)
    center = len(contours)
    radius = len(contours)
    #find enclosing polygon which fits around the contures
    rMax = 1
    origo = (100, 100)
    for i in range(0, len(contours)):
        cont = contours[i]
        approx = cv2.approxPolyDP(cont, 2, True)
        #(x,y),radius = cv2.minEnclosingCircle(approx)
        (x, y), radius = cv2.minEnclosingCircle(cont)
        center = (int(x), int(y))
        radius = int(radius)
        frameCircle = cv2.circle(frame, center, radius, (255, 0, 0), 2)
        if x > 0 and x < 640 and y > 0 and y < 480:  #ROI window for region of interes can be smaller (not the full window)
            if int(radius) > rMax:
                rMax = int(radius)
                origo = center
    print("rMax & origo: ")
    print(rMax, origo)

    frameCircle = cv2.line(frame, (100, 200), (100, 100), (0, 255, 255),
                           2)  # ideal line
    frameCircle = cv2.line(frame, (100, 200), origo, (0, 0, 250),
Ejemplo n.º 49
0
                white, contours, hierarchy = cv2.findContours(
                    white, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

                #cv2.drawContours(white,contours,-1,100,3)

                block_n = detectors.block_number_detector(bNw)

                #print 'block number = ', block_n

                all_shape = []
                all_angle = []
                for contour in contours:

                    perimeter = cv2.arcLength(contour, True)
                    approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True)

                    shape, angle = detectors.shape_detector(approx)
                    all_shape.append(shape)
                    all_angle.append(angle)

                #print all_shape
                #print 'orientation:', all_angle
                eva = 'Wrong'

                if block_n == 1:
                    top = detectors.top_detector(all_shape, all_angle)
                    print 'top surface:', top

                elif block_n == 0:
                    pass
    def Draw_triangle(self,contours,rgb,obj_desire):
        ##################
        DELAY = 0.02
        USE_CAM = 1
        IS_FOUND = 0
        count=0#count feature tile numbers
        cnt=0



        central_list=[]
        uvuv=uv()
        tile_uv=tileuv()
        ##################
        _width  = 480.0
        _height = 640.0
        _margin = 0.0
        corners = np.array(
            [
                [[  		_margin, _margin 			]],
                [[ 			_margin, _height + _margin  ]],
                [[ _width + _margin, _height + _margin  ]],
                [[ _width + _margin, _margin 			]],
            ]
        )

        pts_dst = np.array( corners, np.float32 )
        latest_central=(0,0)
        for cont in contours:
            resultuv = []
            """
            #1,num,2,centeral point 3,for angular point uv ,4,clockwise direction
            #caculating Area for tile selected just one tile
            """
            # print "cont----------", cont
            # 获取轮廓长度
            arc_len = cv2.arcLength(cont, True)
            # 多边形拟合
            approx = cv2.approxPolyDP(cont, 0.1 * arc_len, True)
            # print "cv2.contourArea(cont)",cv2.contourArea(cont)
            # print "approx",len(np.array(approx).reshape(-1,2))
            if cv2.contourArea(cont) > 3000 and cv2.contourArea(cont) < 8000:
            # if cv2.contourArea(cont) > 3000:
                if (len(approx) == 4):
                    IS_FOUND = 1
                    M = cv2.moments(cont)
                    # 获取图像质心坐标
                    cX = int(M["m10"] / M["m00"])
                    cY = int(M["m01"] / M["m00"])
                    now_central = (cX, cY)
                    if self.Judge_isnot_same_tile(latest_central, now_central) != 1:
                        count += 1
                    cv2.putText(rgb, str(count), (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 3)
                    print "CX,CY", [cX, cY]
                    central_list.append([cX, cY])
                    pts_src = np.array(approx, np.float32)
                    # print "pts_src", pts_src
                    cv2.circle(rgb, (cX, cY), 5, (0, 0, 0), -1)
                    # print approx.tolist()
                    angular_point = []
                    new_approx=self.Sort_tile_feature(approx)
                    print "new_approx",new_approx
                    for i in range(len(new_approx)):
                        if i == 0:
                            cv2.circle(rgb, (new_approx[i][0], new_approx[i][1]), 5, (20, 60, 220), -1)
                            angular_point.append([new_approx[i][0], new_approx[i][1]])

                            cv2.putText(rgb, str(i), (new_approx[i][0], new_approx[i][1]),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 1)
                        else:
                            cv2.circle(rgb, (new_approx[i][0], new_approx[i][1]), 5, (0, 255, 0), -1)
                            angular_point.append([new_approx[i][0], new_approx[i][1]])

                            cv2.putText(rgb, str(i), (new_approx[i][0], new_approx[i][1]),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 1)

                    resultuv.append([[count],[cX, cY], angular_point])
                    # draw trangle in image

                    h, status = cv2.findHomography(pts_src, pts_dst)
                    out = cv2.warpPerspective(rgb, h, (int(_width + _margin * 2), int(_height + _margin * 2)))

                    cv2.drawContours(rgb, [approx], -1, (0, 255, 255), 3)
                    # cv2.drawContours(rgb, [approx], -1, (20*count, 255, 0), -1, cv2.LINE_AA)
                    print "all info for tile------", resultuv
                    cv2.line(rgb, ((angular_point[0][0]+angular_point[1][0])/2,(angular_point[0][1]+angular_point[1][1])/2), (cX,cY), (255,0,0), 3)
                    print "Now tile id",count
                    if count == 1:
                        tile_uv.tile_id = count
                        tile_uv.obj_desire = obj_desire
                        tile_uv.cen_uv.uvinfo = [cX, cY]
                        tile_uv.f1th_uv.uvinfo = angular_point[0]
                        tile_uv.s2th_uv.uvinfo = angular_point[1]
                        tile_uv.t3th_uv.uvinfo = angular_point[2]
                        tile_uv.f4th_uv.uvinfo = angular_point[3]
                        self.tile_pub.publish(tile_uv)

                    latest_central = now_central

                else:
                    pass
                # count += 1
                # cnt += 11
        return rgb.copy()
# find contours in the edged image, then initialize the contour that corresponds to the document
cnts=cv2.findContours(edged.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts=imutils.grab_contours(cnts)
docCnt = None

# ensure that at least one contour was found
if len(cnts)>0:
    # sort the contours according to their size in descending order
    cnts=sorted(cnts,key=cv2.contourArea,reverse=True)

# loop over sorted contours    
for c in cnts:
    # approximate the contour
    peri=cv2.arcLength(c,True)
    approx=cv2.approxPolyDP(c,peri*0.02,True)
    # if approximated contour has four points,then we can assume we have found the paper
    if len(approx)==4:
        docCnt=approx
        break

# show the Outlined image
outline=image.copy()
cv2.drawContours(outline,[docCnt],-1,(0,255,0),2)
print(docCnt)
cv2.imshow("Outline",outline)
cv2.waitKey(0)
cv2.destroyAllWindows()

# apply a four point perspective transform to both the original image and grayscale image to obtain a top-down birds eye view of the paper
paper=four_point_transform(image,docCnt.reshape(4,2))
Ejemplo n.º 52
0
def imageProcess(img):
    NONE = -1
    FRONT = 0
    STOP = 1
    LEFTLINE = 2
    RIGHTLINE = 3
    i_param = NONE

    def onMouse(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            print("(%d, %d)" % (x, y))
            pixel = np.uint8([[img[y, x]]])
            print("BGR=", end='')
            print(pixel[0][0])
            pixel_hsv = cv2.cvtColor(pixel, cv2.COLOR_BGR2HSV)
            print("HSV=", end='')
            print(pixel_hsv[0][0])
            cv2.setTrackbarPos("LowH", "Trackbar Windows",
                               pixel_hsv[0][0][0] - 25)
            cv2.setTrackbarPos("HighH", "Trackbar Windows",
                               pixel_hsv[0][0][0] + 25)
            cv2.setTrackbarPos("LowS", "Trackbar Windows",
                               pixel_hsv[0][0][1] - 35)
            cv2.setTrackbarPos("HighS", "Trackbar Windows",
                               pixel_hsv[0][0][1] + 35)
            cv2.setTrackbarPos("LowV", "Trackbar Windows",
                               pixel_hsv[0][0][2] - 40)
            cv2.setTrackbarPos("HighV", "Trackbar Windows",
                               pixel_hsv[0][0][2] + 40)

    def hough(src):
        lines = cv2.HoughLinesP(src,
                                rho=2,
                                theta=np.pi / 180.0,
                                threshold=50,
                                minLineLength=20,
                                maxLineGap=10)
        return lines

    ##################################################
    img_show = img.copy()
    # weighted_img(img, img)

    # gray scaling
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Gaussian blur
    gray_blur = cv2.GaussianBlur(gray, ksize=(3, 3), sigmaX=0.0)
    blur = cv2.GaussianBlur(img, ksize=(3, 3), sigmaX=0.0)

    # HSV color
    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
    lower_Green = (65, 100, 50)
    upper_Green = (80, 255, 255)
    lower_Blue = (95, 75, 30)
    upper_Blue = (120, 255, 255)
    hsvGreen = cv2.inRange(hsv, lower_Green, upper_Green)
    hsvBlue = cv2.inRange(hsv, lower_Blue, upper_Blue)

    # pinkL, pinkU = getTrackbar()
    # hsvPink = cv2.inRange(hsv, pinkL, pinkU)

    # Canny
    edges = cv2.Canny(gray_blur, 50, 100)

    # add hsv and edges binary image
    img_bin = cv2.add(edges, hsvGreen)

    ## ROI
    height, width = img.shape[:2]
    # traffic light ROI
    hsvBlue_roi = roi(
        hsvBlue,
        np.array([[(width // 2, 0), (width, 0), (width, int(height * 0.6)),
                   (width // 2, int(height * 0.6))]],
                 dtype=np.int32))

    # delete small objects
    kernel = np.ones((7, 7), np.uint8)
    hsvBlue_roi = cv2.morphologyEx(hsvBlue_roi, cv2.MORPH_CLOSE, kernel)

    retval, labels, stats, centroids = cv2.connectedComponentsWithStats(
        hsvBlue_roi)
    min_pixels = 1000
    blue_vertices = []
    for i in range(1, retval):
        (x, y, w, h, area) = stats[i]

        # select two biggest areas
        if area > min_pixels:
            vertices1 = np.array([[(x, y), (x + w, y), (x + w, y + h),
                                   (x, y + h)]],
                                 dtype=np.int32)
            blue_vertices.append(vertices1)
    hsvBlue_roi = roi(hsvBlue_roi, blue_vertices)

    _, contours, hierarchy = cv2.findContours(hsvBlue_roi, cv2.RETR_CCOMP,
                                              cv2.CHAIN_APPROX_NONE)

    # find traffic light
    lower_GL = (60, 0, 200)
    upper_GL = (100, 100, 255)
    lower_RL = (0, 0, 150)
    upper_RL = (30, 100, 255)
    for i in range(len(contours)):
        approx = cv2.approxPolyDP(contours[i],
                                  cv2.arcLength(contours[i], True) * 0.02,
                                  True)
        vtc = len(approx)
        if vtc == 4 and hierarchy[0][i][2] != -1:
            inner_cont = hierarchy[0][i][2]
            approx = cv2.approxPolyDP(
                contours[inner_cont],
                cv2.arcLength(contours[inner_cont], True) * 0.02, True)
            vtc = len(approx)
            if vtc == 4:
                (c_x, c_y, c_w, c_h) = cv2.boundingRect(contours[inner_cont])
                hsv_TL = hsv[c_y:c_y + c_h, c_x:c_x + c_w]
                hsv_TL_green = cv2.inRange(hsv_TL, lower_GL, upper_GL)
                hsv_TL_red = cv2.inRange(hsv_TL, lower_RL, upper_RL)
                # cv2.imshow('TL_G', hsv_TL_green)
                cv2.imshow('TL_R', hsv_TL_red)
                for i in hsv_TL_red:
                    if list(i).count(255) > 2:
                        i_param = STOP
                        cv2.rectangle(img_show, (c_x, c_y),
                                      (c_x + c_w, c_y + c_h), (255, 0, 0), 1)
                        cv2.putText(img_show, 'Light', (c_x, c_y + 20),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255))
                        break
                for i in hsv_TL_green:
                    if list(i).count(255) > 5:
                        i_param = FRONT
                        cv2.rectangle(img_show, (c_x, c_y),
                                      (c_x + c_w, c_y + c_h), (255, 0, 0), 1)
                        cv2.putText(img_show, 'Light', (c_x, c_y + 20),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0))
                        break
                if i_param == NONE:
                    cv2.rectangle(img_show, (c_x, c_y), (c_x + c_w, c_y + c_h),
                                  (255, 0, 0), 1)
                    cv2.putText(img_show, 'Light', (c_x, c_y + 20),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255))

    if i_param == NONE:
        i_param = FRONT

    # line ROI
    near_roi_height = 100
    mid_roi_height = 80
    # far_roi_height = 80
    near_height = height - near_roi_height
    mid_height = height - near_roi_height - mid_roi_height
    # far_height = height - near_roi_height - mid_roi_height - far_roi_height

    # detect line
    near_vertices, near_detected = get_roi_square(hsvGreen, img_bin, img_show,
                                                  near_height, height)
    mid_vertices, mid_detected = get_roi_square(hsvGreen, img_bin, img_show,
                                                mid_height, near_height)
    # far_vertices, far_lines = get_roi_square(hsvGreen, img_bin, img_show, far_height, mid_height)
    roi_bin = roi(img_bin, near_vertices + mid_vertices)

    detected = [near_detected, mid_detected]

    # draw center line
    cv2.line(img_show, (width // 2, height), (width // 2, height // 2),
             (50, 180, 50), 2)

    # cv2.imshow('origin', ori_img)
    cv2.imshow('frame', img_show)
    cv2.imshow('edges', edges)
    cv2.imshow('HSV green', hsvGreen)
    cv2.imshow('roi_bin', roi_bin)
    cv2.setMouseCallback('frame', onMouse)

    ret, buffer = cv2.imencode('.jpg', img_show)
    frame = buffer.tobytes()

    return frame, detected, i_param
Ejemplo n.º 53
0
def buscador(cola_frames, val_ini, color, cola_obj):
    cv.namedWindow("Barras " + color)

    cv.createTrackbar('L-H', "Barras " + color, val_ini.l_h, 180, nothing)
    cv.createTrackbar('U-H', "Barras " + color, val_ini.u_h, 180, nothing)

    cv.createTrackbar('L-S', "Barras " + color, val_ini.l_s, 255, nothing)
    cv.createTrackbar('U-S', "Barras " + color, val_ini.u_s, 255, nothing)

    cv.createTrackbar('L-V', "Barras " + color, val_ini.l_v, 255, nothing)
    cv.createTrackbar('U-V', "Barras " + color, val_ini.u_v, 255, nothing)

    while True:
        if (cola_frames.empty()):
            nothing(1)
        else:
            frame = cola_frames.get()

            hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)

            l_h = cv.getTrackbarPos('L-H', "Barras " + color)
            l_s = cv.getTrackbarPos('L-S', "Barras " + color)
            l_v = cv.getTrackbarPos('L-V', "Barras " + color)
            u_h = cv.getTrackbarPos('U-H', "Barras " + color)
            u_s = cv.getTrackbarPos('U-S', "Barras " + color)
            u_v = cv.getTrackbarPos('U-V', "Barras " + color)

            color_min = np.array([l_h, l_s, l_v])
            color_max = np.array([u_h, u_s, u_v])

            mask = cv.inRange(hsv, color_min, color_max)
            kernel = np.ones((5, 5), np.uint8)

            mask = cv.erode(mask, kernel)

            contours, _ = cv.findContours(mask, cv.RETR_TREE,
                                          cv.CHAIN_APPROX_SIMPLE)

            if (len(contours) < 1):
                if cola_obj.full():
                    pass
                else:
                    cola_obj.put([color, "NA"])
            else:
                areamax = 0
                for contour in contours:
                    area = cv.contourArea(contour)

                    approx = cv.approxPolyDP(
                        contour, 0.02 * cv.arcLength(contour, True), True)

                    x = approx.ravel()[0]
                    y = approx.ravel()[1]

                    if (area > 400):
                        momentos = cv.moments(contour)
                        cx = int(momentos['m10'] / momentos['m00'])
                        cy = int(momentos['m01'] / momentos['m00'])
                        if len(approx) == 3:
                            cv.drawContours(frame, [approx], 0,
                                            (val_ini.B, val_ini.G, val_ini.R),
                                            2)
                            cv.circle(frame, (cx, cy), 3,
                                      (val_ini.B, val_ini.G, val_ini.R), -1)
                            if cola_obj.full():
                                pass
                            else:
                                cola_obj.put([color, area, "T", cx, cy])

                            cv.putText(frame, "Base", (x, y), font, 1,
                                       (255, 255, 255))
                        elif len(approx) >= 4 and len(approx) <= 8:
                            if area > areamax:
                                areamax = area
                                cv.drawContours(
                                    frame, [approx], 0,
                                    (val_ini.B, val_ini.G, val_ini.R), 2)
                                cv.circle(frame, (cx, cy), 3,
                                          (val_ini.B, val_ini.G, val_ini.R),
                                          -1)
                                if cola_obj.full():
                                    pass
                                else:
                                    cola_obj.put([color, area, "C", cx, cy])

                                cv.putText(frame, "Objetivo", (x, y), font, 1,
                                           (255, 255, 255))

            cv.imshow("Mask " + color, mask)
            cv.imshow("Siguiendo " + color, frame)

        if cv.waitKey(1) == ord('q'):
            break

        cv.waitKey(1)
Ejemplo n.º 54
0
files = glob.glob(f"{path}/map.topo.*.png")
files.sort()
for file in files:
    image = imageio.imread(file)
    image[mask[0], mask[1], :] = 0
    for region in regions:
        min_row, min_col, max_row, max_col = region.bbox
        rotated = img_as_ubyte(
            rotate(image[min_row:max_row, min_col:max_col, :],
                   angle=90 - math.degrees(region.orientation),
                   order=0))
        _, thresh = cv2.threshold(cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY), 1,
                                  255, cv2.THRESH_BINARY)
        contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)
        x, y, w, h = cv2.boundingRect(contours[-1])
        crop = rotated[y:y + h, x:x + w, :]
        cv2.imwrite(
            file.replace(".png", f".reg{region.label}.png").replace(
                "map.topo", "arrow."), crop)

mask_img = cv2.imread(f"{path}/mask.png")
_, thresh = cv2.threshold(cv2.cvtColor(mask_img, cv2.COLOR_BGR2GRAY), 1, 255,
                          cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)
image = cv2.imread(f"{path}/nets.sh7.png")
for cnt in contours:
    approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
    cv2.drawContours(image, [approx], 0, (0, 0, 0), 5)
cv2.imwrite(f"{path}/arrow.nets.sh7.png", image)
Ejemplo n.º 55
0
# apply Canny Edge Detection
edged = cv2.Canny(blurred, 0, 50)
orig_edged = edged.copy()

# find contours in the edged image
# keep the largest contours and initialize the screen contour
(contours, _) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)

#x,y,w,h = cv2.boundingRect(contours[0])
#cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),0)

# get approximate contour
for c in contours:
    p = cv2.arcLength(c, True)
    approx_cont = cv2.approxPolyDP(c, 0.02 * p, True)

    if len(approx_cont) == 4:
        target = approx_cont
        break


# mapping target points to 800x800 quadrilateral
approx_cont = rect.rectify(target)
pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])

M = cv2.getPerspectiveTransform(approx_cont,pts2)
dst = cv2.warpPerspective(orig,M,(800,800))

cv2.drawContours(image, [target], -1, (0, 255, 0), 2)
dst = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
Ejemplo n.º 56
0
plt.subplot(223), plt.imshow(cv2.cvtColor(
    orig, cv2.COLOR_BGR2RGB)), plt.title('Contour')
plt.xticks([]), plt.yticks([])
orig2 = img.copy()

# create hull array for convex hull points
hull_arr = []
simple = []
# calculate points for each contour
for i in range(len(contours)):
    # creating convex hull object for each contour
    hull = cv2.convexHull(contours[i], True)
    hull_arr.append(hull)
    peri = cv2.arcLength(contours[i], True)
    approx = cv2.approxPolyDP(contours[i], 0.01 * peri, True)
    simple.append(approx)
    #x, y, w, h = cv2.boundingRect(contours[i])
    #cv2.rectangle(orig2, (x, y), (x + w, y + h), (0, 0, 255), 2)
    rows, cols = orig2.shape[:2]
    [vx, vy, x, y] = cv2.fitLine(contours[i], cv2.DIST_L2, 0, 0.01, 0.01)
    lefty = int((-x * vy / vx) + y)
    righty = int(((cols - x) * vy / vx) + y)
    cv2.line(orig2, (cols - 1, righty), (0, lefty), (0, 0, 255), 3)

    M = cv2.moments(hull)
    cX = int(M['m10'] / M['m00'])
    cY = int(M['m01'] / M['m00'])
    cv2.circle(orig2, (cX, cY), 25, (255, 0, 255), -1)

cv2.drawContours(orig2, hull_arr, -1, (0, 255, 0), 3)
Ejemplo n.º 57
0
def approx_poly(cnt, e):
    #cnt = an input contour
    #e = percentage value of arc length used to define approximation
    #returns an approximation of cnt
    return cv2.approxPolyDP(cnt, e * arc_len(cnt), True)
Ejemplo n.º 58
0
            cv2.THRESH_BINARY,11,2)

#invert
th3=cv2.bitwise_not(th3)
cv2.imshow('threshold',th3)
cv2.waitKey(0)

#find contours
im2, contours, hierarchy = cv2.findContours(th3,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
c2=[]
#filter out small contours by area
for i in contours:
    area = cv2.contourArea(i)
    if area>80:
        epsilon = 0.0001*cv2.arcLength(i,True)
        approx = cv2.approxPolyDP(i,epsilon,True)
        c2.append(approx)
        print(area)
        
        
# create new image with only contours and blur it using mean blur
a=np.zeros(img.shape,np.uint8)
cv2.drawContours(a, c2, -1, (255,255,255), 1)
a=cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
a=cv2.bitwise_not(a)
kernel = np.ones((5,5),np.float32)/25
#
#a=cv2.bitwise_not(a)
a = cv2.filter2D(a,-1,kernel)
cv2.imwrite("c2.jpeg",a)
cv2.imshow('fin',a)
Ejemplo n.º 59
0
## so that they can be picked up by a robotic system and placed in different bins

import numpy as np
import cv2

img2 = cv2.imread('Task 5.png')
img = img2.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  #convert to gray scale

ret, thresh = cv2.threshold(gray, 127, 255, 0)  #randomly selected threshold

contours, h = cv2.findContours(thresh, 1,
                               2)  # built in funcino to find contours

for cnt in contours:
    approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True),
                              True)  #count number of arcs to make a contour
    #highlight different contours in different ways based on number of sides
    if len(approx) == 4:
        print("square")
        cv2.drawContours(img, [cnt], 0, (0, 0, 255),
                         3)  #draw contours on image
    elif len(approx) == 9:
        print("half-circle")
        cv2.drawContours(img, [cnt], 0, (255, 255, 0), 3)
    elif (len(approx) > 9):
        print("circle")
        cv2.drawContours(img, [cnt], 0, (10, 100, 100), 3)
        if len(approx) >= 14:
            print("Hole")
            cv2.drawContours(img, [cnt], 0, (100, 0, 10), 3)
Ejemplo n.º 60
-1
def get_corners(img):
    '''Finds the bounding rectangle of the most likely rectangle
    in the given greyscale image, and returns its corners'''
    gray = threshold(img)
    contours, hierachy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    best_contour = sorted(contours, key=lambda c:c.size, reverse=True)[0]
    approx = cv2.approxPolyDP(best_contour, 0.01*cv2.arcLength(best_contour, True), True)
    # print 'Length:', len(approx)


    print 'Got %d contours' % len(contours)
    # print 'C1:', contours[0]
    # print 'Shape:', contours[0].shape
    # size = [c.size for c in contours]
    # plt.hist(size, bins=20)
    # plt.show()
    red = cv2.cv.CV_RGB(255,0,0)
    cv2.drawContours(img, [best_contour], -1, red, 2)
    # cv2.drawContours(img, [approx], -1, red, 2)
    plt.imshow(img)
    plt.show()
    # cv2.imshow('Frame', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    corners = approx.flatten().reshape(len(approx), 2)
    sorted_corners = sorted(corners, key=np.sum)
    print 'Corners:', sorted_corners
    return sorted_corners