Example #1
0
def getContours(img):
    #retrieves extreme outer contours
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)[-2:]
    for cnt in contours:
        area = cv2.contourArea(cnt)
        print(area)
        if area > 10:
            cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)
            peri = cv2.arcLength(cnt, True)
            print(peri)
            approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
            print(len(approx))
            objCor = len(approx)
            x, y, w, h = cv2.boundingRect(approx)

            if objCor == 3: objectType = "Tri"
            elif objCor == 4:
                aspRatio = w / float(h)
                if aspRatio > 0.95 and aspRatio < 1.05: objectType = "Square"
                else: objectType = "Rectangle"
            elif objCor > 4: objectType = "Circle"
            else: objectType = "None"
            #draw rectangles on objects
            cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(imgContour, objectType,
                        (x + (w // 2) - 10, y + (h // 2) - 10),
                        cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 0), 2)

    imgCanny = cv2.Canny(img, 50, 50)
    imgStack = stck.stackImages(0.6, ([img, imgCanny], [thresh, thresh]))

    cv2.imshow("Stacked", imgStack)
Example #2
0
    g_min = cv2.getTrackbarPos("Gray min", "Trackbars")
    g_max = cv2.getTrackbarPos("Gray max", "Trackbars")
    a_len = cv2.getTrackbarPos("Arc len", "Trackbars")
    a_len = a_len / 10000
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    ret, thresh = cv2.threshold(blurred, g_min, g_max, cv2.THRESH_BINARY_INV)
    ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thresh2 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thresh3 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)
    ret, thresh4 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO)
    ret, thresh5 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV)

    test = cv2.bitwise_or(thresh, thresh1)
    #res1 = cv2.bitwise_and(frame,frame,mask=thresh)

    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    for contour in contours:
        approx = cv2.approxPolyDP(contour,
                                  a_len * cv2.arcLength(contour, True), True)
        cv2.drawContours(frame, [approx], -1, (0, 0, 255), 3)
    imgStack = stack.stackImages(0.2, ([
        test,
        thresh,
    ], [thresh2, thresh3], [thresh4, thresh5]))
    cv2.imshow("Stacked", imgStack)
    key = cv2.waitKey(1)
    if key == 27:  #esc
        cv2.destroyAllWindows()
        break
Example #3
0
            #TODO: Look up explanation approx.ravel, get coordinates contour
            x = approx.ravel()[0]
            y = approx.ravel()[1]
            #print(len(approx))

            # Write X and Y coordinates to detected object
            if len(approx) >= 150:
                XandY = str(x) + ", " + str(y)
                cv2.putText(frame, XandY, (x, y), font, 2, (0, 0, 255))

    #Video
    #cv2.imshow("Frame", frame)
    #cv2.imshow("Mask",mask)

    #if pic
    #imgResult = cv2.bitwise_and(cap,cap,mask=mask)

    #Stack images, just for convenience
    imgStack = stck.stackImages(0.6, ([frame, gray, thresh]))
    cv2.imshow("Stacked", imgStack)
    process = psutil.Process(os.getpid())
    memory = process.memory_info().rss * pow(10, -6)
    print("%0.2f" % memory)  # in bytes

    key = cv2.waitKey(1)
    if key == 27:  #esc
        break

cap.release()
cv2.destroyAllWindows()
Example #4
0
frame = cv2.imread("txt.png")
cv2.namedWindow("Stacked", cv2.WINDOW_NORMAL)

while True:
    g_min = cv2.getTrackbarPos("Gray min", "Trackbars")
    g_max = cv2.getTrackbarPos("Gray max", "Trackbars")
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)

    ret, thresh = cv2.threshold(blurred, g_min, g_max, cv2.THRESH_BINARY)
    hFrame, wFrame, _ = frame.shape
    #['m', '221', '282', '237', '293', '0'], ['text','x','y','width','height']
    #boxes = pytesseract.image_to_boxes(frame)
    boxes = pytesseract.image_to_data(frame)
    #print(boxes)
    #print(boxes)

    for b in boxes.splitlines():
        b = b.split(' ')
        x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
        cv2.rectangle(frame, (x, hFrame - y), (w, hFrame - h), (0, 0, 255), 1)
        cv2.putText(frame, b[0], (x, hFrame - y + 125),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (50, 50, 255), 1)

    imgStack = stack.stackImages(1, ([frame, frame], [thresh, thresh]))
    while True:
        cv2.imshow("Stacked", imgStack)
        key = cv2.waitKey(1)
        if key == 27:  #esc
            exit()
