繁体   English   中英

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

[英]How to remove black bounding box from the image using openCV or Python

我的视网膜扫描图像周围有一个黑色矩形框。 如何单独使用python或opencv删除该部分,因为我必须执行数百张图像的操作

生成的图像应如下所示:

裁剪的最终和所需图像,基本上是在删除边界黑框之后

好吧,这是一些入门:

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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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