Python 文件使用 OpenCV 写入所有边界框坐标

Aji*_*kya 2 python opencv machine-learning computer-vision python-3.x

我的任务:我的任务是提取以下图像的边界框坐标: 在此处输入图片说明

我有以下代码。我正在尝试使用 roi 获取这些坐标,但我不确定如何获取它们。

import cv2
import numpy as np

large = cv2.imread('1.jpg')

small = cv2.cvtColor(large, cv2.COLOR_BGR2GRAY)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
grad = cv2.morphologyEx(small, cv2.MORPH_GRADIENT, kernel)

_, bw = cv2.threshold(grad, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)

contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

mask = np.zeros(bw.shape, dtype=np.uint8)

for idx in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[idx])
    mask[y:y+h, x:x+w] = 0
    cv2.drawContours(mask, contours, idx, (255, 255, 255), -1)
    r = float(cv2.countNonZero(mask[y:y+h, x:x+w])) / (w * h)

    if r > 0.45 and w > 8 and h > 8:
        cv2.rectangle(large, (x, y), (x+w-1, y+h-1), (0, 255, 0), 1)
        roi=large[y:y+h, x:x+w]

print(roi)
Run Code Online (Sandbox Code Playgroud)

结果应该是这样的:

1675,1335,2338,1338,2337,1455,1674,1452.  :Box1
3067,519,3604,521,3603,651,3066,648       :Box2
1017,721,1729,726,1728,857,1016,852       :Box3
Run Code Online (Sandbox Code Playgroud)

我已经提到: 使用 OpenCV Python 提取所有边界框。在此链接上,当他们已经将带有矩形 GUI 的带注释的图像作为输入时,他们正在提取边界框内的图像。我想将检测到的区域提取到文本文件中。我该怎么做?

Ha *_*Bom 5

x, y, w, h = cv2.boundingRect(contours[idx]) 是你想要的坐标,然后写到txt文件中:

...
with open("coords.txt","w+") as file:
    for idx in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[idx])
        mask[y:y+h, x:x+w] = 0
        file.write("Box {0}: ({1},{2}), ({3},{4}), ({5},{6}), ({7},{8})".format(idx,x,y,x+w,y,x+w,y+h,x,y+h))
        cv2.drawContours(mask, contours, idx, (255, 255, 255), -1)
        r = float(cv2.countNonZero(mask[y:y+h, x:x+w])) / (w * h)
...
Run Code Online (Sandbox Code Playgroud)

结果将包含每个框的 4 个点,就像这样。

Box 0: (360,259), (364,259), (364,261), (360,261)
Box 1: (380,258), (385,258), (385,262), (380,262)
Box 2: (365,258), (370,258), (370,262), (365,262)
Box 3: (386,256), (393,256), (393,260), (386,260)
Box 4: (358,256), (361,256), (361,258), (358,258)
Run Code Online (Sandbox Code Playgroud)