Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The problem have been solved. I am using Raspberry pi 4B. Using OpenCV 4.2.0. Not Opencv 3.x. Code:

#!/usr/bin/python37
#Date: 19th March, 2020
#Raspberry pi 4B, Buster, Thonny IDE 3.5, OpenCV 4.2.0

import cv2
import numpy as np

image = cv2.imread('centre.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 3)
thresh = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                        cv2.CHAIN_APPROX_NONE)[0]

for c in cnts:
    # compute the center of the contour
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    # draw the contour and center of the shape on the image
    #cv2.drawContours(image, [c], -1, (0, 255, 0), 4)
    #cv2.circle(image, (cX, cY), 2, (0, 0, 255), -1)
    #cv2.line(image,(152, 340), (328, 338),(255,0,0),5)
    #cv2.line(image,(328, 338), (322, 189),(255,0,0),5)
    #cv2.line(image,(322, 189), (491, 176),(255,0,0),5)
    #cv2.line(image,(491, 176), (492,67),(255,0,0),5)
    #cv2.line(image,(492, 67), (153, 62),(255,0,0),5)
    #cv2.line(image,(153, 62), (152, 340),(255,0,0),5)

    corners = np.int32([[152, 340], [328, 338],
                        [322, 189], [491, 176],
                        [492, 67], [153, 62]])

    cv2.polylines(image, [corners], True, (255, 0, 0), 2, cv2.LINE_AA)  


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

Output:

image description