Exemplo n.º 1
0
def find_tree_contour(gray, param, min_frac=0.5):
    _,thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) # threshold
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
    dilated = cv2.dilate(thresh,kernel,iterations = param) # dilate
    im2, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 

    out = np.zeros(gray.shape, dtype=np.uint8) + 255
    
    for i, contour in enumerate(contours):
        [x,y,w,h] = cv2.boundingRect(contour)
        if gray.shape[-1] > gray.shape[-2]:
            if w < gray.shape[-1] * min_frac:
                continue
        elif gray.shape[-1] < gray.shape[-2]:
            if h < gray.shape[-2] * min_frac:
                continue
        else:
            if h < gray.shape[-2] * min_frac or w < gray.shape[-1] * min_frac:
                continue
        #cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,255), 1)
        cv2.drawContours(out, contours, i, 0, -1)

    out2 = np.zeros(gray.shape, dtype=np.uint8) + 255
    out2[out == 0] = gray[out == 0]
    return out2
Exemplo n.º 2
0
	def captureImage(self):
		position = [0, 0]
		velocity = [0, 0]

		frame = self.cap.read()[1]

		if frame != None:
			frame = cv2.flip(frame, 1)

			self.frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

			threshold_mask = self.createMultipleThresholds(self.frame_hsv)


			contour = self.getLargestContour(threshold_mask)
			if type(contour) != int:
				cv2.drawContours(frame, contour, -1, (0, 255, 255), 2)
				position = self.getContourMoment(contour)
				cv2.circle(frame, (position[0], position[1]), 5, (0,0,255), -1)

			# calculate velocity
			velocity = [position[0] - self.oldPosition[0], position[1] - self.oldPosition[1]]
			# print velocity

			cv2.imshow("Frame", frame)
			cv2.waitKey(10)

		self.oldPosition = position
		return [position, velocity]
Exemplo n.º 3
0
def track2(bs,img_copy,img, avg):
	x = -1
	y = -1

	img_copy = cv2.GaussianBlur(img_copy,(5,5),0)
	cv2.accumulateWeighted(img_copy,avg,0.4)
	res = cv2.convertScaleAbs(avg)
	res = cv2.absdiff(img, res)
	_,processed_img = cv2.threshold( res, 7, 255, cv2.THRESH_BINARY )
	processed_img = cv2.GaussianBlur(processed_img,(5,5),0)
	_,processed_img = cv2.threshold( processed_img, 240, 255, cv2.THRESH_BINARY )

	processed_img = bs.bg_subtractor.apply(processed_img, None, 0.05)
	
	# img_thresh = cv2.morphologyEx(img_thresh, cv2.MORPH_OPEN, kernel)
	
	if np.count_nonzero(processed_img) > 5:
		# Get the largest contour
		contours, hierarchy = cv2.findContours(processed_img, cv2.RETR_TREE, 
			cv2.CHAIN_APPROX_SIMPLE)
		areas = [cv2.contourArea(c) for c in contours]
		max_index = np.argmax(areas)

		# Make sure it's big enough
		if cv2.contourArea(contours[max_index]) >= MIN_BLOB_SIZE:
			cv2.drawContours(img, contours, max_index, (255, 255, 255), -1)
			x, y = getCentroid(contours[max_index])

	return x, y
Exemplo n.º 4
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
def make_boundaries(image):
    temp = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(temp,5,255,cv2.THRESH_BINARY_INV)
    contors,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    for i in range(1,len(contors),1):
         if 10000 > cv2.contourArea(contors[i]) > 540:
             cv2.drawContours(image,contors,i,(0,255,0),2)
Exemplo n.º 6
0
    def object_detection(self, depth_array):
        """
        Function to detect objects from the depth image given
        :return:
        """
        self.detect_arm()

        # Perform thresholding on the image to remove all objects behind a plain
        ret, bin_img = cv2.threshold(depth_array, 0.3, 1, cv2.THRESH_BINARY_INV)

        # Erode the image a few times in order to separate close objects
        element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
        err_img = cv2.erode(bin_img, element, iterations=20)

        # Create a new array of type uint8 for the findContours function
        con_img = np.array(err_img, dtype=np.uint8)

        # Find the contours of the image and then draw them on
        contours, hierarchy = cv2.findContours(con_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(con_img, contours, -1, (128, 255, 0), 3)

        for x in range(0, len(contours)):
            x, y, w, h = cv2.boundingRect(contours[x])
            cv2.rectangle(con_img, (x, y), ((x+w), (y+h)), (255, 0, 127), thickness=5, lineType=8, shift=0)

        # Show the colour images of the objects
        # self.show_colour(contours)

        # Show the Depth image and objects images
        cv2.imshow('Contours', con_img)
        cv2.imshow("Depth", bin_img)
        cv2.waitKey(3)
Exemplo n.º 7
0
def track(bs,img_copy,img, avg):
	x = -1
	y = -1

	img_copy = cv2.GaussianBlur(img_copy,(5,5),0)
	cv2.accumulateWeighted(img_copy,avg,0.4)
	res = cv2.convertScaleAbs(avg)

	res = bs.bg_subtractor.apply(res, None, 0.05)

	gradient = cv2.morphologyEx(res, cv2.MORPH_GRADIENT, kernel)

	processed_img = cv2.GaussianBlur(gradient,(5,5),0)

	_,threshold_img = cv2.threshold( processed_img, 20, 255, cv2.THRESH_BINARY )

	if np.count_nonzero(threshold_img) > 5:

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

		# totally not from stack overflow
		areas = [cv2.contourArea(c) for c in contours]
		# max_index  = np.argmax(areas)
		max_index = np.argmin(areas)
		# Make sure it's big enough
		if cv2.contourArea(contours[max_index]) >= MIN_BLOB_SIZE_ROBOT:
			# img_out = np.zeros(img_thresh.shape).astype(np.uint8)
			cv2.drawContours(img, contours, max_index, (255, 255, 255), -1)
			x, y = getCentroid(contours[max_index])

	return x, y
Exemplo n.º 8
0
def rectangle_mask(img, p1, p2, device, debug, color="black"):
  # takes an input image and returns a binary image masked by a rectangular area denoted by p1 and p2
  # note that p1 = (0,0) is the top left hand corner bottom right hand corner is p2 = (max-value(x), max-value(y))
  # device = device number. Used to count steps in the pipeline
  # debug = True/False. If True; print output image
  # get the dimensions of the input image
  ix, iy = np.shape(img)
  size = ix,iy
  # create a blank image of same size
  bnk = np.zeros(size, dtype=np.uint8)
  # draw a rectangle denoted by pt1 and pt2 on the blank image
  
  if color=="black":
    cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (255,255,255))
    ret, bnk = cv2.threshold(bnk,127,255,0)
    contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    # make sure entire rectangle is within (visable within) plotting region or else it will not fill with thickness = -1
    # note that you should only print the first contour (contour[0]) if you want to fill with thickness = -1
    # otherwise two rectangles will be drawn and the space between them will get filled
    cv2.drawContours(bnk, contour, 0 ,(255,255,255), -1)
    device +=1
  if color=="gray":
    cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (192,192,192))
    ret, bnk = cv2.threshold(bnk,127,255,0)
    contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    # make sure entire rectangle is within (visable within) plotting region or else it will not fill with thickness = -1
    # note that you should only print the first contour (contour[0]) if you want to fill with thickness = -1
    # otherwise two rectangles will be drawn and the space between them will get filled
    cv2.drawContours(bnk, contour, 0 ,(192,192,192), -1)
  if debug:
    print_image(bnk, (str(device) + '_roi.png'))
  return device, bnk, contour, hierarchy
Exemplo n.º 9
0
def border_mask(img, p1, p2, device, debug, color="black"):
  # by using rectangle_mask to mask the edge of plotting regions you end up missing the border of the images by 1 pixel
  # This function fills this region in
  # note that p1 = (0,0) is the top left hand corner bottom right hand corner is p2 = (max-value(x), max-value(y))
  # device = device number. Used to count steps in the pipeline
  # debug = True/False. If True; print output image
  if color=="black":
    ix, iy = np.shape(img)
    size = ix,iy
    bnk = np.zeros(size, dtype=np.uint8)
    cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (255,255,255))
    ret, bnk = cv2.threshold(bnk,127,255,0)
    contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(bnk, contour, -1 ,(255,255,255), 5)
    device +=1
  if color=="gray":
    ix, iy = np.shape(img)
    size = ix,iy
    bnk = np.zeros(size, dtype=np.uint8)
    cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (192,192,192))
    ret, bnk = cv2.threshold(bnk,127,255,0)
    contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(bnk, contour, -1 ,(192,192,192), 5)
    device +=1
  if debug:
    print_image(bnk, (str(device) + '_brd_mskd_' + '.png'))
  return device, bnk, contour, hierarchy
