【问题标题】:How to find upper left most corner of my Contour Bounding Box in Python OpenCV如何在 Python OpenCV 中找到我的 Contour Bounding Box 的左上角
【发布时间】:2018-08-25 05:43:24
【问题描述】:

我在做什么:我有一个机械臂,我想在一张纸上找到对象的 x,y 坐标。

我能够找到一张纸的轮廓并得到它的尺寸(h,w)。我想要左上角的坐标,所以当我将对象放在我的纸上时,我可以获得相对于该点的图像坐标。从那里我会将这些像素坐标转换为 cm,然后我将能够将 x,y 坐标返回给我的机械臂。

问题:我找到了轮廓的中心,我认为左上角就是...

中心x坐标-(宽度/2),中心y坐标-(高度/2)

我得到的轮廓框的图片。

*带有我的框的轮廓图片应该在我的轮廓的左上角

但是,我得到了超出纸张范围的坐标。有没有更简单的方法可以找到我的左上角坐标?

代码

class Boundary(object):
def __init__(self, image):
    self.frame = image
    self.DefineBounds()

def DefineBounds(self):

    # convert the image to grayscale, blur it, and detect edges
    # other options are four point detection, white color detection to search for the board?

    gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)

    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    # (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    th, contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

    c = max(contours, key=cv2.contourArea)

    # compute the bounding box of the of the paper region and return it
    cv2.drawContours(self.frame, c, -1, (0, 255, 0), 3)
    cv2.imshow("B and W", edged)
    cv2.imshow("capture", self.frame)
    cv2.waitKey(0)

    # minAreaRect returns (center (x,y), (width, height), angle of rotation )
    # width = approx 338 (x-direction
    # height = 288.6 (y-direction)

    self.CenterBoundBox = cv2.minAreaRect(c)[0]
    print("Center location of bounding box is {}".format(self.CenterBoundBox))
    CxBBox = cv2.minAreaRect(c)[0][1]
    CyBBox = cv2.minAreaRect(c)[0][0]

    # prints picture resolution
    self.OGImageHeight, self.OGImageWidth = self.frame.shape[:2]
    #print("OG width {} and height {}".format(self.OGImageWidth, self.OGImageHeight))

    print(cv2.minAreaRect(c))
    BboxWidth = cv2.minAreaRect(c)[1][1]
    BboxHeight = cv2.minAreaRect(c)[1][0]

    self.Px2CmWidth = BboxWidth / 21.5  # 1cm = x many pixels
    self.Px2CmHeight = BboxHeight / 18  # 1cm = x many pixels
    print("Bbox diemensions {}  x  {}".format(BboxHeight, BboxWidth))
    print("Conversion values Px2Cm width {}, Px2Cm height {}".format(self.Px2CmWidth, self.Px2CmHeight))

    self.TopLeftCoords = (abs(CxBBox - BboxWidth/2), abs(CyBBox - BboxHeight/2))
    x = int(round(self.TopLeftCoords[0]))
    y = int(round(self.TopLeftCoords[1]))
    print("X AND Y COORDINATES")
    print(x)
    print(y)
    cv2.rectangle(self.frame, (x, y), (x+10, y+10), (0, 255, 0), 3)
    print(self.TopLeftCoords)

    cv2.imshow("BOX",self.frame)
    cv2.waitKey(0)

【问题讨论】:

  • 假设 c 是纸的轮廓,cv2.boundingRect(c) 应该返回 [intX, intY, intWidth, intHeight],其中 (intX, intY) 是您的左上角。虽然没有测试。告诉我它是否适合你!
  • 谢谢,我会在今天晚些时候尝试这个解决方案,并在我有实验室访问权限时报告!

标签: python opencv robotics object-recognition opencv-contour


【解决方案1】:

查找包含输入二维点集的最小区域的旋转矩形。

发件人:OpenCV docs

所以你的问题的原因很明显,你的countour有轻微的倾斜,所以包围整个轮廓的最小矩形将在下边超出范围。

自从

contours

只保存一个点向量(这里说的是C++接口),应该很容易通过搜索最大轮廓中x值最低、y值最高的点找到左上角。

【讨论】:

  • 好的,谢谢你说得有道理,当我有实验室访问权限时我会试试这个,并告诉你它是否有效。
  • 是的,谢谢我能够按照您的想法运行此代码,并且它大部分时间都在左上角! self.extLeft = tuple(c[c[:, :, 0].argmin()][0])
  • self.extLeft = tuple(c[c[:, :, 0].argmin()][0])
猜你喜欢
  • 2021-06-29
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
相关资源
最近更新 更多