Example #1
0
def main(args):
    image = cv2.imread(args["image"])
    resized = imutils.resize(image, width=300)
    ratio = image.shape[0] / float(resized.shape[0])
    sd = ShapeDetector()

    imgray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(imgray, 127, 255, 0)
    image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # loop over the contours
    for c in contours:
        area = cv2.contourArea(c)
        if area > 500:
            [x, y, w, h] = cv2.boundingRect(c)
            if h > 30 and h < 80 and w > 35:
                M = cv2.moments(c)
                cX = int((M["m10"] / M["m00"]) * ratio)
                cY = int((M["m01"] / M["m00"]) * ratio)
                shape = sd.detect(c)
                print(shape)

                if (shape == "rectangle" or shape == "pentagon"):
                    cv2.drawContours(resized, [c], -1, (0, 255, 0), 3)
                    cv2.putText(resized, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
                                0.5, (255, 255, 255), 2)

    cv2.imshow('final image', resized)
    cv2.waitKey(0)
Example #2
0
def panelSizeEval(im, b_th):
    image = cv2.imread(im)
    print("Evaluating image: %s" % im)
    # resized = imutils.resize(image, width=512, height=384)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, b_th, 255, cv2.THRESH_BINARY)[1]
    #     cv2.imshow("Image", thresh)
    #     cv2.waitKey(0)
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()
    # loop over the contours
    sq = 0
    cSize = 0
    for c in cnts:
        shape = "unidentified"
        M = cv2.moments(c)
        if M["m00"] != 0:
            shape = sd.detect(c)
        if shape == "square":
            ca = cv2.contourArea(c)
            # print(ca)
            if (ca > 40000) and (ca < 160000):
                print(ca)
                sq += 1
                cSize = ca
    if sq == 0:
        return 60000
    else:
        return cSize
Example #3
0
def panelDetect(image,b_th,ct_th):
    image = cv2.imread(image)
    resized = imutils.resize(image, width=640, height=480)
    ratio = image.shape[0] / float(resized.shape[0])
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, b_th, 255, cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()
    # loop over the contours
    sq=0
    for c in cnts:
        shape = "unidentified"
        M = cv2.moments(c)
        if M["m00"] != 0:
            cX = int(round((M["m10"] / M["m00"]))) # * ratio
            cY = int(round((M["m01"] / M["m00"]))) # * ratio
            shape = sd.detect(c)
        if shape == "square":
            if cv2.contourArea(c)>ct_th:
                sq +=1
                c = c.astype("float")
                c *= ratio
                c = c.astype("int")
                peri = cv2.arcLength(c, True)
                approx = cv2.approxPolyDP(c, 0.04 * peri, True)
                cv2.drawContours(image, [c], -1, (0, 255, 0), 1)
                cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 1)
    if sq == 1:
        print("Panel Found:",approx)
        return approx
    else:
        return [[[0,0]],[[0,0]],[[0,0]],[[0,0]]]
Example #4
0
    def detect(imagepath):

        # load the image and resize it to a smaller factor so that
        # the shapes can be approximated better
        image = cv2.imread(imagepath)
        resized = imutils.resize(image, width=300)
        ratio = image.shape[0] / float(resized.shape[0])

        # convert the resized image to grayscale, blur it slightly,
        # and threshold it
        gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

        # find contours in the thresholded image and initialize the
        # shape detector
        cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        sd = ShapeDetector()
        for c in cnts:
            # compute the center of the contour, then detect the name of the
            # shape using only the contour
            M = cv2.moments(c)
            cX = int((M["m10"] / M["m00"]) * ratio)
            cY = int((M["m01"] / M["m00"]) * ratio)
            shape = sd.detect(c)
            print(shape)
Example #5
0
def main(argv):
    # Read the image data from argument
    image = cv2.imread(argv[1])
    # Resize if it is necessary(faster but not accurate)
    # resized = imutils.resize(image, width=800)
    # ratio = image.shape[0] / float(resized.shape[0])
    # Make it Gray scale
    gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)

    # Get the edge by canny effect
    canny = cv2.Canny(gray.copy(), 80, 120)

    # Set the kernel for dilation(make the white line thicker)
    kernel = np.ones((3, 3), np.uint8)
    dilation = cv2.dilate(canny.copy(), kernel, iterations=7)
    cv2.imwrite("dilate.jpg", dilation)

    # Get the contour from dilated picture, EXTERNAL contour used
    cnts = cv2.findContours(dilation.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_NONE)
    cnts = imutils.grab_contours(cnts)

    # initialize the ShapeDetector class
    sd = ShapeDetector()

    oracle = []
    for c in cnts:
        try:
            # Do if the contourarea is larger than specific area
            if cv2.contourArea(
                    c) > np.shape(image)[0] / 40 * np.shape(image)[1] / 40:

                # Get the moments of the shapes
                M = cv2.moments(c)
                cX = int((M["m10"] / M["m00"]))
                cY = int((M["m01"] / M["m00"]))

                # Detect the shape
                shape = sd.detect(c)

                # Put additional information in the image
                c = c.astype("float")
                c = c.astype("int")
                cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
                cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 0, 255), 2)

                # Store the shape information to oracle
                oracle.append([shape, np.array([cX, cY])])
        except:
            continue

    # Save the image file
    cv2.imwrite("Shape_recognized.jpg", image)
    oracle = np.array(oracle)

    # Create the json file to return the following value to PHP
    # ["Type of shape", "RowID", "number of shape in the same row"]
    jsonfile = get_block(image, oracle, (6, 6))
    print(jsonfile)