Example #5
0
    #while True:
    if command == 'y':
        cap = opencamera()
        _, frame = cap.read()
        #frame = cap.read()
        g_min = cv2.getTrackbarPos("Gray min", "Trackbars")
        g_max = cv2.getTrackbarPos("Gray max", "Trackbars")
        MaskedFrame = FrameMasking(frame, g_min, g_max)
        
        hFrame, wFrame, _ = frame.shape
        frame = MaskedFrame
        boxes = pytesseract.image_to_boxes(frame)
        for b in boxes.splitlines():
            b = b.split(' ')
            print(b[0][0])
            #print(b)
            #all strings -> int
            x,y,w,h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
            cv2.rectangle(frame,(x,hFrame-y),(w,hFrame-h),(0,0,255),1)
            cv2.putText(frame, b[0], (x, hFrame-y+125), cv2.FONT_HERSHEY_COMPLEX, 0.5, (50,50,255),1)
        
        
        while True:
            imgStack = stack.stackImages(0.5,([frame,frame],[MaskedFrame,MaskedFrame]))
            cv2.imshow("Stacked", imgStack)
            key = cv2.waitKey(1)
            if key == 27:
                print("break")
                cap.release()
                break
Example #6
0
                    ##calculate difference between center and predefined dot
                    #print(centers[0])
                    temp = centers[0]
                    xCenter = temp[0]
                    yCenter = temp[1]
                    #distance = math.sqrt((math.pow(xCenter,2)-math.pow(320,2))+(math.pow(yCenter,2)-0))
                    distance = math.sqrt(
                        math.pow(xCenter - 480, 2) + math.pow(yCenter - 0, 2))
                    cv2.putText(frame, str(distance), (25, 25),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
                    #D = np.linalg.norm(cx-cy)
                    #print(D)
        """
        x,y,w,h = cv2.boundingRect(img)            
        cv2.rectangle(mask,(x,y),(x+w,y+h),(0,255,0),2)
        plt.imshow(frame)
        print("Distances: vertical: %d, horizontal: %d" % (h,w))
        """

        #a = cv2.magnitude(centers[0],centers[1], 2)
        #print(a)

    imgStack = stck.stackImages(0.5, ([frame, thresh, maskerode, maskerode], [
        maskrange, frame, frame, frame
    ], [maskerode, maskdilation, maskopen, maskclose]))
    #imgStack = stck.stackImages(0.9,([frame,maskerode,maskdilation],[maskopen,maskclose,frame]))
    cv2.imshow("Stacked", imgStack)

    key = cv2.waitKey(1)
    if key == 27:  #esc
        break
Example #7
0
                aspRatio = w / float(h)
                if aspRatio > 0.95 and aspRatio < 1.05: objectType = "Square"
                else: objectType = "Rectangle"
            elif objCor > 4: objectType = "Circle"
            else: objectType = "None"
            #draw rectangles on objects
            cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(imgContour, objectType,
                        (x + (w // 2) - 10, y + (h // 2) - 10),
                        cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 0), 2)


cap = cv2.VideoCapture(0)

while True:
    _, img = cap.read()
    imgContour = img.copy()

    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    imgblur = cv2.GaussianBlur(imgGray, (7, 7), 1)
    #canny corners
    imgCanny = cv2.Canny(imgblur, 50, 50)
    getContours(imgCanny)
    k = cv2.waitKey(1) & 0xFF
    imgBlank = np.zeros_like(img)
    imgStack = stck.stackImages(
        0.6, ([img, imgGray, imgblur], [imgCanny, imgContour, imgBlank]))
    cv2.imshow("Stacked", imgStack)
    if k == ord('q'):
        break