[英]How to remove black bounding box from the image using openCV or Python
I have this retinal scanned image with a black rectangular box around this. 我的视网膜扫描图像周围有一个黑色矩形框。 How do I remove that portion alone using python or opencv since I have to carry out the operation for hundreds of images
如何单独使用python或opencv删除该部分,因为我必须执行数百张图像的操作
The resultant image should look like this: 生成的图像应如下所示:
The cropped final and required image,basically after removing the bounding black box 裁剪的最终和所需图像,基本上是在删除边界黑框之后
Well, here is something to get started: 好吧,这是一些入门:
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)
It may not be most elegant or the most efficient, but it does the job and you can get started with this. 它可能不是最优雅或效率最高的,但是可以完成工作,您可以开始使用它。 Maybe you can look up documentation and improve this.
也许您可以查找文档并对此进行改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.