Example #6
0
def panelSizeEval(im, b_th):
    image = cv2.imread(im)
    resized = imutils.resize(image, width=640, height=480)
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, b_th, 255, cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()
    # loop over the contours
    sq = 0
    cSize = 0
    for c in cnts:
        shape = "unidentified"
        M = cv2.moments(c)
        if M["m00"] != 0:
            shape = sd.detect(c)
        if shape == "square":
            ca = cv2.contourArea(c)
            if (ca > 2000) and (ca < 8000):
                # print(ca)
                sq += 1
                # cSize = cSize + ca
                cSize = ca
    if sq == 0:
        return 4000
    else:
        return cSize
Example #7
0
def process(source_file_path, text):
    sd = ShapeDetector(source_file_path, text)
    sd.process()
    for b in sd.boxes:
        pprint(vars(b))
    for l in sd.lines:
        pprint(vars(l))
    return sd.processed_image, sd.boxes, sd.lines
Example #8
0
def getAlarm():
    for dweet in dweepy.listen_for_dweets_from('deanDronePI'):
        #grabbing the relevant data from dweet
        content1 = dweet["content"]
        cows = content1["cows"]
        content1 = dweet["content"]
        pic = content1["pic"]

        if pic = 10:
            cam = Picamera()
            camera.capture('/home/pi/Desktop/image.jpg')
            ap = argparse.ArgumentParser()
            ap.add_argument("-i", "--image", required=True,
            	help="/home/pi/Desktop")
            args = vars(ap.parse_args())

            image = cv2.imread(args["image.jpg"])
            smaller = imutils.resize(image, width=300)
            ratio = image.shape[0] / float(resized.shape[0])

            gray = cv2.cvtColor(smaller, cv2.COLOR_BGR2GRAY)
            blurred = cv2.GaussianBlur(gray, (5, 5), 0)
            thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]


            cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            cnts = cnts[0] if imutils.is_cv2() else cnts[1]
            sd = ShapeDetector()
            counted_cows = 0


            # loop over the contours
            for c in cnts:
            	# compute the center of the contour, then detect the name of the
            	# shape using only the contour
            	M = cv2.moments(c)
            	cX = int((M["m10"] / M["m00"]) * ratio)
            	cY = int((M["m01"] / M["m00"]) * ratio)
            	shape = sd.detect(c)

            	# multiply the contour (x, y)-coordinates by the resize ratio,
            	c = c.astype("float")
            	c *= ratio
            	c = c.astype("int")
            	cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
            	counted_cows = counted_cows + 1


            if counted_cows >= cows:
                alarm = "false"
                return alarm

            else:
                alarm = "true"
                return alarm
        else:
            alarm = "not yet time"
            return alarm
