【问题标题】:How to remove black bounding box from the image using openCV or Python如何使用 openCV 或 Python 从图像中删除黑色边界框
【发布时间】:2018-11-16 03:22:40
【问题描述】:

【问题讨论】:

  • 如果您还可以提供一个示例,说明您希望通过删除边界框获得什么,那将会很有帮助。
  • 我希望最终的图像只是视网膜眼睛(圆形的),并且要消除包围圆圈的整个黑色。
  • @chatbot_chakra 所以你只是想把视网膜圆盘外的黑色部分改成白色?我建议洪水填充。
  • 其实不是白色的。这只是我想要的圆圈。这是为了将图像输入神经网络,因此目的是只输入有用的部分而忽略其余部分。

标签: python opencv image-processing computer-vision


【解决方案1】:

好吧,下面是开始:

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)

它可能不是最优雅或最有效的,但它可以完成工作,您可以开始使用它。也许您可以查找文档并进行改进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2016-04-27
    • 2020-12-27
    • 2017-10-28
    • 2019-11-17
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多