简体   繁体   中英

How to find upper left most corner of my Contour Bounding Box in Python OpenCV

What I'm doing : I have a robotic arm and I want to find x,y coordinates for objects on a piece of paper.

I am able to find a contour of a sheet of paper and get its dimensions (h,w). I want the coordinates of my upper left corner so when I place objects onto my piece of paper I can get image coordinates relative to that point. From there I'll convert those pixel coordinates to cm and I'll be able to return x,y coordinates to my robotic arm.

Problem : I find the center of my contour and I thought the upper left corner would then be the...

center x coordinate - (width/2), center y coordinate - (height/2)

Picture of the contour box I'm getting. 在此处输入图像描述

*Picture of contour with my box that should be around the upperleft corner of my contour 在此处输入图像描述

However, I get a coordinate out of the bounds of my piece of paper. Is there an easier way to find my upper left coordinates?

code

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)

Finds a rotated rectangle of the minimum area enclosing the input 2D point set.

From: OpenCV docs

So the reason for your problem is obvious, your countour has a slight slant, so the minimum rectangle which encloses the whole contour will be out of bounds on the lower side.

Since

contours

just holds a vector of points (talking about the C++ interface here) it should be easy to find the upper left corner by searching for the point with lowest x and highest y value in the largest contour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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