Example #9
0
def detectgambar(file):
    # construct the argument parse and parse the arguments
    #ap = argparse.ArgumentParser()
    #ap.add_argument("-i", "--image", required=True,
    #    help="path to the input image")
    #args = vars(ap.parse_args())

    # load the image and resize it to a smaller factor so that
    # the shapes can be approximated better
    image = cv2.imread(file)
    resized = imutils.resize(image, width=300)
    ratio = image.shape[0] / float(resized.shape[0])

    # convert the resized image to grayscale, blur it slightly,
    # and threshold it
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

    # find contours in the thresholded image and initialize the
    # shape detector
    thresh = 255 - thresh
    cnts = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    sd = ShapeDetector()

    # loop over the contours
    for c in cnts:
        # compute the center of the contour, then detect the name of the
        # shape using only the contour
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)

        shape = sd.detect(c)

        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")

        if shape == "triangle":
            cv2.drawContours(image, [c], -1, (0, 0, 255), 2)
        else:
            cv2.drawContours(image, [c], -1, (0, 255, 0), 2)

        cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
                    0.5, (0, 0, 255), 2)

        # show the output image
        cv2.imshow("Image", image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

#input('Press ENTER to exit')
Example #10
0
def start_game(image, cnts):
    game_board = []
    x_coordinates = []
    y_coordinates = []
    lowestY = 999999
    sd = ShapeDetector()
    cd = ColorDetector()

    for c in cnts:
        # compute the center of the contour
        M = cv2.moments(c)
        if M["m00"] == 0:
            M["m00"] = 1
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])

        shape = sd.detect(c)
        if shape == "hexagon":
            # print(str(cX) + ", " + str(cY))
            # draw the contour and center of the shape on the image
            color = cd.determine_color(image, c)
            cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
            cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)
            cv2.putText(image, color, (cX - 20, cY - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            if cY < lowestY:
                lowestY = cY
                game_board.append([color])
                x_coordinates.append([cX])
                y_coordinates.append(cY)

            else:
                for i in range(0, len(y_coordinates)):
                    if cY == y_coordinates[i]:
                        x_coordinates[i].append(cX)
                        game_board[i].append(color)
    if len(game_board) == 0:
        return
    for i in range(0, len(x_coordinates)):
        swapped = True
        while swapped:
            swapped = False
            for j in range(len(x_coordinates[i]) - 1):
                if x_coordinates[i][j] > x_coordinates[i][j + 1]:
                    # Swap the elements
                    x_coordinates[i][j], x_coordinates[i][
                        j + 1] = x_coordinates[i][j + 1], x_coordinates[i][j]
                    game_board[i][j], game_board[i][j + 1] = game_board[i][
                        j + 1], game_board[i][j]
                    # Set the flag to True so we'll loop again
                    swapped = True
    game_board = game_board[::-1]
    x_coordinates = x_coordinates[::-1]
    y_coordinates = y_coordinates[::-1]
    solve_game(game_board, x_coordinates, y_coordinates)
Example #11
0
def recognize(image):
    image = imutils.resize(image, width=500)
    #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = auto_canny(image)
    cv2.imwrite("edged.png", edged)

    image = cv2.imread("edged.png")
    resized = imutils.resize(image, width=300)
    ratio = image.shape[0] / float(resized.shape[0])

    # convert the resized image to grayscale, blur it slightly,
    # and threshold it
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

    # find contours in the thresholded image and initialize the
    # shape detector
    cv2.imwrite("edged.png", edged)
    (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)

    cv2.drawContours(edged, cnts, -1, (0, 255, 0), 3)
    (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts],
                  key=lambda x: x[1])
    sd = ShapeDetector()

    # loop over the contours
    for (c, _) in cnts:
        # compute the center of the contour, then detect the name of the
        # shape using only the contour
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)
        shape = sd.detect(c)

        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        cv2.imwrite("drawContours.png", image)
        cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (255, 255, 255), 2)
        # show the output image
        print(shape)
        output = list()
        output.append(str(shape))

        return output
Example #12
0
def main(args):
    '''
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True,
                    help="path to the input image")
    args = vars(ap.parse_args())
    '''

    # load the image and resize it to a smaller factor so that
    # the shapes can be approximated better
    image = cv2.imread(args["image"])
    resized = imutils.resize(image, width=300)
    ratio = image.shape[0] / float(resized.shape[0])

    # convert the resized image to grayscale, blur it slightly,
    # and threshold it
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

    # find contours in the thresholded image and initialize the
    # shape detector
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()

    # loop over the contours
    for c in cnts:
        # compute the center of the contour, then detect the name of the
        # shape using only the contour
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)
        shape = sd.detect(c)

        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")
        cv2.drawContours(resized, [c], -1, (0, 255, 0), 2)
        cv2.putText(resized, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (255, 255, 255), 2)

        # show the output image
        cv2.imshow("Image", resized)
        cv2.waitKey(0)
Example #13
0
def detector(img1,img2_color):
    img1 = cv2.resize(img1,(360,360)) 
    img2 = cv2.cvtColor(img2_color,cv2.COLOR_BGR2GRAY)
    #img2 = cv2.resize(img2,(360,360)
    ret,thresh = cv2.threshold(img2,127,255,0)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    sd = ShapeDetector()
    cnts = []
    for cnt in contours:
        if sd.detect(cnt) == "triangle":
            cnts.append(cnt)
    cv2.drawContours(img2_color, contours, -1, (0,255,0), 3)        
    cv2.imshow('Refined', img2_color)
    cv2.waitKey(0)
    return None
def contour_id(
    cnts,
    fout="ContourShape.txt"
):  #take contours and write their shape in file if circle enough
    sd = ShapeDetector()
    i = 0
    f = open(fout, "w")
    for c in cnts:
        if sd.detect(c) == "circle":
            i = i + 1
            for j in range(len(c)):
                f.write(
                    str(i) + " " + str(c[j, 0, 0]) + " " + str(c[j, 0, 1]) +
                    "\n")
    f.close()
