Home > OS >  Draw points only without joining them using drawContours() opencv python
Draw points only without joining them using drawContours() opencv python

Time:11-10

Trying to get image B as shown below but below code gives image A [followed tutorial by https://pyimagesearch.com/2021/10/06/opencv-contour-approximation/]. mask refers to the green region.

cnts = cv2.findContours(mask.copy(), mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)


cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)


eps = 0.001 


peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, eps * peri, True)

#here np.array of shape [4,1,2] is got.
  
output = mask.copy()
cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
(x, y, w, h) = cv2.boundingRect(c)


cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,0.9, (0, 255, 0), 2)
cv2.imshow("Approximated Contour", output)
cv2.waitKey(0)]

enter image description here

How to get image B? I think drawContours() should not join points with a line but dont find how to get that. Opencv link https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html doesnt show the code to get image B

CodePudding user response:

Try this:

import numpy as np
import cv2

img = cv2.imread('corner.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)

corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
corners = np.int0(corners)

for corner in corners:
    x,y = corner.ravel()
    cv2.circle(img,(x,y),3,255,-1)
    
cv2.imshow('Corner',img)
cv2.waitKey(0)
  • Related