如何使用openCV或Python从图像中移除黑色边界框

2024-04-25 01:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文
1条回答
网友
1楼 · 发布于 2024-04-25 01:58:59

好吧,这里有一些开始:

import cv2

threshold = 25

img = cv2.imread('D:\\img.jpg', 0) # load grayscale version

# the indeces where the useful region starts and ends
hStrart = 0
hEnd = img.shape[0]
vStart = 0
vEnd = img.shape[1]

# get row and column maxes for each row and column
hMax = img.max(1)
vMax = img.max(0)

hDone_flag = False
vDone_flag = False

# go through the list of max and begin where the pixel value is greater 
# than the threshold
for i in range(hMax.size):
    if not hDone_flag:
        if hMax[i] > threshold:
            hStart = i
            hDone_flag = True

    if hDone_flag:
        if hMax[i] < threshold:
            hEnd = i
            break

for i in range(vMax.size):
    if not vDone_flag:
        if vMax[i] > threshold:
            vStart = i
            vDone_flag = True

    if vDone_flag:
        if vMax[i] < threshold:
            vEnd = i
            break

# load the color image and choose only the useful area from it
img2 = (cv2.imread('D:\\img.jpg'))[hStart:hEnd, vStart:vEnd,:]

# write the cropped image
cv2.imwrite("D:\\clipped.jpg", img2)

它可能不是最优雅或最有效率的,但它确实起到了作用,你可以从这开始。也许你可以查阅文档并改进它。在

相关问题 更多 >