Example #15
0
def detect(image, hsv):
    resized = imutils.resize(hsv, width=300)
    ratio = image.shape[0] / float(resized.shape[0])

    # convert the resized image to grayscale, blur it slightly,
    # and threshold it
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (15, 15), 0)
    thresh = cv2.threshold(blurred, 110, 255, cv2.THRESH_BINARY)[1]

    # find contours in the thresholded image and initialize the
    # shape detector
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()
    # loop over the contours
    for c in cnts:
        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        #peri = cv2.arcLength(c, True)
        #print(cv2.boundingRect(c))
        #approx = cv2.approxPolyDP(c, 0.04 * peri, True)
        #print("approx:\n", approx)
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")

        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)

    return image, cnts
Example #16
0
def process_img(image):

    final = apply_brightness_contrast(image, 12, 50)

    sd = ShapeDetector()
    cd = ColorDetector()
    # convert to gray
    processed_img = cv2.cvtColor(final, cv2.COLOR_BGR2GRAY)
    processed_img = cv2.equalizeHist(processed_img)

    # edge detection
    processed_img = cv2.Canny(processed_img, threshold1=100, threshold2=200)

    cnts = cv2.findContours(processed_img.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)

    game_board = []
    x_coordinates = []
    y_coordinates = []
    lowestY = 999999

    cv2.imwrite("./Gray_Image.jpg", image)

    return image, cnts
Example #17
0
def press_next(image, cnts):
    sd = ShapeDetector()
    cd = ColorDetector()

    for c in cnts:
        M = cv2.moments(c)
        if M["m00"] == 0:
            M["m00"] = 1
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])

        shape = sd.detect(c)
        if shape == "rectangle":
            print(str(cX) + ", " + str(cY))
            mouse.move(cX, cY, absolute=True, duration=0.12)
            mouse.click()
Example #18
0
def main(args):
    image = cv2.imread(args["image"])
    if image.shape[0] > image.shape[1]:
        resized = imutils.resize(image, width=240)
    else:
        resized = imutils.resize(image, height=240)

    sd = ShapeDetector()
    imgray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    (thresh, im_bw) = cv2.threshold(imgray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    image, contours, hierarchy = cv2.findContours(im_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    count = 0;
    # loop over the contours
    for c in contours:
        area = cv2.contourArea(c)
        if area > 200:
            [x, y, w, h] = cv2.boundingRect(c)
            #cv2.drawContours(resized, [c], -1, (0, 255, 0), 2)
            if h in range(30, 50) and w in range(30,50):
                M = cv2.moments(c)
                cX = int((M["m10"] / M["m00"]) - w / 2.5)
                cY = int((M["m01"] / M["m00"]) + h / 1.5)
                shape = sd.detect(c)
                print(shape, cX, cY)

                if shape in ["square", "rectangle","pentagon"]:
                    # save the crop_image
                    crop_image = resized[y:y + w, x:x + w]

                    #img_name = "Media/well_{0}.png".format(count)
                    #count += 1
                    img_name = "test.jpg"

                    #compare with raw/images
                    cv2.imwrite(img_name, crop_image)
                    result = match_shape(img_name)

                    # draw contour
                    cv2.drawContours(resized, [c], -1, (255, 0, 0), 2)
                    cv2.putText(resized, result, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)

                    #draw rectangle for well
                    cv2.rectangle(resized, (x-90, y-80), (x+125, y+135), (255, 0, 0), 2)

    cv2.imshow('final image', resized)
    cv2.waitKey(0)
Example #19
0
def find_marker(image):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1]
    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    sd = ShapeDetector()
    ratio = 100 / 30
    for c in cnts:
        # compute the center of the contour, then detect the name of the
        # shape using only the contour
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)
        shape = sd.detect(c)

        num = 0.01 * cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, num, True)
        #		if len(approx)==5:
        #		    print "pentagon"
        #		    thing = cv2.contourArea(c, false)
        if len(approx) == 4:
            print("rectangle")
            thing = cv2.contourArea(c, false)

# multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        #	cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
        #		0.5, (255, 255, 255), 2)

        # show the output image
        cv2.imshow("Image", image)
        cv2.waitKey(0)

    # compute the bounding box of the of the paper region and return it
    return thing
