在 OpenCV Python 中查找旋转矩形 [英] Find Rotated Rectangle in OpenCV Python

查看:45
本文介绍了在 OpenCV Python 中查找旋转矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像下面这样的python函数

def getContours(img, imgContour):轮廓,层次结构 = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:]对于轮廓中的 cnt:面积 = cv2.contourArea(cnt)如果面积>1000:cv2.drawContours(imgContour, 轮廓, -1, (255, 0, 255), 7)peri = cv2.arcLength(cnt, True)approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)# 打印(长度(大约))x, y, w, h = cv2.boundingRect(approx)#cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 5)如果 len(approx) == 3:cv2.putText(imgContour, Segitiga", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)elif len(大约) == 4:(x, y, w, h) = cv2.boundingRect(approx)ar = w/float(h)打印(ar)如果 ar >= 0.95 并且 ar <= 1.05:cv2.putText(imgContour, Persegi", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)别的:cv2.putText(imgContour, Segi Panjang", (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)别的: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)

i have python function like below

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)

how do I know that the rectangle detected in the image has a rotation of 45 (rhombus)? I haven't found a function in opencv related to rotation detection

解决方案

From what i think, you are asking for how to get a rotated rect to capture some 45degree placed item.

Take a look here use cv::minAreaRect function

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

这篇关于在 OpenCV Python 中查找旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