![](/img/trans.png)
[英]How to remove a black background from an image and make that transparent using opencv?
[英]How to remove black bounding box from the image using openCV or Python
好吧,这是一些入门:
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.