Example #20
0
def detector(img1,img2_color):
    img1 = cv2.resize(img1,(360,360)) 
    img2 = cv2.cvtColor(img2_color,cv2.COLOR_BGR2GRAY)
    #img2 = cv2.resize(img2,(500,360)) 
    #img2_color = cv2.resize(img2_color,(500,360)) 
    sift = cv2.xfeatures2d.SIFT_create()
    kp1, des1 = sift.detectAndCompute(img1,None)
    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 10)
    search_params = dict(checks = 50)
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    kp2, des2 = sift.detectAndCompute(img2,None)
    matches = flann.knnMatch(des1,des2,k=2)
    ratio_thresh = 0.8
    good_matches = []
    for m,n in matches:
        if m.distance < ratio_thresh * n.distance:
            good_matches.append(m)
    list_kp2 = [kp2[mat.queryIdx].pt for mat in good_matches] 
    print(list_kp2)
    if(len(good_matches)>5):
        ret,thresh = cv2.threshold(img2,127,255,0)
        im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        sd = ShapeDetector()
        cnts = []
        for cnt in contours:
            if sd.detect(cnt) == "triangle":
                cnts.append(cnt)
        cv2.drawContours(img2_color, cnts, -1, (0,0,255), 3)   
        for cnt in cnts:
            try:
                ellipse = cv2.fitEllipse(cnt)
                img2_color = cv2.ellipse(img2_color,ellipse,(0,255,0))
            except:
                pass     
        cv2.imshow('Refined', img2_color)
        cv2.waitKey(0)
        return 1
    else:
        return 0    
def detect_shape(image):
    resized = imutils.resize(image, width=300)
    ratio = image.shape[0] / float(resized.shape[0])

    # convert the resized image to grayscale, blur it slightly,
    # and threshold it

    blurred = cv2.GaussianBlur(resized, (5, 5), 0)
    gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
    lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB)
    thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()
    for c in cnts:
        # compute the center of the contour
        M = cv2.moments(c)
        if M["m00"] == 0:
            continue

        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)

        # detect the shape of the contour and label the color
        shape = sd.detect(c)
        if shape in ['no_shape', 'rectangle', 'square']:
            return False
        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape and labeled
        # color on the image
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")
        text = "{}".format(shape)
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (255, 255, 255), 2)
        return True
Example #22
0
    def processBot(contour,checkpointType):
        '''
        param-contour [Type-list], checkpointType[Type-CheckpointType object]
        returns-checkPointList[Type-list of Checkpoint objects]
        It works the same way as Frame.processCheckpoints(). The exact same way as above.  
        ''' 
        #print 'Frame: getCenter called '
        checkPointList = []
        for c in contour:
            # compute the center of the contour, then detect the name of the
            # shape using only the contour
            Moment = cv2.moments(c)
            

            if Moment["m00"] > 0:
                origin = None
                if not Frame.runOnce:
                    origin = Frame.townHall.center
                else:
                    origin = Point(0,0)
                Frame.runTimeCounter += 1
                shapeDetector = ShapeDetector()
                #shape = shapeDetector.detect(c)
                shape = CheckpointShape.SQUARE
                #print "DEtected Shape " + shape
                point = Point()
                point.x = int((Moment["m10"] / Moment["m00"]+ 1e-7) * Frame.ratio)#uses moment of inertia concept
                point.y = int((Moment["m01"] / Moment["m00"]+ 1e-7) * Frame.ratio)
                dist = float(distance(origin,point))
                # multiply the contour (x, y)-coordinates by the resize ratio,
                # then draw the contours and the name of the shape on the image
                #print "Position: " + point.toString()
                c = c.astype("float")
                c *= Frame.ratio
                c = c.astype("int")
                area=cv2.contourArea(c)
                if area > 1600:
                    Frame.draw_contour(c,checkpointType.type, point, checkpointType.contour_color)
                    checkPointList.append(Checkpoint(area, point, dist, 0, shape))
                
        return checkPointList
Example #23
0
    def get_center(contour,checkpointType):
        #print 'Frame: getCenter called '
        checkPointList = []
        for c in contour:
            # compute the center of the contour, then detect the name of the
            # shape using only the contour
            Moment = cv2.moments(c)
            

            if Moment["m00"] > 0:
                origin = None
                if not Frame.runOnce:
                    origin = Frame.townHall.center
                else:
                    origin = Point(0,0)
                Frame.runTimeCounter += 1
                shapeDetector = ShapeDetector()
                #shape = shapeDetector.detect(c)
                shape = CheckpointShape.SQUARE
                #print "DEtected Shape " + shape
                point = Point()
                point.x = int((Moment["m10"] / Moment["m00"]) )#uses moment of inertia concept
                point.y = int((Moment["m01"] / Moment["m00"]))
                dist = float((((origin.x-point.x)*(origin.x-point.x))+((origin.y - point.y )*(origin.x - point.y)))^(1/2))
                # multiply the contour (x, y)-coordinates by the resize ratio,
                # then draw the contours and the name of the shape on the image
                #print "Position: " + point.toString()
                c = c.astype("float")
                c *= Frame.ratio
                c = c.astype("int")
                area=cv2.contourArea(c)
                if area > 700:
                    Frame.draw_contour(c,checkpointType.type,point,checkpointType.contour_color)
                    checkPointList.append(Checkpoint(area,point,dist,0,shape))
                
        return checkPointList
