python - 在OpenCV Python中查找旋转矩形

标签 python opencv

我有如下的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
enter image description here

关于python - 在OpenCV Python中查找旋转矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62596734/

相关文章:

python - asfortranarray 中的运行时警告

python - 是否可以通过 Python 的 Socket 模块发送文件和/或文件夹?

python - 在 Python 中使用 Pyramids 进行快速模板匹配

Python 属性错误 : 'module' object has no attribute 'DIST_L2'

opencv - 如何在原生 C++ 代码中的 android studio 中使用 opencv

python - 如何使用 Shapely 获取 MultiPolygon 内每个点的列表

python - 使用 Boto3 回调,显示上传进度

python - OSX brew python "Could not find a version that satisfies the requirement pyopencv"

Python selenium 防止网站被阻塞

python - OpenCV Python:窗口大小和鼠标事件坐标