嘗試如下所示獲取影像 B,但下面的代碼給出了影像 A [遵循 https://pyimagesearch.com/2021/10/06/opencv-contour-approximation/ 的教程]。mask 指的是綠色區域。
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)]

如何獲得影像 B?我認為 drawContours() 不應該用一條線連接點,但不知道如何得到它。Opencv 鏈接https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html沒有顯示獲取影像 B 的代碼
uj5u.com熱心網友回復:
嘗試這個:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531073.html
標籤:opencv