resized = imutils.resize(image, width=300)
ratio = image.shape[0] / float(resized.shape[0])

# convert the resized image to grayscale, blur it slightly,
# and threshold it
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 111, 245, cv2.THRESH_BINARY_INV)[1]

# find contours in the thresholded image and initialize the
# shape detector
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sd = ShapeDetector()

# loop over the contours
for c in cnts:
    # compute the center of the contour, then detect the name of the
    # shape using only the contour
    if cv2.contourArea(c) > MIN_THRESH:
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)
        shape = sd.detect(c)

        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        c = c.astype("float")
        c *= ratio
Example #25
0
 # Calculate the shrink ratio
 ratio = img2proc.shape[0] / float(resized.shape[0])
 # Grey scaling
 gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
 # Gaussian blur
 blurred = cv2.GaussianBlur(gray, (gaussian_size, gaussian_size), 0)
 #         cv2.imshow("Image", blurred)
 #         cv2.waitKey(0)
 # Set the threshold to filter out the GCP area
 thresh = cv2.threshold(blurred, th_GCP, 255, cv2.THRESH_BINARY)[1]
 # Find contours of the filtered areas
 cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                         cv2.CHAIN_APPROX_SIMPLE)
 cnts = cnts[0] if imutils.is_cv2() else cnts[1]
 # Load shape detector
 sd = ShapeDetector()
 # Initialize the center of the GCP (cX,cY) coordinates
 cX = 0
 cY = 0
 # The total number of the square shape and rectangle shape detected
 snum = 0
 shape = "unidentified"
 contourSize = 0
 for c in cnts:
     shape = "unidentified"
     shape = sd.detect(c)
     #             print(shape)
     if cv2.contourArea(c) >= contour_min_size and cv2.contourArea(
             c
     ) <= contour_max_size and shape == "square":  # or shape == "rectangle"):
         print("Contour size: %f" % cv2.contourArea(c))
    def get_frame(self):
        if self.video.isOpened():
            check, frame = self.video.read()  

            self.BWl=self.win.get()
            self.BWh=self.wout.get()
            self.onoffBW= self.blandwh.get()
            self.onoffinv= self.inv.get()

            #frame = cv2.imread("/Users/maria/Desktop/IGEM/qrcode.png") #use this line to test with still pictures

            #overlay= cv2.imread("/Users/maria/Desktop/IGEM/SQR.jpg") 

            dim = (102, 102) #Resize Size of Square Overlay
            
            #overlay= cv2.resize(overlay, dim)
            #rows,cols,channels = overlay.shape

            #overlay=cv2.addWeighted(frame[0:0+rows, 0:0+cols],0,overlay,1,1)

            #frame[0:0+rows, 0:0+cols ] = overlay

            #frame= cv2.imwrite('combined.png', added_image)


            img= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #turn picture to grayscale
            #overlay= cv2.cvtColor(overlay, cv2.COLOR_BGR2GRAY) #turn picture to grayscale
            

            resized = cv2.resize(img, (200, 200))#imutils.resize(img, width=300)
            ratio = img.shape[0] / float(resized.shape[0])

            #barcodes = decode(img) #decoding before black and white !!!!!!!!!!!!
            
            #if self.onoffBW == 1:
            #for i in range(20, 180, 10):
            (thresh, img) = cv2.threshold(img, self.BWl, self.BWh, cv2.THRESH_BINARY) #turn picture Black and White
            barcodes = decode(img) #to use to scan for barcodes before inverting image
            #    print(i)
            #    if not not barcodes:
            #        break

            #for i in range(20, 180, 5):
            #    (thresh, src) = cv2.threshold(img, 100, self.BWh, cv2.THRESH_BINARY) #turn picture Black and White
            #    barcodes = decode(src) #to use to scan for barcodes before inverting image
            #    #print(i)
            #    if not not barcodes:
            #        break
            
            #overlay = cv2.threshold(overlay, self.BWl, self.BWh, cv2.THRESH_BINARY)[1]
            
            

            #if self.onoffinv == 1:
            img = cv2.bitwise_not(img) #Invert color of image!!!!!!!!!!!!

            #overlay = cv2.bitwise_not(overlay) #Invert color of image
            #thresh = cv2.threshold(img, self.BWl, self.BWh, cv2.THRESH_BINARY)[1]

            #img = cv2.blur(img,(5,5)) #BLUR IMAGE

            barcodesinv = decode(img) #find QR codes (when picture is Black and Wait, seems to work better with out codes)

            if barcodes: #detection of barcodes without inverting image
                # loop over the detected barcodes
                for barcode in barcodes:
                    # extract the bounding box location of the barcode and draw the
                    # bounding box surrounding the barcode on the image
                    (x, y, w, h) = barcode.rect
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 3)
                    # the barcode data is a bytes object so if we want to draw it on
                    # our output image we need to convert it to a string first
                    barcodeData = barcode.data.decode("utf-8")
                    barcodeType = barcode.type
                    
                    #ciphertext=barcodeData
                    #from Crypto.Cipher import AES
                    #import base64
                    #block_size=16
                    #key=b'\xebT\xe7Tm\xf3S/\x06\xeb\x005\xc6z\x0f\x1f'
                    #ciphertext = base64.b64decode(ciphertext) #binascii.unhexlify(ciphertext) #puts in into byte format

                    #iv = ciphertext[:block_size]
                    #decipher = AES.new(key, AES.MODE_CBC, iv)
                    #ENCRYPTED=binascii.unhexlify(ciphertext)
                    #DECRYPTED=decipher.decrypt(ciphertext[AES.block_size:])
                    #DECRYPTED2 = DECRYPTED.rstrip(b"\0") #takes away padding
                    #DECRYPTED3 = DECRYPTED2.decode("utf-8") 
                    #plaintext = cipher.decrypt(ciphertext[AES.block_size:])
                    #print(DECRYPTED3)
                    #barcodeData=DECRYPTED3

                    # draw the barcode data and barcode type on the image
                    text = "{} ({})".format(barcodeData, barcodeType)
                    cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 2)
                    # print the barcode type and data to the terminal
                    print("[INFO] Found {} barcode\n Your message is: {}".format(barcodeType, barcodeData))
                    #break #uncomment this line to break loop as soon as QR code is detected

            if barcodesinv: #detection of barcodes with image inverted
                # loop over the detected barcodes
                for barcode in barcodesinv:
                    # extract the bounding box location of the barcode and draw the
                    # bounding box surrounding the barcode on the image
                    (x, y, w, h) = barcode.rect
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 3)
                    # the barcode data is a bytes object so if we want to draw it on
                    # our output image we need to convert it to a string first
                    barcodeData = barcode.data.decode("utf-8")
                    barcodeType = barcode.type
                        
                    # draw the barcode data and barcode type on the image
                    text = "{} ({})".format(barcodeData, barcodeType)
                    cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 2)
                    
                    ## print the barcode type and data to the terminal
                    #print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
                    self.info = "[INFO] Found {} barcode: {}".format(barcodeType, barcodeData)
                    #break #uncomment <--- this line to break loop as soon as QR code is detected

            ## Shape Detector
            cnts = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            cnts = imutils.grab_contours(cnts)
            sd = ShapeDetector()
            if len(cnts)>2:
                for c in cnts:
                    M = cv2.moments(c)
                    if M["m00"]!=0:
                        #if  cv2.moments(c)
                        # compute the center of the contour, then detect the name of the
                        # shape using only the contour
                        M = cv2.moments(c)
                        
                        cX = int((M["m10"] / M["m00"]) )
                        cY = int((M["m01"] / M["m00"]) )
                        shape = sd.detect(c)

                        # multiply the contour (x, y)-coordinates by the resize ratio,
                        # then draw the contours and the name of the shape on the image
                        c = c.astype("float")
                        #c *= ratio

                        #dim = (60, 60)  ##TO RESIZE Squares
                        #overlay= cv2.resize(overlay, dim)
                        #overlay = cv2.threshold(overlay, 100, 255, cv2.THRESH_BINARY)[1]

                        c = c.astype("int")
                        if shape=="square" or shape=="rectangle":
                            if cX>dim[1] and cY>dim[1] and cX<len(img)-dim[1] and cY<len(img)-dim[1]:
                                cv2.drawContours(img, [c], -1, (100, 100, 100), 10)
                                cv2.putText(img, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 2)
                                #overlay=cv2.addWeighted(img[cY-int(rows/2):cY+int(rows/2), cX-int(cols/2):cX+int(cols/2)],0,overlay,1,1) #ADDING squares in the center of other squares
                                #img[cY-int(rows/2):cY+int(rows/2), cX-int(cols/2):cX+int(cols/2)] = overlay


            return(check, frame)
    def detectAreaRatio(self, folder=None):

        loadImg = LoadImages()

        if folder == None:
            folder = "../images/isolated_images/"

        self.image_list = loadImg.load(folder)

        # load the image and resize it to a smaller factor so that
        # the shapes can be approximated better
        index = 0
        index_obj = 0
        for img in self.image_list:
            resized = imutils.resize(img, width=300)
            ratio = img.shape[0] / float(resized.shape[0])

            # convert the resized image to grayscale, blur it slightly,
            # and threshold it
            gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
            blurred = cv2.GaussianBlur(gray, (5, 5), 0)
            thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

            # find contours in the thresholded image and initialize the
            # shape detector
            cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)
            cnts = imutils.grab_contours(cnts)
            sd = ShapeDetector()

            # create image objects
            img_obj = image(img)
            img_obj.id = index
            self.image_obj_list.append(img_obj)

            # loop over the contours
            for c in cnts:
                #area = cv2.contourArea(c)
                #print('area is:', area)
                if cv2.contourArea(c) <= 200:
                    continue

                # compute the center of the contour, then detect the name of the
                # shape using only the contour
                M = cv2.moments(c)
                cX = 0
                cY = 0
                if M["m00"] != 0:
                    cX = int((M["m10"] / M["m00"]) * ratio)
                    cY = int((M["m01"] / M["m00"]) * ratio)
                shape = sd.detect(c)

                # multiply the contour (x, y)-coordinates by the resize ratio,
                # then draw the contours and the name of the shape on the image
                c = c.astype("float")
                c *= ratio
                c = c.astype("int")
                cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
                #cv2.putText(img, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
                #    0.5, (255, 255, 255), 2)

                # Rotated Rectangle
                rect = cv2.minAreaRect(c)
                box = cv2.boxPoints(rect)
                box = np.int0(box)
                imgRotRect = cv2.drawContours(img, [box], 0, (0, 0, 255), 2)

                # calculate the area
                area_obj = cv2.contourArea(c)
                area_box = self.findAreaRotRect(box)

                # create dna_object
                d_obj = dna_object()
                d_obj.id = index_obj
                d_obj.getAttributes(c, area_obj, area_box)

                self.dna_object_list.append(d_obj)

                r = d_obj.area_ratio

                print("# ", index)
                print("area obj: %.2f" % area_obj)
                print("area box: %.2f" % area_box)
                print("area ratio: %.2f" % r)

                index_obj += 1

                #cv2.imshow(str(index), imgRotRect)
                #cv2.waitKey(0)

            index += 1
        mask = get_mask(hsv, icol)
        mask2 = get_mask(hsv, icol2)
        mask = mask + mask2

        kernal = np.ones((3, 3), np.uint8)
        mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal)
        Change_image = cv2.bitwise_and(Change_image, Change_image, mask=mask)

        cv2.imshow('img4', Change_image)

        # find contours in the thresholded image and initialize the
        # shape detector
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]
        sd = ShapeDetector()

        # loop over the contours
        for c in cnts:

            # compute the center of the contour, then detect the name of the
            # shape using only the contour
            M = cv2.moments(c)
            try:
                cX = int((M["m10"] / M["m00"]))
                cY = int((M["m01"] / M["m00"]))
                (shape, approx) = sd.detect(c)

                if len(approx) == 3 or len(approx) == 4:
                    (x, y, w, h) = cv2.boundingRect(approx)
                    cv2.rectangle(Result_image, (x, y), (x + w, y + h), 255, 2)
