简体   繁体   English

使用OpenCV 2.4.9检测矩形

[英]Detecting rectangles using OpenCV 2.4.9

I want to detect rectangles from an image, and this is the code I am using: 我想从图像中检测矩形,这是我正在使用的代码:

    import cv2 ; import numpy as np
    img = cv2.imread('/home/stagiaire/Bureau/new_photos_mire/0294/new/corrigees/GRE.TIF',0)
ret,thresh = cv2.threshold(img,127,255,0)
    contours,hierarchy = cv2.findContours(thresh, 1, 2)
    cnt = contours[0]
    x,y,w,h = cv2.boundingRect(cnt)
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    im = cv2.drawContours(im,[box],0,(0,0,255),2)

The message error I am getting is: 我收到的消息错误是:

AttributeError: 'module' object has no attribute 'boxPoints'

Concerning this line of code: 关于这一行代码:

    box = cv2.boxPoints(rect)

I beleive that it is caused because of the version of OpenCV I am using (2.4.9). 我相信这是由于我使用的OpenCV版本(2.4.9)引起的。 It is not possible for me now to have the 3.0 version so any way I can detect rectangles using openCV 2.9 and Python 2.7 ?? 我现在不可能拥有3.0版本,因此我可以使用openCV 2.9和Python 2.7检测矩形的任何方式?

EDIT 编辑

So as answered by Surabhi Valma, this could be a solution : 因此,正如Surabhi Valma回答的那样,这可能是一个解决方案:

Just add cv2.cv.BoxPoints(rect) instead of cv2.boxPoints(rect)

When I go thru releases could not find 2.9. 当我通过发行时找不到2.9。 May be your version is 2.4.9. 可能您的版本是2.4.9。 If you try with 3.x, it may work. 如果尝试使用3.x,它可能会起作用。

There was an opencv-issue / feature already tracked to closure This function is for sure available in 3.0.0-dev or above, Please try to upgrade and check. 已经有一个opencv-issue / 功能已被跟踪到关闭状态。此功能肯定在3.0.0-dev或更高版本中可用,请尝试升级并检查。

The most recent OpenCV 2 release is 2.4.13.2; 最新的OpenCV 2版本是2.4.13.2; there is no 2.9. 没有2.9。 Anyways, this method was never included in the cv2 library with Python wrappers. 无论如何,该方法从未包含在Python包装器的cv2库中。 Your choices are to upgrade to OpenCV 3+ or fall back on the (deprecated) cv module (which is included with older versions of Python OpenCV wrappers) to access the C method directly: 您的选择是升级到OpenCV 3+,还是依靠(不建议使用的) cv模块(旧版本的Python OpenCV包装器随附)来直接访问C方法:

rect = cv2.minAreaRect(cnt)
box = np.int0(cv2.cv.BoxPoints(rect))
cv2.drawContours(im,[box],0,(0,0,255),2)

只需添加cv2.cv.BoxPoints(rect)而不是cv2.boxPoints(rect)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM