我有如下的python函数
def getContours(img, imgContour):
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:]
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 1000:
cv2.drawContours(imgContour, contours, -1, (255, 0, 255), 7)
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
# print(len(approx))
x, y, w, h = cv2.boundingRect(approx)
#cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 5)
if len(approx) == 3:
cv2.putText(imgContour, "Segitiga", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
print(ar)
if ar >= 0.95 and ar <= 1.05:
cv2.putText(imgContour, "Persegi", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
else:
cv2.putText(imgContour, "Segi Panjang", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
else:
cv2.putText(imgContour, "Lingkaran/Octagon", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
我怎么知道图像中检测到的矩形旋转45度(菱形)?我尚未在opencv中找到与旋转检测相关的功能
最佳答案
从我的想法来看,您正在询问如何获取旋转的矩形以捕获一些45度放置的物品。
在这里看看
使用cv::minAreaRect
函数
rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img,[box],0,(0,0,255),2)
https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html关于python - 在OpenCV Python中查找旋转矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62596734/