Exemplo n.º 10
0
def main():
	global red
	global white
	global coloured
	global pots
	img=cv2.imread('C:\\ASK\\Major\\image\\5.jpg')
	copy1=img
	start=timeit.default_timer()									
	cv2.imshow('Image',img)											
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	hsv=cv2.cvtColor(img,cv2.cv.CV_BGR2HSV)							
	lower_bound=np.array([38,150,40])
	upper_bound=np.array([75,255,200])
	masked=cv2.inRange(hsv,lower_bound,upper_bound)
	contour=structures.detect_table(masked)
	pots=structures.detect_pots(img,contour)
	cv2.drawContours(img,contour,contourIdx=-1,color=[0,0,255])
	cv2.imshow('Detected table',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	detect_balls(hsv)
	draw_balls(img,coloured)
	draw_balls(img,red)
	draw_balls(img,white)
	cv2.imshow('Detected balls and table',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	return (white,pots,coloured,red)
Exemplo n.º 11
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)
Exemplo n.º 12
0
def remove_blobs(image, max_area=1000, min_height=5):
    """
    Remove blobs with thresholed size from the image.

    Parameters
    ------------
    image : Numpy array

    max_area : float
        Area threshold

    min_height : float
        Height threshold

    Returns
    -------
    newimage : Numpy array
        The image with blobs removed

    """
    ret, img2 = cv2.threshold(image, 250, 255, 1)
    contours, hier = cv2.findContours(np.array(img2), 
                                      cv2.RETR_LIST, 
                                      cv2.CHAIN_APPROX_SIMPLE)
    mask = np.zeros(img2.shape, np.uint8)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        x, y, width, height = cv2.boundingRect(cnt)
        if cv2.contourArea(cnt) > max_area or height < min_height:
            cv2.drawContours(mask,[cnt],0, 255, -1)
 
    return cv2.bitwise_and(img2, cv2.bitwise_not(mask))
Exemplo n.º 13
0
    def run(self):
        started = time.time()
        print "started"
        print started
        while True:

            ret, frame = self.cap.read()
            currentframe = frame.copy()
            # cv2.imshow("Image", currentframe)
            instant = time.time()
            # print instant
            self.processImage (currentframe)
            if not self.isRecording:
                if self.somethingHasMoved():
                    self.speedEstimation()
                cv2.drawContours(currentframe, self.currentcontours,-1,(0, 0, 255),2)
            if self.show:
                for dist in self.tracks_dist:
                    if dist[2] > 0:
                        font = cv2.FONT_HERSHEY_SIMPLEX
                        # cv2.putText(currentframe,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)
                        # cv2.putText(currentframe, str(dist[2]/(9*5/30)), (60, 60), font, 4,(255,255,255),2,cv2.CV_AA)
                        draw_str(currentframe,(dist[0],dist[1]), str(dist[2]/(9*5/30)))
                cv2.imshow("Image", currentframe)
            self.prev_gray = self.gray_frame
            self.frame_idx += 1
            c = cv2.waitKey(1) % 0x100
            if c==27 or c == 10: #Break if user enters 'Esc'.
                break
def MomentDescriptor(name, thres):
    img1=cv2.imread(name)   
#     img=img1
    img=cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
#   edges=cv2.Canny(img, thres, thres*2) 
    #Image to draw the contours
    drawing=np.zeros(img.shape[:2], np.uint8)
    ret,thresh = cv2.threshold(img,thres,255,0)
   
    contours, hierarchy=cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    MomentVector=[]
    
    for cnt in contours:
        M=cv2.moments(cnt) #Calculate moments
        if M['m00']!=0:
            Cx=int(M['m10']/M['m00'])
            Cy=int(M['m01']/M['m00'])
            
            Moments_Area=M['m00'] # Contour area moment
            Contours_Area=cv2.contourArea(cnt) # Contour area using in_built function
           #Draw moment
            rect = cv2.minAreaRect(cnt)
            box = cv2.cv.BoxPoints(rect)
            box = np.int0(box)
#           cv2.drawContours(img1,contours, 0, (0,255,0),3) #draw contours in green color
            cv2.drawContours(img1,[box],0,(0,0,255),1)
            cv2.circle(img1, (Cx,Cy), 3,(0,255,0), -1)#draw centroids in red color
            MomentVector.append([M['m00'],Cx,Cy])
            cv2.imshow('winname',img1)
            cv2.waitKey(5000)
    print MomentVector
Exemplo n.º 15
0
    def draw_walls(self):
        left_wall_points = np.array([self.transform(point) for point in self.left_wall_points])
        right_wall_points = np.array([self.transform(point) for point in self.right_wall_points])

        rect = cv2.minAreaRect(left_wall_points[:,:2].astype(np.float32))
        box = cv2.cv.BoxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(self.grid, [box], 0, 128, -1)

        rect = cv2.minAreaRect(right_wall_points[:,:2].astype(np.float32))
        box = cv2.cv.BoxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(self.grid, [box], 0, 128, -1)

        # So I dont have to comment abunch of stuff out for debugging
        dont_display = True
        if dont_display:
            return

        # Bob Ross it up (just for display)
        left_f, right_f = self.transform(self.left_f), self.transform(self.right_f)
        left_b, right_b = self.transform(self.left_b), self.transform(self.right_b)

        boat = self.transform(self.boat_pos)
        target = self.transform(self.target)

        cv2.circle(self.grid, tuple(boat[:2].astype(np.int32)), 8, 255)
        cv2.circle(self.grid, tuple(target[:2].astype(np.int32)), 15, 255)
        cv2.circle(self.grid, tuple(self.transform(self.mid_point)[:2].astype(np.int32)), 5, 255)
        cv2.circle(self.grid, tuple(left_f[:2].astype(np.int32)), 10, 255)
        cv2.circle(self.grid, tuple(right_f[:2].astype(np.int32)), 10, 255)
        cv2.circle(self.grid, tuple(left_b[:2].astype(np.int32)), 3, 125)
        cv2.circle(self.grid, tuple(right_b[:2].astype(np.int32)), 3, 128)
        cv2.imshow("test", self.grid)
        cv2.waitKey(0)
Exemplo n.º 16
0
def save_image_as(pix, add, name):
	import cv2, time
	from PIL import ImageGrab, Image, ImageDraw, ImageOps
	pix2 = pix.copy()  ## We make a copy of the background image
	cv2.drawContours(pix2,add,-1,(0,255,0),3)
	im = Image.fromarray(pix2)
	im.save("Images/" + name + ".png")
Exemplo n.º 17
0
def thresh_callback(thresh):
    p = cv2.getTrackbarPos('perim','input')
    print thresh, p
    edges = cv2.Canny(blur,thresh,thresh*2)
    drawing = np.zeros(img.shape,np.uint8)     # Image to draw the contours
    contours,hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    block = False
    for i,cnt in enumerate(contours):
        color = np.random.randint(0,255,(3)).tolist()  # Select a random color   
        perim = cv2.arcLength(cnt, True)
        #print perim
        if perim > p and not block:
            x,y,w,h = cv2.boundingRect(cnt)
            print x,y, w, h
            cv2.drawContours(drawing,[cnt],0,color,2)
            cv2.imshow('output',drawing)
            #with open('im_rect.jpg', 'w'):
            # plt.imshow(img)
            block = True
            subpic = img[y:y+h,x:x+w]
            # fgbg = cv2.BackgroundSubstractorMOG()
            #fgmask = fgbg.apply(subpic)
            #cv2.bitwise_and(subpic, subpic, mask=fgmask)
            print subpic[10,10]
            img[img==subpic[10,10]]=255
            cv2.imwrite('rect_bottom.png', subpic)
   
    cv2.imshow('input',img)
Exemplo n.º 18
0
 def run(self):
   while True:
     f, orig_img = self.capture.read()
     orig_img = cv2.flip(orig_img, 1)
     img = cv2.GaussianBlur(orig_img, (5,5), 0)
     img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2HSV)
     img = cv2.resize(img, (len(orig_img[0]) / self.scale_down, len(orig_img) / self.scale_down))
     red_lower = np.array([0, 150, 0],np.uint8)
     red_upper = np.array([5, 255, 255],np.uint8)
     red_binary = cv2.inRange(img, red_lower, red_upper)
     dilation = np.ones((15, 15), "uint8")
     red_binary = cv2.dilate(red_binary, dilation)
     contours, hierarchy = cv2.findContours(red_binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
     max_area = 0
     largest_contour = None
     for idx, contour in enumerate(contours):
       area = cv2.contourArea(contour)
       if area > max_area:
         max_area = area
         largest_contour = contour
     if not largest_contour == None:
       moment = cv2.moments(largest_contour)
       if moment["m00"] > 1000 / self.scale_down:
         rect = cv2.minAreaRect(largest_contour)
         rect = ((rect[0][0] * self.scale_down, rect[0][1] * self.scale_down), (rect[1][0] * self.scale_down, rect[1][1] * self.scale_down), rect[2])
         box = cv2.cv.BoxPoints(rect)
         box = np.int0(box)
         cv2.drawContours(orig_img,[box], 0, (0, 0, 255), 2)
         cv2.imshow("ColourTrackerWindow", orig_img)
         if cv2.waitKey(20) == 27:
           cv2.destroyWindow("ColourTrackerWindow")
           self.capture.release()
           break
Exemplo n.º 19
0
def cartonify_image(image):
    """
    convert an inpuy image to a cartoon-like image
    Args:
       image: input PIL image

    Returns:
        out (numpy.ndarray): A grasycale or color image of dtype uint8, with
                             the shape of image
    """

    output = np.array(image)
    x, y, c = output.shape

    # noise removal while keeping edges sharp
    for i in xrange(c):
        output[:, :, i] = cv2.bilateralFilter(output[:, :, i], 5, 50, 50)

    #edges in an image using the Canny algorithm
    edge = cv2.Canny(output, 100, 200)
    #convert image into RGB color space
    output = cv2.cvtColor(output, cv2.COLOR_RGB2HSV)

    #historygram array
    hists = []

    #Compute the histogram of a set of data.
    #H
    hist, _ = np.histogram(output[:, :, 0], bins=np.arange(180+1))
    hists.append(hist)
    #S
    hist, _ = np.histogram(output[:, :, 1], bins=np.arange(256+1))
    hists.append(hist)
    #V
    hist, _ = np.histogram(output[:, :, 2], bins=np.arange(256+1))
    hists.append(hist)

    centroids = []
    for h in hists:
        centroids.append(kmeans_histogram(h))
    print("centroids: {0}".format(centroids))

    output = output.reshape((-1, c))
    for i in xrange(c):
        channel = output[:, i]
        index = np.argmin(np.abs(channel[:, np.newaxis] - centroids[i]), axis=1)
        output[:, i] = centroids[i][index]
    output = output.reshape((x, y, c))
    output = cv2.cvtColor(output, cv2.COLOR_HSV2RGB)

    # Retrieves contours from the binary image
    # RETR_EXTERNAL: retrieves only the extreme outer contours
    # CHAIN_APPROX_NONE= stores absolutely all the contour points
    contours, _ = cv2.findContours(edge,
                                   cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_NONE)

    # Draws contours outlines
    cv2.drawContours(output, contours, -1, 0, thickness=1)
    return output
Exemplo n.º 20
0
def maskcolor(path,_hsv_mask):
	if path=='0':
		path=0;
	cap = cv2.VideoCapture(path)

	while(cap.isOpened()):
		_, frame = cap.read()
		hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

		# Threshold the HSV image to get only blue colors
		mask = cv2.inRange(hsv,_hsv_mask[0],_hsv_mask[1]);

		# Bitwise-AND mask and original image
		_res = cv2.bitwise_and(frame,frame, mask= mask)
		_gray = cv2.cvtColor(_res,cv2.COLOR_RGB2GRAY)
		_edge = cv2.adaptiveThreshold(_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
		cnt,hchy = cv2.findContours(_edge,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
		print hchy
		cv2.drawContours(frame, cnt, -1, (0,255,0), 3)
		#x,y,w,h = cv2.boundingRect(np.vstack(cnt))
		#cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)		
		#rect = cv2.minAreaRect(np.vstack(cnt))
		#box = cv2.cv.BoxPoints(rect)
		#box = np.int0(box)
		#cv2.drawContours(frame,[box],0,(0,0,255),2)
		cv2.imshow('a',frame)
		#cv2.imshow('b',_edge)
		#cv2.imshow('c',res)
		#plt.show()

		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
	cap.release()
	cv2.destroyAllWindows()
Exemplo n.º 21
0
 def fill_ratio(self, mat, contour, threshed):
     fill_mask = np.zeros(mat.shape[:2], dtype=np.uint8)
     cv2.drawContours(fill_mask, [contour], -1, 255, thickness=-1)
     fill_masked = cv2.bitwise_and(threshed, threshed, mask=fill_mask)
     hull_area = cv2.contourArea(cv2.convexHull(contour))
     fill = np.sum(fill_masked) / 255 / hull_area
     return fill
Exemplo n.º 22
0
    def updateContours(self, frame, contours):

        self.movingObjects = []
        self.histograms = []
        
        height, width = frame.shape[:2]
        
        i=0
        for contour in contours:
            c,r,w,h = cv2.boundingRect(contour)
            if w*h < 20:
				continue
			# set up the ROI for tracking
            roi = frame[r:r+h, c:c+w]
            hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            
            mask2 = cv2.inRange(hsv_roi, np.array((0., 30.,30.)), np.array((180.,250.,250.)))
            mask = np.zeros((height,width), np.uint8)
            cv2.drawContours(mask, [contour], 0, 255, -1)
            maskArea = mask[r:r+h,c:c+w]
            maskArea = cv2.bitwise_and(maskArea, mask2)
            img = cv2.bitwise_and(roi,roi,mask=maskArea)
            #cv2.imshow('maskarea', mask2)
            roi_hist = cv2.calcHist([hsv_roi],[0,1],maskArea,[180,256],[0,180,0,256])
            #roi_hist = cv2.calcHist([hsv_roi],[0],maskArea,[180],[0,180])
            cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
            #print i
            i+=1
            self.movingObjects.append((c,r,w,h))
            self.histograms.append(roi_hist)
Exemplo n.º 23
0
def get_centroids (contours, frame):
	centres = []
	if contours:
		for i in range(len(contours)):
			moments = cv2.moments(contours[i])
			centres.append((int(moments['m10']/moments['m00']), int(moments['m01']/moments['m00'])))
		
			if i>0:                
				dist = calculateDistance(centres[i-1][0],centres[i-1][1],centres[i][0],centres[i][1])
				area=cv2.contourArea(contours[i])
				prevarea=cv2.contourArea(contours[i-1])
				if dist < 120:                    
					if area > prevarea:
						rect = cv2.minAreaRect(contours[i])
						box = cv2.boxPoints(rect)
						box = np.int0(box)
						print(box)
						frame = cv2.drawContours(frame,[box],0,(0,0,255),2)
					else :
						rect = cv2.minAreaRect(contours[i-1])
						box = cv2.boxPoints(rect)
						box = np.int0(box)
						print(box)
						frame = cv2.drawContours(frame,[box],0,(0,0,255),2)
			else:
 	
				rect = cv2.minAreaRect(contours[i])
				box = cv2.boxPoints(rect)
				box = np.int0(box)
				frame = cv2.drawContours(frame,[box],0,(0,0,255),2)
				print(box)
	return centres, frame
Exemplo n.º 24
0
	def getLetter(self):
		if self.thresh is None:
			print "no thresh for getLetter"
			return 0
		contours, hierarchy = cv2.findContours( self.thresh.copy() ,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
		
		# sort contours by cnt area / img area ratio from max to min
		contours = sorted(contours, key = lambda x: cv2.contourArea(x)/self.thresh.size, reverse = False)
		
		
		for cnt in contours:
			area = cv2.contourArea(cnt)
			
			# area calculation
			(x,y),radius = cv2.minEnclosingCircle(cnt)
			radius = int(radius)
			minArea = 3.14159*radius*radius*0.6
			maxArea = 3.14159*radius*radius*1.4
			
			# if actual area is close enough to 2*pi*r area && area > 10% of pic
			if area >= minArea and area <= maxArea and area > self.thresh.size*0.1:

				# copy
				thresh = cv2.bitwise_not(self.thresh)
				mask = np.zeros_like(thresh) 				# Create mask where white is what we want, black otherwise
				cv2.drawContours(mask, [cnt], 0, 255, -1) 	# Draw filled contour in mask
				self.letter = np.zeros_like(thresh) 				# Extract out the object and place into output image
				self.letter[mask == 255] = thresh[mask == 255]
Exemplo n.º 25
0
def imclearborder(imgBW, radius):

    # Given a black and white image, first find all of its contours
    imgBWcopy = imgBW.copy()
    contours = cv2.findContours(imgBWcopy.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]

    # Get dimensions of image
    imgRows = imgBW.shape[0]
    imgCols = imgBW.shape[1]

    contourList = []  # ID list of contours that touch the border

    # For each contour...
    for idx in np.arange(len(contours)):
        # Get the i'th contour
        cnt = contours[idx]

        # Look at each point in the contour
        for pt in cnt:
            rowCnt = pt[0][1]
            colCnt = pt[0][0]

            check1 = (rowCnt >= 0 and rowCnt < radius) or (rowCnt >= imgRows - 1 - radius and rowCnt < imgRows)
            check2 = (colCnt >= 0 and colCnt < radius) or (colCnt >= imgCols - 1 - radius and colCnt < imgCols)

            if check1 or check2:
                contourList.append(idx)
                break

    for idx in contourList:
        cv2.drawContours(imgBWcopy, contours, idx, (0, 0, 0), -1)
        print "deleted ROI"

    return imgBWcopy
Exemplo n.º 26
0
 def contourMidpoint(self, imageIn):
     contours, hierarchy = cv2.findContours(imageIn, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
     bestContour = None
     bestArea = 0
     result = None
     for contour in contours:
         area = cv2.contourArea(contour)
         if area > bestArea:
             bestContour = contour
             bestArea = area
     if bestContour != None:
         # print bestArea
         l = bestContour[bestContour[:,:,0].argmin()][0][0]
         r = bestContour[bestContour[:,:,0].argmax()][0][0]
         t = bestContour[bestContour[:,:,1].argmin()][0][1]
         b = bestContour[bestContour[:,:,1].argmax()][0][1]
         # print l, r, t, b
         cx = (l + r) / 2
         cy = (t + b) / 2
         if self.debug:
             imageContour = np.zeros(self.imageHSV.shape, np.uint8)
             cv2.drawContours(imageContour, [bestContour], 0, (0, 255, 0), 1)
             cv2.circle(imageContour, (cx, cy), 1, (0, 0, 255), -1)
             cv2.imshow(self.windowContour, imageContour)
         result = (cx, cy, bestArea)
     return result
Exemplo n.º 27
0
def get_motions(f, fMask, thickness=1, color=(170, 170, 170)):
    '''
    Iterates over the contours in a mask and draws a bounding box
    around the ones that encompas an area greater than a threshold.
    This will return an image of just the draw bock (black bg), and
    also an array of the box points.
    '''
    rects_mot = []
    f_rects = np.zeros(f.shape, np.uint8)
    # get contours
    if imutils.is_cv3():
        _, cnts, hierarchy = cv2.findContours(
            fMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    elif imutils.is_cv2():
        cnts, hierarchy = cv2.findContours(
            fMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # loop over the contours
    for c in cnts:
        # if the contour is too small, ignore it
        if cv2.contourArea(c) < contourThresh:
            continue

        if imutils.is_cv3():
            box = cv2.boxPoints(cv2.minAreaRect(c))
        elif imutils.is_cv2():
            box = cv2.cv.BoxPoints(cv2.minAreaRect(c))

        box = np.int0(box)
        cv2.drawContours(f_rects, [box], 0, color, thickness)
        rects_mot.append(cv2.boundingRect(c))
    return f_rects, rects_mot
Exemplo n.º 28
0
 def convexhull(self, gray):
     mask = np.zeros(gray.shape, dtype=np.uint8)
     cnt, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     for c in cnt:
         hull = cv2.convexHull(c)
         cv2.drawContours(mask, [hull], -1, 255, -1)
     return mask
Exemplo n.º 29
0
def contours(img):
	#img = blurred(img)
	#img = cv2.erode(hsvthreshold(img), cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))
	img = hsvthreshold(img)
	ret = cv2.merge([img, img, img])
	cons, hier = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
	targets = []

	#cv2.drawContours(ret, np.array(cons), -1, np.array([255.0, 255.0, 0.0]), -1)
	for i in range(len(cons)):
		if cv2.contourArea(cons[i]) >= 1000:
			#print len(cons[i])
			#print len(cv2.convexHull(cons[i]))
			#cons[i] = cv2.convexHull(cons[i])
			cv2.drawContours(ret, np.array(cons), i, np.array([255.0, 0.0, 0.0]), 1)
			pts = []
			for pt in cons[i]:
				pts.append(pt[0])
				#ret[pt[0][1]][pt[0][0]] = np.array([255.0, 0.0, 0.0])
			
			corn = corners(pts, ret)
			if len(corn) == 4:
				rvec, tvec = cv2.solvePnP(target,np.array(corn[:]),cam,dis)
				print np.linalg.norm(tvec)
				#target = pts[:,1].astype(np.double).sum() / len(pts)
				for x, y in corn:
					cv2.circle(ret, (int(x), int(y)), 5, np.array([0.0, 0.0, 255.0]), -1)
	
	return ret
Exemplo n.º 30
0
 def updateBiggestObjectContour(self, frame, contour):
     
     if contour == None:
         return
         
     height, width = frame.shape[:2]
     
     c,r,w,h = cv2.boundingRect(contour)
     
     if w*h < 20:
         return
     # set up the ROI for tracking
     roi = frame[r:r+h, c:c+w]
     hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
     
     mask2 = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
     mask = np.zeros((height,width), np.uint8)
     cv2.drawContours(mask, [contour], 0, 255, -1)
     maskArea = mask[r:r+h,c:c+w]
     maskArea = cv2.bitwise_and(maskArea, mask2)
     img = cv2.bitwise_and(roi,roi,mask=maskArea)
     #cv2.imshow('maskarea', mask2)
     #roi_hist = cv2.calcHist([hsv_roi],[0,1,2],maskArea,[180,256,256],[0,180,0,256,0,256])
     roi_hist = cv2.calcHist([hsv_roi],[0,1],maskArea,[180,256],[0,180,0,256])
     #roi_hist = cv2.calcHist([hsv_roi],[0],maskArea,[180],[0,180])
     cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
     #print i
     self.biggestObject = (c,r,w,h)
     self.biggestObjectHistogram = roi_hist
Exemplo n.º 31
0
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
print("STEP 3: 灰度图像求阈值")
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)

#4.检测、计数和绘制轮廓
# 在图像中找到前景物体的轮廓
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
output = image.copy()
# 循环绘制轮廓
for c in cnts:
    # 以紫色线条绘制轮廓
    # 一次显示一个物体的轮廓
    cv2.drawContours(output, [c], -1, (240, 0, 159), 3)
    print("STEP 4: 绘制轮廓")
    cv2.imshow("Contours", output)
    cv2.waitKey(0)
# 注明紫色轮廓的个数
text = "I found {} objects!".format(len(cnts))
cv2.putText(output, text, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX, 0.7,
    (240, 0, 159), 2)
print("STEP 4: 标注轮廓数目")
cv2.imshow("Contours", output)
cv2.waitKey(0)

#5、腐蚀和膨胀
#通过腐蚀减小前景物体的尺寸,利用cv2.erode将轮廓尺寸减小5
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=5)
line  -- corner

fit an poly  wrong

x+y??

shape matching ??
"""


rect=cv2.minAreaRect(cnt)
box=cv2.boxPoints(rect)
box=np.int0(box)
box_img=np.copy(img)

cv2.drawContours(box_img,[box],0,(0,0,255),3)
cv2.imwrite('box.jpg',box_img)
cnt_img=np.copy(img)
cv2.drawContours(cnt_img,contours,chosen_index,(0,255,0),3)
cv2.imwrite('cnt.jpg',cnt_img)

"""
cam=PiCamera()
for i in range(5):
    cam.start_preview()
    time.sleep(2)
    cam.capture('test'+str(i)+'.jpg')
    cam.stop_preview()
    time.sleep(2)
"""
    
Exemplo n.º 33
0
def processPoints(im, debug):

    # Convert to Grayscale
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

    # Threshold the image with adaptative thresholding
    thresh = cv2.adaptiveThreshold(imgray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY, 15, 2)

    # Find image contours
    (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_SIMPLE)

    # Output of "good" contours
    allcnts = []

    # Auxiliarry variables to help understand what is being processed (total points)
    totalP = 0

    # loop over our contours
    for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True)

        # If perimeter is too low, discart contour
        if peri < 100:
            continue

        # Try approximating poligon to simplify number of points
        approx = cv2.approxPolyDP(c, 6, True)

        # Sum total points processed
        totalP = totalP + len(approx)

        # Append contour to "good contours"
        allcnts.append(approx)

    # Debug total points
    print("Frame total points: " + str(totalP))

    # Debug: Show image contours
    if debug:
        if onlyLines:
            out = im.copy() * 0
            cv2.drawContours(out, allcnts, -1, (0, 255, 0), 1)
        else:
            out = im

        # Debug: Show image
        cv2.imshow("thres", thresh)
        cv2.imshow("output", out)

    # Stored list of x y points in series
    points = []

    # Loop over selected contours and append everything to a single array of x, y, x, y, x, y... coordinates
    # points.append(1)
    # points.append(1)

    # points.append(1)
    # points.append(255)

    # points.append(255)
    # points.append(255)

    # points.append(255)
    # points.append(1)

    # points.append(1)
    # points.append(1)

    for contour in allcnts:
        for p in contour:
            x = p[0][0] * 1.0
            y = p[0][1] * 1.0
            xOut = int(math.floor(x / im.shape[0] * 255))
            yOut = int(math.floor(y / im.shape[1] * 255))
            points.append(xOut)
            points.append(yOut)
            points.append(0)

        points.append(0)
        points.append(0)
        points.append(0)

    # Return points
    return points
Exemplo n.º 34
0
def draw_contours_on_image_like(contours, img_orig):
    img_blank = np.ones_like(img_orig) * 255
    cv2.drawContours(img_blank, contours, -1, (0, 255, 0), 1)
    return img_blank
Exemplo n.º 35
0
cv2.imshow(window_name, binary_pokemon_image)
cv2.waitKey()

# initialize the outline image
outline_image = np.zeros(pokemon_resized_image.shape, dtype="uint8")

# find the outermost contours (the outline) of the pokemone
contours = cv2.findContours(binary_pokemon_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# parsing the contours for various versions of OpenCV.
contours = imutils.grab_contours(contours)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[0]
print(contours.shape)
# sort the contours based on their area (in descending order) and keeping only the largest contour and discard the others.

# draw contours
cv2.drawContours(outline_image, [contours], -1, 255, -1)
# show outline image
window_name = 'Outline Image'
cv2.imshow(window_name, outline_image)
cv2.waitKey()

# compute Zernike moments to characterize the shape of pokemon outline
zernikeMoments = ZernikeMoments(21)
pokemonFeatures = zernikeMoments.describe(outline_image)

# perform the search to identify the pokemon
searcher = Searcher(index)
pokemon_name = searcher.search(pokemonFeatures)
print("That pokemon is: %s" % pokemon_name[0][1].upper())

# show image Comparing
Exemplo n.º 36
0
        fore = fgbg.apply(frame, learningRate=0)
    if backgroundFrame == 1:

        print startTime
    back = fgbg.getBackgroundImage()
    kernel = np.ones((3, 3), np.uint8)
    fore = cv2.erode(fore, kernel, iterations=1)
    fore = cv2.dilate(fore, kernel, iterations=5)
    _, contours, _ = cv2.findContours(fore, cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_NONE)
    for contour in contours:
        if cv2.contourArea(contour) > 8500:
            tcontours = []
            hull = []
            tcontours.append(contour)
            cv2.drawContours(frame, contour, -1, (0, 0, 255), 2)
            hull.insert(0, cv2.convexHull(tcontours[0], clockwise=False))
            cv2.drawContours(frame, hull, -1, (0, 255, 0), 2)
            x, y, w, h = cv2.boundingRect(tcontours[0])
            #x1, y1, w1, h1 = cv2.boundingRect(hull[0])
            cv2.rectangle(frame, (x, y - 20), (x + 213, y + 193), (0, 255, 0),
                          2, 8, 0)
            #cv2.rectangle(frame,(x1,y1),(x1+w1,y1+h1), (255, 255, 0), 2, 8, 0)
    if backgroundFrame > 0:
        cv2.putText(fore, "Recording Background", (30, 30),
                    cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (200, 200, 250), 1,
                    cv2.LINE_AA)
    else:
        diff = time.time() - startTime
        int_diff = int(diff)
        cv2.putText(fore, "Time:" + str(int_diff), (30, 30),
Exemplo n.º 37
0
def doloop():
    global min_distance, num_im, dx, dy, dpx, dpy
    while True:
        # Get a fresh frame
        rgb = get_video()
        depth = measure_depth()
        depthimage = np.dstack((depth, depth, depth)).astype(np.uint8)

        # remove the background
        if not os.path.exists("floor.npy"):
            measure_background()
        floor = np.load("floor.npy")
        depth = remove_background(depth, floor)

        cutimage = np.dstack((depth, depth, depth)).astype(np.uint8)

        #Find contour
        gray = cv2.cvtColor(cutimage, cv2.COLOR_BGR2GRAY)
        _, thresholded = cv2.threshold(gray, min_distance, 255,
                                       cv2.THRESH_BINARY_INV)

        try:
            contours, hierarchies = cv2.findContours(thresholded,
                                                     cv2.RETR_EXTERNAL,
                                                     cv2.CHAIN_APPROX_NONE)
            contour = max(contours, key=cv2.contourArea)
            cv2.drawContours(depthimage, contour, -1, (0, 0, 255), 2)

            outline = np.copy(contour[:, 0, :])
            tck, u = interpolate.splprep(outline.transpose(), s=0)
            du = 1 / (num_points - 1)
            unew = np.arange(0, 1.0, du)
            out = interpolate.splev(unew, tck)

            outline[1, :] = outline[1, :] * dpy
            outline[1, :] = outline[1, :] + dy
            outline[0, :] = outline[0, :] * dpx
            outline[0, :] = outline[0, :] + dx

            outline = outline.transpose().reshape((-1, 1, 2))

            cv2.drawContours(rgb, [outline.astype(int)], -1, (0, 0, 255), 2)

            mask = np.zeros_like(rgb)
            cv2.drawContours(mask, [outline.astype(int)], -1, (255, 255, 255),
                             -1)
            cropped = np.zeros_like(rgb)
            cropped[mask == 255] = rgb[mask == 255]

            da = np.hstack((depthimage, rgb))
            db = np.hstack((mask, cropped))
            da = np.vstack((da, db))
        except Exception as error:
            #raise error
            da = np.hstack((depthimage, rgb))

        # Simple Downsample
        cv2.imshow('both', np.array(da))
        #cv2.imshow('both',np.array(da[::2,::2,::-1]))

        key = cv2.waitKey(5)
        if chr(key & 255) == ' ':  #space
            print('Writing contour')
            outline = outline.reshape((-1, 2))
            outline[:, 1:] = 480 - outline[:, 1:]
            #outline[:,0] = 640-outline[:,0]
            np.savetxt("scf{}-outline-coords.dat".format(num_im),
                       outline,
                       fmt='%i %i')
            cv2.imwrite("scf{}-depthimage.png".format(num_im), depthimage)
            cv2.imwrite("scf{}-colorimage.png".format(num_im),
                        cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
            cv2.imwrite("scf{}-fullcolorimage.png".format(num_im),
                        cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR))
            cv2.imwrite("scf{}-blackandwhiteimage.png".format(num_im),
                        cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY))
            num_im += 1
        elif chr(key & 255) == 'b':
            measure_background()
        elif chr(key & 255) == 'w':
            dy -= 5
        elif chr(key & 255) == 's':
            dy += 5
        elif chr(key & 255) == 'a':
            dx -= 5
        elif chr(key & 255) == 'd':
            dx += 5
        elif chr(key & 255) == 'r':
            dpy *= 1.02
        elif chr(key & 255) == 'f':
            dpy /= 1.02
        elif chr(key & 255) == 't':
            dpx *= 1.02
        elif chr(key & 255) == 'g':
            dpx /= 1.02
        elif key == 65362:  #up on linux
            min_distance += 10
            print("threshold ", min_distance)
        elif key == 65364:  #down on linux
            min_distance -= 10
            print("threshold ", min_distance)
        elif key == -1:  #none
            continue
        else:
            print(key)
        print(dx, dy, dpx, dpy)
Exemplo n.º 38
0
def rysowanie(frame,x,y,contour_max,color,yy):
    cv2.putText(frame,'B: x = {0} y = {1}'.format(x,y),(20,yy), cv2.FONT_HERSHEY_SIMPLEX, 1,color,2)
    cv2.circle(frame,(x,y), 3, (255,0,255), -1)
    cv2.drawContours(frame,[contour_max], 0, (255,0,255), 2)
Exemplo n.º 39
0
def main(table):
    camSrv = CameraServer.getInstance()
    camSrv.enableLogging()

    cam = UsbCamera("logitech", 0)
    camSrv.addCamera(cam)
    #cam = cs.startAutomaticCapture()
    
    cam.setResolution(CAM_WIDTH, CAM_HEIGHT)
    
    # Get a CvSink. This will capture images from the camera
    cvSink = camSrv.getVideo() #camera=cam)
    
    # (optional) Setup a CvSource. This will send images back to the Dashboard
    outputStream = camSrv.putVideo("Rectangle", CAM_WIDTH, CAM_HEIGHT)
    
    # Allocating new images is very expensive, always try to preallocate
    rawimg = np.zeros(shape=(CAM_HEIGHT, CAM_WIDTH, 3), dtype=np.uint8)
    # OpenCV ranges
    # Hue: 0 - 180
    # Saturation: 0 - 255
    # Vibrancy: 0 - 255
    lower = np.array([0, 0, 200])
    upper = np.array([10, 100, 255])

    while True:
        # Tell the CvSink to grab a frame from the camera and put it
        # in the source image.  If there is an error notify the output.
        #print('grab a frame...')
        time, rawimg = cvSink.grabFrame(rawimg,0.5)
        if time == 0:
            # Send the output the error.
            print(cvSink.getError())
            #outputStream.notifyError(cvSink.getError())
            # skip the rest of the current iteration
            continue

        # convert RGB to HSV
        hsv = cv2.cvtColor(rawimg, cv2.COLOR_RGB2HSV)

        # Threshold the HSV image to get only the selected colors
        mask = cv2.inRange(hsv, lower, upper)
        
        mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        if len(contours) == 0:
            print('no contours')
            continue
        try:
            contours.sort(key=lambda x: cv2.contourArea(x))
            target = max(contours, key=cv2.contourArea)
            boundingBox = cv2.minAreaRect(target)
        except:
            print('no bounding box')
            continue

        angle = boundingBox[2]
        if angle < -45:
            angle = angle + 90
            
        table.putNumber('cameraAngle', angle)

        # draw a red outline on the output image
        # so that the user can see what is targeted
        boxPoints = cv2.boxPoints(boundingBox)
        boxPoints = np.int0(boxPoints)
        rawimg = cv2.drawContours(rawimg, [boxPoints], 0, (0,0,255), 2)
        # Give the output stream a new image to display
        outputStream.putFrame(rawimg)    
Exemplo n.º 40
0
 image = imstack[:,:,1]
 gray = cv2.GaussianBlur(image, (3, 3), 0)
 edged = cv2.Canny(gray, 20, 100)
 im_shape = images_d.shape
 
 # find contours in the edge map
 cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                             cv2.CHAIN_APPROX_SIMPLE)
 cnts = imutils.grab_contours(cnts)
 
 # ensure at least one contour was found
 if len(cnts) > 0:
 	# grab the largest contour, then draw a mask for the well
 	c = max(cnts, key=cv2.contourArea)
 	mask = np.zeros(gray.shape, dtype="uint8")
 	cv2.drawContours(mask, [c], -1, 255, -1)
  
 	# compute its bounding box of well, then extract the ROI,
 	# and apply the mask
 	(x, y, w, h) = cv2.boundingRect(c)
     # rect_mask = np.zeros(image.shape)
     # rect_mask[y:y + h, x:x + w]=1
 	imageROI = image[y:y + h, x:x + w]
 	# maskROI = mask[y:y + h, x:x + w]
 	# imageROI1= cv2.bitwise_and(imageROI, imageROI, mask=maskROI)
     
 image_rotated = image.copy()
     
 for angle in angles:
         rotated = ndimage.rotate(imageROI , angle, reshape=False, cval=255)
         image_rotated[y:y + h, x:x + w] = rotated
Exemplo n.º 41
0
    # 2 形态学闭操作: 先膨胀再腐蚀
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    img_bgr_morph = cv.morphologyEx(img_bgr_copy, cv.MORPH_CLOSE, kernel)
    img_gray = cv.cvtColor(img_bgr_morph, cv.COLOR_BGR2GRAY)  # 转为 灰度图

    # cv.namedWindow("track_bar")
    # cv.createTrackbar("threshold", "track_bar", 0, 255, on_change)
    ret, img_bi = cv.threshold(img_gray, 10, 255, cv.THRESH_BINARY)
    cv.imshow("img_bi", img_bi)

    # 3 查找轮廓
    _, contours, hierarchy = cv.findContours(img_bi, cv.RETR_EXTERNAL,
                                             cv.CHAIN_APPROX_SIMPLE)
    print "找到轮廓的个数: ", len(contours)
    cv.drawContours(img_bgr_copy, contours, -1, (0, 0, 255))
    cv.imshow("img_bgr_copy3", img_bgr_copy)

    # 4 多边形逼 近
    for i, contour in enumerate(contours):
        # 获取矩形区域
        x, y, w, h = cv.boundingRect(contour)

        # 计算面积
        area = cv.contourArea(contour)
        print area

        # 计算周长
        arc = cv.arcLength(contour, True)

        epsilon = arc * 0.02  # 逼近程度 阈值
Exemplo n.º 42
0
# STEP 2: FINDING CONTOURS
# assumption - largest contour in the image with exactly four points is the piece of paper to be scanned
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
for c in cnts:
    # approximate the contour
    p = cv2.arcLength(c, True)  #contour perimeter
    approx = cv2.approxPolyDP(c, 0.02*p, True)
    if len(approx) == 4:
        screenContour = approx
        break
# show the outline
print("Finding Contours")
cv2.drawContours(image, [screenContour], -1, (0,255,0), 2) # pass -1 to draw all the contours
cv2.imshow("outline", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# STEP # - APPLY PERSPECTIVE TRANSFORM AND THRESHOLD
# apply 4 point transform to obtain top-down view of the orginal Image
warped = four_point_transform(orig, screenContour.reshape(4,2)*ratio)
# convert warped image to grayscale and then threshold it to give it 'black and white paper' look
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped, 11, offset=10, method = "gaussian")
warped = (warped > T).astype("uint8")*255

print("Apply PERSPECTIVE TRANSFORM")
cv2.imshow("Original", imutils.resize(orig, height=650))
cv2.imshow("Scanned",imutils.resize(warped,height=650))
Exemplo n.º 43
0
            segmentation = [[int(x) for x in cnt.ravel().tolist()]]
            x,y,w,h = cv2.boundingRect(cnt)

            annotations.append({
                'segmentation': segmentation,
                'area': area,
                'bbox': [int(x), int(y), int(w), int(h)],
                'iscrowd': 0,
                'id': len(annotations)+1,
                'image_id': 1,
                'category_id': category_id
            })

        for ann in annotations:
            color = tuple ([int(x*255) for x in np.random.rand(3)])
            cv2.drawContours(bg,[np.array(ann['segmentation']).reshape(-1,1,2)],0,color,-1)

        name = str(uuid.uuid1()).split('-')[0]
        all_coco = {'info': {'year': 2021,
        'version': '1.0',
        'description': 'build with dataset_builder',
        'contributor': 'MiXaiLL76',
        'url': 'https://t.me/mixaill76',
        'date_created': '2021-01-06 16:22:20'},
        'images': [{
                "id": 1,
                "width": W,
                "height": H,
                "file_name": f"{name}.png",
                "license": 1,
                "date_captured": "",
Exemplo n.º 44
0
        largest_area = cv2.contourArea(contours[i])
        largest_area_index = i

M = cv2.moments(contours[largest_area_index])
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

# print(cX,cY)
# print(asli.shape)
sample = np.zeros((20, 20, 3), dtype=np.uint8)
x = 0
for i in range(20):
    for n in range(20):
        sample[i, n] = (asli[cY - 10 + n, cX - 10 + i])

cv2.drawContours(image, contours[largest_area_index], -1, 255, 3)
cv2.rectangle(image, (cX - 11, cY - 11), (cX + 11, cY + 11), (255, 255, 255),
              1)
cv2.circle(image, (cX, cY), 1, (255, 255, 255), -1)
cv2.putText(image, "Titik Sample", (cX - 20, cY - 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

gray_sample = cv2.cvtColor(sample, cv2.COLOR_BGR2GRAY)
# sample = cv2.GaussianBlur(sample,(3,3),cv2.BORDER_DEFAULT)
print(gray_sample)
print("Avg of red           : ", np.average(sample[:, :, 2]))
print("Avg of green         : ", np.average(sample[:, :, 1]))
print("Avg of blue          : ", np.average(sample[:, :, 0]))
print("Avg Gray value = ", np.average(sample))
hsv_sample = cv2.cvtColor(sample, cv2.COLOR_BGR2HSV)
# print(sample)
 def data(self,noised_maps,ref_map,mask,rotate_angle,mode):
     # Lboxes are the large boxes around doorways for classfication
     # Sboxes are the small boxes around doorways for determination of the number of doors involved in a window
     # divided_masks are the masks of each doorway separately
     Lboxes=[]
     Sboxes=[]
     divided_masks=[]
     _,contours,_ = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
     for cnt in contours:
         # separate the masks based on their contours
         canvas = np.zeros_like(mask)
         cv2.drawContours(canvas,[cnt], 0, 255, -1)
         divided_masks.append(canvas)
         rect = cv2.minAreaRect(cnt)
         box = cv2.boxPoints(rect)  
         box = np.int0(np.round(box))
         # adjust the large box according to the rotated angle
         tuned_x_gap = round(self.x_gap/self.resolution*(1-utils.normfun(rotate_angle%90,24)*33)).astype(np.int)
         large_box = utils.calc_box(box,tuned_x_gap,int(self.y_gap/self.resolution),rotate_angle,self.center) 
         Lboxes.append(large_box)
         small_box = utils.calc_box(box,1,1,rotate_angle,self.center)
         Sboxes.append(small_box)
     Lboxes = np.array(Lboxes)
     Sboxes = np.array(Sboxes) 
     # rotate the divided masks with the given angle
     rotate_kernel = cv2.getRotationMatrix2D(tuple(self.center),rotate_angle,1)
     rotate_masks=[]
     for mask in divided_masks:
         rotate_mask = cv2.warpAffine(mask,rotate_kernel,self.img_size,borderValue=0)
         rotate_masks.append(rotate_mask)
       
     for x in range(0,self.img_size[0],self.stride):
         if (x+self.window_size)<=self.img_size[0]:
             for y in range(0,self.img_size[1],self.stride):
                 if (y+self.window_size)<=self.img_size[1]:
                     window = np.array([x,y,x+self.window_size-1,y+self.window_size-1])
                     # reious is regarded as the threshold for classification
                     reious = utils.overlapp(window,Lboxes)
                     if mode ==0:
                         # extract samples of background
                         if (reious<=self.thres_bg).all(): 
                             bk = ref_map[x:x+self.window_size,y:y+self.window_size]
                             if np.sum(bk==self.colors['gray'])<3600:
                                 # calculate the selection rate according to the variance and the proportion of gray pixels
                                 var = np.var(bk)
                                 denominator = 40-utils.sigmoid(var/3200)*12+30*np.sum((bk>120)*(bk<130))/(self.window_size**2)
                                 if np.random.randint(denominator)==0:  
                                     # save the extracted samples
                                     for n,nmaps in enumerate(noised_maps):
                                         for l, lmap in enumerate(nmaps):
                                             background = lmap[x:x+self.window_size,y:y+self.window_size]
                                             # background region for cnn
                                             path = self.dir_path[0]+'_'+str(n)+str(l)+'/0/'
                                             utils.mkdir(path)
                                             cv2.imwrite(path+str(self.cnn_count)+'.png',background)
                                     self.cnn_count +=1
                                     # set the upper limit
                                     if self.cnn_count==self.cnn_num:
                                         sys.exit()
                     if mode == 1:
                         # extract samples of doorways
                         if(reious>=self.thres_door).any(): 
                             # calculate the number of doors involved in this window
                             if (rotate_angle%90)<10:
                                 thres =0
                             elif (rotate_angle%90)>80:
                                 thres =0
                             elif (rotate_angle%90)<=45:
                                 thres = (rotate_angle%90)/45*0.5
                             else :
                                 thres = (90-rotate_angle%90)/45*0.5
                             num_door = np.where(utils.overlapp(window,Sboxes)>thres)[0].shape[0]
                             ref = np.where(reious>=self.thres_door)[0].shape[0]
                             # ensure that all the doors involved in this window are completely covered
                             if num_door ==ref:     
                                 if np.random.randint(10)==0:
                                     indices = np.where(reious>=self.thres_door)[0]
                                     # save the extracted samples
                                     for n,nmaps in enumerate(noised_maps):
                                         for l, lmap in enumerate(nmaps):
                                             proxi_area = lmap[x:x+self.window_size,y:y+self.window_size]
                                             mask_area = np.zeros((self.window_size,self.window_size),np.uint8)
                                             for index in indices:
                                                 mask_area = mask_area+rotate_masks[index][x:x+self.window_size,y:y+self.window_size]
                                             # doorway region for u-net
                                             path = self.dir_path[1]+'_'+str(n)+str(l)+'/imgs/'
                                             utils.mkdir(path)
                                             cv2.imwrite(path+str(self.unet_count)+'.png',proxi_area)
                                             # mask of doorway for u-net
                                             path = self.dir_path[1]+'_'+str(n)+str(l)+'/masks/'
                                             utils.mkdir(path)
                                             cv2.imwrite(path+str(self.unet_count)+'.png',mask_area)
                                             # doorway region for cnn
                                             path = self.dir_path[0]+'_'+str(n)+str(l)+'/1/'
                                             utils.mkdir(path)
                                             if self.cnn_count<self.cnn_num:
                                                 cv2.imwrite(path+str(self.cnn_count)+'.png',proxi_area)
                                     self.cnn_count+=1
                                     self.unet_count+=1 
                                     # set the upper limit
                                     if self.unet_count==self.unet_num:
                                         sys.exit()
Exemplo n.º 46
0
# crop1 = img1[y1:y1+h1,x1:x1+w1]

# cv2.imwrite('image2.png',crop)
# cv2.imwrite('image1.png',crop1)

###CONTOURS FOR IMAGE SEGMENTAITON####

ret, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                            cv2.CHAIN_APPROX_SIMPLE)

###First argument img is the source of image
###Second is the countours which should be passed as python list
###Third is index of contours (to draw all contours pass -1)
####remaining are color and thickness

mask2 = cv2.drawContours(thresh, contours, 0, (255, 0, 0), -1)

masked_data = cv2.bitwise_and(img, img, mask=mask2)

b, g, r = cv2.split(img2)
rgba = [b, g, r, thresh]
dst = cv2.merge(rgba, 4)
#idx=1
#print(contours)
#print(type(contours))
# x,y,w,h = cv2.boundingRect(dst)
# roi=dst[y:y+h,x:x+w]
#cv2.imwrite(str(idx) + '.jpg', dst)
cv2.imwrite('image2sfdsf.png', dst)
import numpy as np
import cv2

image = cv2.imread('images/hand.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow('Original Image', image)
cv2.waitKey(0) 

ret, thresh = cv2.threshold(gray, 176, 255, 0)

contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    
n = len(contours) - 1
contours = sorted(contours, key=cv2.contourArea, reverse=False)[:n]

for c in contours:
    hull = cv2.convexHull(c)
    cv2.drawContours(image, [hull], 0, (0, 255, 0), 2)
    cv2.imshow('Convex Hull', image)

cv2.waitKey(0)    
cv2.destroyAllWindows()
Exemplo n.º 48
0
# In[11]:


#Get largest Contour
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
area_thresh = 0
for c in contours:
    area = cv2.contourArea(c)
    if area > area_thresh:
        area_thresh = area
        big_contour = c
        
#Draw 
page = np.zeros_like(img)
cv2.drawContours(page, [big_contour], 0, (255,255,255), -1)

#Get perimeter and ~polygon
peri = cv2.arcLength(big_contour, True)
corners = cv2.approxPolyDP(big_contour, 0.04 * peri, True)

#Draw polygon on input img from detedcted corner
polygon = img.copy()
cv2.polylines(polygon, [corners], True, (0,0,255), 1, cv2.LINE_AA)

print(len(corners))
print(corners)


width = 0.5 * ((corners[0][0][0] - corners[1][0][0]) + (corners[3][0][0] - corners[2][0][0]))
height = 0.5 * ((corners[2][0][1] - corners[1][0][1]) + (corners[3][0][1] - corners[0][0][1]))
Exemplo n.º 49
0
def geometry():
    #extraction info from json
    json_obj = request.get_json(force=True)
    shape_sides = json_obj['shape']
    surface = json_obj['surface']
    color = json_obj['color']

    #getting original image
    big_img = cv2.imread(app.root_path + '/static/pictures/original.jpg', 1)

    #convert from RGB to HSV
    org_img_hsv = cv2.cvtColor(big_img, cv2.COLOR_BGR2HSV)

    #defining color boundaries in HSV
    if color == "G":
        lower_color = np.array([35, 30, 0])
        upper_color = np.array([104, 255, 255])
    if color == "R":
        lower_color = np.array([0, 30, 0])
        upper_color = np.array([30, 255, 255])
        lower_color1 = np.array([150, 30, 0])
        upper_color1 = np.array([180, 255, 255])
    if color == "B":
        lower_color = np.array([109, 30, 0])
        upper_color = np.array([169, 255, 255])

    #creating mask according to specific color boundaries
    mask = cv2.inRange(org_img_hsv, lower_color, upper_color)
    #
    contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)

    #spacial case if color is red because of red's non-continues nature
    if color == "R":
        mask1 = cv2.inRange(org_img_hsv, lower_color1, upper_color1)
        contours1, hierarchy = cv2.findContours(mask1.copy(),
                                                cv2.RETR_EXTERNAL,
                                                cv2.CHAIN_APPROX_SIMPLE)
        for cnt in contours1:
            if cv2.isContourConvex(cnt):
                area = cv2.contourArea(cnt)

                peri = cv2.arcLength(cnt, True)
                approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)

                if (area > float(surface) and len(approx) == int(shape_sides)):
                    cv2.drawContours(big_img, [cnt], -1, (0, 0, 0), 4)

    for cnt in contours:
        if cv2.isContourConvex(cnt):
            area = cv2.contourArea(cnt)

            peri = cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)

            if (area > float(surface) and len(approx) == int(shape_sides)):
                cv2.drawContours(big_img, [cnt], -1, (0, 0, 0), 2)

    randon_hex = secrets.token_hex(8)
    f_ext = '.jpg'
    picture_fn = randon_hex + f_ext

    path = os.path.join(app.root_path, 'static/pictures', picture_fn)
    cv2.imwrite(path, big_img)
    rel_path = '/static/pictures/' + picture_fn
    return jsonify({'result': 'success', 'image_path': rel_path})
Exemplo n.º 50
0
blueLower = np.array([75, 67, 0], dtype="uint8")
blueUpper = np.array([255, 180, 80], dtype="uint8")

camera = cv2.VideoCapture(args['video'])

while True:
    (grabbed, frame) = camera.read()
    if not grabbed:
        break
    blue = cv2.inRange(frame, blueLower, blueUpper)
    blue = cv2.GaussianBlur(blue, (3, 3), 0)

    (cnts, _) = cv2.findContours(blue.copy(), cv2.RETR_EXTERNAL,
                                 cv2.CHAIN_APPROX_SIMPLE)

    if len(cnts) > 0:
        cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
        rect = np.int32(cv2.boxPoints(cv2.minAreaRect(cnt)))

        cv2.drawContours(frame, [rect], -1, (0, 255, 0), 2)

    cv2.imshow("Tracking", frame)
    cv2.imshow("Binary", blue)
    time.sleep(0.025)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
camera.release()
cv2.destroyAllWindows()
Exemplo n.º 51
0
    frame = imutils.resize(frame, width=550)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    subjects = detect(gray, 0)
    for subject in subjects:
        shape = predict(gray, subject)
        shape = face_utils.shape_to_np(shape)
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]
        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)
        ear = (leftEAR + rightEAR) / 2.0
        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)
        cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
        cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

        cv2.putText(frame, "Visuals: {:.2f}".format(ear), (0, 30),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)

        if ear < thresh:
            flag += 1
            #print (flag)
            cv2.putText(frame, "B L I N K", (430, 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (100, 155, 0), 2)

            if flag >= frame_check:
                cv2.putText(frame, "********* WAKE UP SIR ********", (10, 70),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
                # image, "TEXT", (x,y), fontFamily-eg. SIMPLEX / PLAIN, frontScale, (B,G,R), thickness
Exemplo n.º 52
0
    def detect_parking(self,
                       img,
                       distanceBetween2Lines=45,
                       distanceFromTop=175,
                       minLineLength=140,
                       binaryThreshold=230):

        height = img.shape[0]
        width = img.shape[1]

        # img = bird_view(img, False)

        # left cover
        cv2.drawContours(
            img, [np.array([(width // 2, 0), (0, 0),
                            (0, height * 2 // 3)])], 0, (0, 0, 0), -1)
        # right cover
        cv2.drawContours(img, [
            np.array([(width // 2, 0), (width, 0), (width, height * 2 // 3)])
        ], 0, (0, 0, 0), -1)
        # bottom cover
        cv2.drawContours(img, [
            np.array([(0, height * 10 // 11), (width, height * 10 // 11),
                      (width, height), (0, height)])
        ], 0, (0, 0, 0), -1)
        # top cover
        cv2.drawContours(img, [
            np.array([(0, 0), (width, 0), (width, int(height * 0.6)),
                      (0, int(height * 0.6))])
        ], 0, (0, 0, 0), -1)

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        _, thres = cv2.threshold(gray, binaryThreshold, 255, cv2.THRESH_BINARY)

        try:
            rho, theta = self.findLine(thres, minLineLength)
            if rho < 0:
                raise Exception
        except:
            return False, (0, 0), (0, 0)

        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * (a))
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * (a))

        pt1 = (x1, y1)
        pt2 = (x2, y2)

        if (not self.isFirstDetection) and (np.abs(rho - self.lastRho) >
                                            distanceBetween2Lines):
            self.isSecondLine = True
        else:
            self.isFirstDetection = False

        self.lastRho = rho

        if self.isSecondLine and rho > distanceFromTop:
            return True, pt1, pt2
        else:
            return False, pt1, pt2
Exemplo n.º 53
0
            mc[i] = (mu[i]['m10'] / (mu[i]['m00'] + 1e-5),
                     mu[i]['m01'] / (mu[i]['m00'] + 1e-5)
                     )  #-- add 1e-5 to avoid division by zero

            if (flag):
                points3d[i] = list(
                    getXYZfromUVD(int(mc[i][0]), int(mc[i][1]), mid_depth[i]))

                points3d[i][0] = .001 - points3d[i][0]
                points3d[i][1] = 1.0 - points3d[i][1]
                points3d[i][2] = -.45 - points3d[i][2]
                print(points3d[i])

                cv2.circle(RGB_image, (int(mc[i][0]), int(mc[i][1])),
                           int(radius[i]), color, 2)
                cv2.drawContours(RGB_image, contours, i, color, 2)
                cv2.circle(RGB_image, (int(mc[i][0]), int(mc[i][1])), 4, color,
                           -1)

        #depth_message = bridge.cv2_to_imgmsg(Depth_image, encoding="passthrough")
        try:
            None
        except:
            #print("Not Published")
            None

        if cv2.waitKey(1) == 27:
            break  # esc to quit
    cv2.destroyAllWindows()
Exemplo n.º 54
0
    def drumbackend(self):

        pygame.mixer.music.pause()

        lower_red = numpy.array([-10, 100, 100])
        upper_red = numpy.array([10, 255, 255])
        lower_blue = numpy.array([80, 100, 100])
        upper_blue = numpy.array([100, 255, 255])
        frequencybeep = 2500  # Set Frequency To 2500 Hertz
        durationbeep = 50  # Set Duration To 1000 ms == 1 second
        kernelOpen = numpy.ones((10, 10))
        kernelClose = numpy.ones((20, 20))

        cap = cv2.VideoCapture(0)
        cap.set(3, 1920)
        cap.set(4, 1080)

        retval, frame = cap.read()

        runningcam = True

        while runningcam:

            cv2.flip(frame, 1, frame)  #mirror the image

            self.screen.fill(0)

            retval, frame = cap.read()

            frame = numpy.rot90(frame)

            frame = numpy.ascontiguousarray(frame, dtype=numpy.uint8)

            cv2.rectangle(frame, (450, 200), (550, 300), (0, 255, 0), 2)
            cv2.rectangle(frame, (450, 600), (550, 700), (0, 255, 0), 2)
            cv2.rectangle(frame, (450, 1000), (550, 1100), (0, 255, 0), 2)
            cv2.rectangle(frame, (250, 400), (350, 500), (0, 255, 0), 2)
            cv2.rectangle(frame, (250, 800), (350, 900), (0, 255, 0), 2)

            frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            mask1 = cv2.inRange(frameHSV, lower_red, upper_red)
            mask2 = cv2.inRange(frameHSV, lower_blue, upper_blue)

            #res = cv2.bitwise_and(frame,frame, mask= mask1)

            maskOpen1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, kernelOpen)
            maskClose1 = cv2.morphologyEx(maskOpen1, cv2.MORPH_CLOSE,
                                          kernelClose)

            maskOpen2 = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, kernelOpen)
            maskClose2 = cv2.morphologyEx(maskOpen2, cv2.MORPH_CLOSE,
                                          kernelClose)

            maskClose = maskClose1 + maskClose2

            maskFinal = maskClose

            img, contours, h = cv2.findContours(maskFinal.copy(),
                                                cv2.RETR_EXTERNAL,
                                                cv2.CHAIN_APPROX_NONE)

            for contour in contours:

                area = cv2.contourArea(contour)

                if area > 1000:

                    cv2.drawContours(frame, contour, -1, (0, 255, 255), 3)

            for i in range(len(contours)):
                from playsound import playsound

                x, y, w, h = cv2.boundingRect(contour[i])
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
                #print(x,y)

                if x > 450 and y > 200 and x + w < 550 and y + h < 300:

                    cv2.rectangle(frame, (450, 200), (550, 300), (0, 255, 255),
                                  3)

                    # from playsound import playsound
                    playsound(
                        "F:\\openCV-jay\\raunak files\\drum\\sound\\sound\\New folder\\New folder\\sound 1-[AudioTrimmer.com].wav"
                    )

                elif x > 450 and y > 600 and x + w < 550 and y + h < 700:

                    cv2.rectangle(frame, (450, 600), (550, 700), (0, 255, 255),
                                  3)

                    # from playsound import playsound
                    playsound(
                        "F:\\openCV-jay\\raunak files\\drum\\sound\\sound\\New folder\\New folder\\sound 2-[AudioTrimmer.com].wav"
                    )

                elif x > 450 and y > 1000 and x + w < 550 and y + h < 1100:

                    cv2.rectangle(frame, (450, 1000), (550, 1100),
                                  (0, 255, 255), 3)

                    playsound(
                        "F:\\openCV-jay\\raunak files\\drum\\sound\\sound\\New folder\\New folder\\sound 3-[AudioTrimmer.com].wav"
                    )

                elif x > 250 and y > 400 and x + w < 350 and y + h < 500:

                    cv2.rectangle(frame, (250, 400), (350, 500), (0, 255, 255),
                                  3)

                    #from playsound import playsound
                    playsound(
                        "F:\\openCV-jay\\raunak files\\drum\\sound\\sound\\New folder\\New folder\\sound 4-[AudioTrimmer.com].wav"
                    )

                elif x > 250 and y > 800 and x + w < 350 and y + h < 900:

                    cv2.rectangle(frame, (250, 800), (350, 900), (0, 255, 255),
                                  3)

                    #from playsound import playsound
                    playsound(
                        "F:\\openCV-jay\\raunak files\\drum\\sound\\sound\\New folder\\New folder\\sound 5-[AudioTrimmer.com].wav"
                    )

            framepygame = frame

            framepygame = cv2.cvtColor(framepygame, cv2.COLOR_BGR2RGB)

            framepygame = pygame.surfarray.make_surface(framepygame)

            framepygame = pygame.transform.scale(framepygame, (1920, 1080))

            self.screen.blit(framepygame, (0, 0))

            pygame.display.update()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    runningcam = False
                    cap.release()
                    pygame.quit()
                    sys.exit()

                elif event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_ESCAPE:

                        pygame.mixer.music.unpause()
                        runningcam = False
                        self.draw()

                    if event.key == pygame.K_q:

                        runningcam = False
                        cap.release()
                        pygame.quit()
                        sys.exit()
Exemplo n.º 55
0
cv.imwrite('{}/GrayscaleBef.png'.format(pathForToday), gray)
weiSize,serSize,filStr = 7,7,12

gray = cv.fastNlMeansDenoising(gray, gray, weiSize, serSize, filStr)
cv.imwrite('{}/GrayscaleAftS{}F{}.png'.format(pathForToday, weiSize, filStr), gray)

lowThresh,highThresh = 100,300
note = open("{}/data.txt".format(pathForToday), "w")
note.write("Low thresh:{}\nHigh thresh:{}\n".format(lowThresh,highThresh))
note.close()

thresh = cv.Canny(gray, lowThresh, highThresh)
grad = gray
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # Изменить коэфициенты
cv.drawContours(grad, contours, -1, (0,255,0), 3) # Поиграться с коэфициентами

for cnt in contours:
    rect = cv.minAreaRect(cnt) # пытаемся вписать прямоугольник
    box = cv.boxPoints(rect) # поиск четырех вершин прямоугольника
    box = np.int0(box) # округление координат
    cv.drawContours(grad,[box],0,(255,0,0),2) # Поиграться с коэфициентами

cv.imwrite('{}/Countours_TestB{}.png'.format(pathForToday, BLUR_C), grad)
f = open("Ready_Images/iteration.txt", "w")
iterationNumber += 1
f.write(str(iterationNumber))
f.close()
cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 56
0
    def detect (self, image, threshold = 0.7):
        match_method = cv2.TM_CCOEFF_NORMED
        detected = {}
        num_objects = 0
            
        with Timer('detection'):
            # Compare squares against patterns, and if not recognized, take the sample as new pattern
            for i, cnt in enumerate(self.squares):
                max_found = threshold
                max_item = None
                max_location = (-1,-1)
                max_angle = -1
                bounds = cv2.boundingRect(cnt)
                location_abs, size, phi = cv2.minAreaRect(cnt)
                cropped = cv2.getRectSubPix(self.processed, bounds[2:], location_abs)
                location = (location_abs[0]-bounds[0], location_abs[1]-bounds[1])
                w, h = tuple(int(c) for c in size)
                rot = cv2.getRotationMatrix2D(location, phi, 1)
                rotated = cv2.warpAffine(cropped, rot, dsize = cropped.shape[:2], flags=cv2.INTER_CUBIC)                
                sample = cv2.getRectSubPix(rotated, (w-4, h-4), location)
                #sample = cv2.cvtColor(sample, cv2.COLOR_BGR2GRAY)
                sample = cv2.resize(sample, (90, 90))
                #ret, sample = cv2.threshold(sample, 230, 255, cv2.THRESH_BINARY)
                #sample = cv2.equalizeHist(sample)
            
                #cv2.circle(self.original, tuple(int(l) for l in location_abs), 20, (0,0,255))
                #cv2.imshow('crop'+str(i),self.processed)
            
                for tag, pattern in self.patterns.items():
                    match = cv2.matchTemplate(sample, pattern, match_method)
                    result, _, _, _  = cv2.minMaxLoc(match)
                    if result > max_found:
                        #print "Best candidate for %s is %s with %s" % (str(i),tag, str(result))
                        max_found = result
                        max_item = tag
                        max_location = (int(location_abs[0]), int(location_abs[1]))
                        max_angle = phi
                        if result > threshold * 1.5:
                            break
                else:
                    #print "Discarted", str(i)
                    if self.training: 
                        #print "Store new pattern",i
                        cv2.imwrite('patterns/tag-%s.pgm' % str(i), sample)

                if max_item:
                    cv2.drawContours(self.original, self.squares, i, (0, 255, 0), 5)
                    tag_id = max_item.replace("patterns/tag-", "").replace(".pgm", "")
                    max_angle = (360 - max_angle) % 360
                    try:
                        name, angle = tag_id.split("-", 1)
                        #if name=='arrow':
                        #    print "Orig angle", max_angle, "using", angle
                        if angle=='t1':
                            max_angle += 90
                        elif angle=='t2':
                            max_angle += 180
                        elif angle=='t3':
                            max_angle += 270
                    except:
                        name = tag_id
                    cv2.putText(self.original, "%s" % name,
                        (max_location[0]-20, max_location[1]-20), cv2.FONT_HERSHEY_PLAIN, 1, (0,0,255), 1)
                    #vmod = 10
                    #p2 = (int(max_location[0] + vmod * np.cos(max_angle + np.pi/4)),
                    #     int(max_location[1] + vmod * np.sin(max_angle + np.pi/4)))
                    #cv2.line(self.original, max_location, p2, (0,255,255), 3)
                    #print "Chosen %s to be %s" % (str(i), max_item)
                    if not name in detected:
                        detected[name] = []
                    detected[name].append({'angle': int(100 * max_angle)/100.0, 'pos': max_location})
                    num_objects +=1
        return num_objects, detected
Exemplo n.º 57
0
    clone = frame.copy()
    for c in cnts:

        # calculate contour properties
        area = cv2.contourArea(c)
        peri = cv2.arcLength(c, True)
        
        # select desired contours
        if area > 0:
            box = cv2.minAreaRect(c)
            ((x, y), (w, h), theta) = box
            if (theta < -7.0 and theta >- 11.0) and w < 15 and w > 2 and h > 10:
                
                # draw in the minimum rectangles
                box = np.int0(cv2.cv.BoxPoints(box))
                cv2.drawContours(clone, [box], -1, (0, 255, 0), 2)
                if debug > 0:
                    cv2.putText(clone, '%0.1f' % theta, (int(x), int(y-15)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 200, 100), 1)
                    
                # identify center of rectangle
                cx = int(x+(w/2))
                cy = int(y+(h/2))
                cv2.circle(clone, (int(x), int(y)), 3, (0, 0, 255), -1)

                if debug > 0:
                    cv2.putText(clone, ' (%d,%d)' % (int(x), int(y)), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
                if debug > 1:
                    print x, y, w, h, theta
    
    if debug > 0:
        fnum = '%d %d %d' % (fcnt, param1, param2)
Exemplo n.º 58
0
def sleep_detect():
    global outputFrame, lock, COUNTER, active, is_sleep, ALARM_ON

    while True:
        # grab the frame from the threaded video file stream, resize
        # it, and convert it to grayscale
        # channels)
        frame = vs.read()
        frame = imutils.resize(frame, width=450)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # detect faces in the grayscale frame
        rects = detector.detectMultiScale(gray,
                                          scaleFactor=1.1,
                                          minNeighbors=5,
                                          minSize=(30, 30),
                                          flags=cv2.CASCADE_SCALE_IMAGE)

        # loop over the face detections
        for (x, y, w, h) in rects:
            # construct a dlib rectangle object from the Haar cascade
            # bounding box
            rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))

            # determine the facial landmarks for the face region, then
            # convert the facial landmark (x, y)-coordinates to a NumPy
            # array
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)

            # extract the left and right eye coordinates, then use the
            # coordinates to compute the eye aspect ratio for both eyes
            leftEye = shape[lStart:lEnd]
            rightEye = shape[rStart:rEnd]
            leftEAR = eye_aspect_ratio(leftEye)
            rightEAR = eye_aspect_ratio(rightEye)

            # average the eye aspect ratio together for both eyes
            ear = (leftEAR + rightEAR) / 2.0

            # compute the convex hull for the left and right eye, then
            # visualize each of the eyes
            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

            # check to see if the eye aspect ratio is below the blink
            # threshold, and if so, increment the blink frame counter
            if ear < EYE_AR_THRESH:
                COUNTER += 1

                # if the eyes were closed for a sufficient number of
                # frames, then sound the alarm
                if COUNTER >= EYE_AR_CONSEC_FRAMES:
                    # if the alarm is not on, turn it on
                    if not ALARM_ON:
                        ALARM_ON = True

                        # check to see if the TrafficHat buzzer should
                        # be sounded
                        if args["alarm"] > 0 and active == True:
                            th.buzzer.blink(0.1, 0.1, 10, background=True)

                    # is_sleep on
                    is_sleep = True
                    # draw an alarm on the frame
                    cv2.putText(frame, "DROWSINESS ALERT!", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

            # otherwise, the eye aspect ratio is not below the blink
            # threshold, so reset the counter and alarm
            else:
                COUNTER = 0
                ALARM_ON = False
                is_sleep = False

            # draw the computed eye aspect ratio on the frame to help
            # with debugging and setting the correct eye aspect ratio
            # thresholds and frame counters
            cv2.putText(frame, "EAR: {:.3f}".format(ear), (300, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

        # acquire the lock, set the output frame, and release the lock
        with lock:
            outputFrame = frame.copy()
Exemplo n.º 59
0
def get():
    if request.method == 'POST':
        url = request.args.get(key='imagelink')
        req = urlopen(url)
        arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
        img = cv2.imdecode(arr, -1)  # 'Load it as it is'
        # Load image
        original = img.copy()  # Original Image copy
        # Image Processing
        gamma = 0.8
        invGamma = 1 / gamma
        table = np.array([((i / 255.0)**invGamma) * 255
                          for i in np.arange(0, 256)]).astype("uint8")
        cv2.LUT(img, table, img)
        img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  #To grayscale
        img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                    cv2.THRESH_BINARY, 11, 12)  #To binary
        img = np.invert(img)  #Invert
        img = cv2.morphologyEx(img, cv2.MORPH_CLOSE,
                               cv2.getStructuringElement(
                                   cv2.MORPH_RECT, (3, 3)))  #Closing
        contours, hier = cv2.findContours(
            img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  #Find all contours
        biggest_area = 0
        biggest_contour = None
        for i in contours:
            area = cv2.contourArea(i)
            if area > biggest_area:
                biggest_area = area
                biggest_contour = i
        if biggest_contour is None:
            sys.exit(1)
        mask = np.zeros((img.shape), np.uint8)
        cv2.drawContours(mask, [biggest_contour], 0, (255, 255, 255), -1)
        img = cv2.bitwise_and(img, mask)
        grid = original.copy()
        cv2.drawContours(grid, [biggest_contour], 0, (255, 0, 255), 3)
        contours, hier = cv2.findContours(
            img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  #Find all contours
        c = 0
        grid = original.copy()
        average_cell_size = biggest_area / 81
        bound_range = 4
        lower_bound = average_cell_size - average_cell_size / bound_range
        upper_bound = biggest_area / 81 + average_cell_size / bound_range
        cells = []
        x, y, w, h = cv2.boundingRect(biggest_contour)
        epsilon = 0.1 * cv2.arcLength(biggest_contour, True)
        approx = cv2.approxPolyDP(biggest_contour, epsilon, True)
        cv2.drawContours(grid, [approx], 0, (255, 255, 0), 3)
        for i in contours:
            area = cv2.contourArea(i)
            if area >= lower_bound and area <= upper_bound:
                cv2.drawContours(grid, contours, c, (0, 255, 0), 3)
                cells.append(i)
            c += 1
        bx, by, bw, bh = cv2.boundingRect(biggest_contour)
        aw = int(bw / 9)
        ah = int(bh / 9)
        awb = int(aw / 4)
        ahb = int(aw / 4)
        tabla = np.zeros((9, 9))
        dataList = []
        if len(cells) == 81:
            for i in range(9):
                for j in range(9):
                    x = [0, int(bx - awb + j * aw), int(bx + bw)]
                    y = [0, int(by - ahb + i * ah), int(by + bh)]
                    x.sort()
                    y.sort()
                    x = x[1]
                    y = y[1]
                    crop = img[y:by + ah + ahb + i * ah,
                               x:bx + aw + awb + j * aw]
                    cont, hier = cv2.findContours(crop, cv2.RETR_TREE,
                                                  cv2.CHAIN_APPROX_SIMPLE)
                    bsize = 0
                    bcont = None
                    bindex = None
                    for c in range(len(cont)):
                        area = cv2.contourArea(cont[c])
                        if area > bsize:
                            bsize = area
                            bcont = cont[c]
                            bindex = c
                    if bcont is None:
                        sys.exit(1)
                    else:
                        secondbsize = 0
                        secondbcont = None
                        for c in range(len(cont)):
                            if hier[0][c][3] == bindex:
                                area = cv2.contourArea(cont[c])
                                if area > secondbsize:
                                    secondbsize = area
                                    secondbcont = cont[c]
                        if secondbcont is None:
                            sys.exit(2)
                        mask = np.zeros((crop.shape), np.uint8)
                        cv2.drawContours(mask, [secondbcont], 0,
                                         (255, 255, 255), -1)
                        finetune = cv2.bitwise_and(crop, mask)
                        x, y, w, h = cv2.boundingRect(secondbcont)
                        finetune = finetune[y + 3:y + h - 3, x + 3:x + w - 3]
                        finetune = cv2.morphologyEx(
                            finetune, cv2.MORPH_CLOSE,
                            cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)))
                        finetune = np.invert(finetune)
                        kernel = np.array([[-1, -1, -1], [-1, 9, -1],
                                           [-1, -1, -1]])
                        finetune = cv2.filter2D(finetune, -1, kernel)
                        finetune = cv2.resize(finetune, (0, 0), fx=3, fy=3)
                        finetune = cv2.GaussianBlur(finetune, (11, 11), 0)
                        finetune = cv2.medianBlur(finetune, 9)
                        data = pytesseract.image_to_string(
                            finetune,
                            lang='eng',
                            config=
                            '--psm 10 --oem 3 -c tessedit_char_whitelist=123456789'
                        )
                        dataList = dataList + re.split(r',|\.|\n| ', data)
                        number = re.findall('\d+', data)
                        if not re.findall('\d+', data):
                            tabla[i, j] = 0
                        else:
                            tabla[i, j] = number[0]
        else:
            sys.exit(1)
        numpyData = {"array": tabla}
        encodedNumpyData = json.dumps(numpyData, cls=NumpyArrayEncoder)
        return encodedNumpyData
Exemplo n.º 60
0
    return contours


src = cv.imread("D:/images/abc.png")
cv.namedWindow("input1", cv.WINDOW_AUTOSIZE)
cv.imshow("input1", src)
src2 = cv.imread("D:/images/a5.png")
cv.imshow("input2", src2)

# 轮廓发现
contours1 = contours_info(src)
contours2 = contours_info(src2)

# 几何矩计算与hu矩计算
mm2 = cv.moments(contours2[0])
hum2 = cv.HuMoments(mm2)

# 轮廓匹配
for c in range(len(contours1)):
    mm = cv.moments(contours1[c])
    hum = cv.HuMoments(mm)
    dist = cv.matchShapes(hum, hum2, cv.CONTOURS_MATCH_I1, 0)
    if dist < 1:
        cv.drawContours(src, contours1, c, (0, 0, 255), 2, 8)
    print("dist %f" % (dist))

# 显示
cv.imshow("contours_analysis", src)
cv.imwrite("D:/contours_analysis.png", src)
cv.waitKey(0)
cv.destroyAllWindows()