Example #29
0
th2 = cv2.adaptiveThreshold(blurred,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,2)
cv2.imshow('Adaptive Threshold', th2)

edges = cv2.Canny(resized,100,200)
cv2.imshow('Edges', edges)

# find contours in the thresholded image and initialize the
# shape detector


cnts = cv2.findContours(th2.copy(), cv2.RETR_LIST,
	cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

sd = ShapeDetector()
cl = ColorLabeler()

# loop over the contours
cnt = 0

for c in cnts:
	# compute the center of the contour, then detect the name of the
	# shape using only the contour
	M = cv2.moments(c)
	#cX = int((M["m10"] / M["m00"]) * ratio)
	#cY = int((M["m01"] / M["m00"]) * ratio)
	
	shape = sd.detect(c, height)
	color = cl.label(lab, c)
	
Example #30
0
ratio = image.shape[0] / float(resized.shape[0])

# blur the resized image slightly, then convert it to both
# grayscale and the L*a*b* color spaces
blurred = cv2.GaussianBlur(resized, (5, 5), 0)
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB)
thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1]
cv2.imshow("Thresh", thresh)

# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# initialize the shape detector and color labeler
sd = ShapeDetector()
cl = ColorLabeler()

# loop over the contours
for c in cnts:
    # compute the center of the contour
    M = cv2.moments(c)
    cX = int((M["m10"] / M["m00"]) * ratio)
    cY = int((M["m01"] / M["m00"]) * ratio)

    # detect the shape of the contour and label the color
    shape = sd.detect(c)
    color = cl.label(lab, c)

    # multiply the contour (x, y)-coordinates by the resize ratio,
    # then draw the contours and the name of the shape and labeled
Example #31
0
    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([0, 220, 0])
    upper_blue = np.array([15, 255, 200])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame, frame, mask=mask)

    # find contours in the thresholded image and initialize the
    # shape detector
    cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    sd = ShapeDetector()

    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()