简体   繁体   English

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

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

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.

相关问题 如何使用opencv从图像中删除黑色背景并使之透明? - How to remove a black background from an image and make that transparent using opencv? opencv-python:如何用边界框坐标裁剪图像 - opencv-python: how to crop image with bounding box coordinates 如何在每条线上绘制单个边界框,裁剪边界框并将图像保存在 opencv python 文件夹中 - How to draw a single bounding box on each line ,crop the bounding box and save image in folder opencv python 使用Python OpenCV,如何在特定颜色边界框内提取图像区域? - Using Python OpenCV, How would you extract an image area inside a particular color bounding box? 使用 Python OpenCV 删除图像的黑色标题部分 - Remove black header section of image using Python OpenCV 如何从图像中识别最大的边界矩形并使用 Opencv 和 python 将它们分成单独的图像 - How to identify largest bounding rectangles from an image and separate them into separate images using Opencv and python 如何去歪斜文本图像并检索该图像 Python OpenCV 的新边界框? - How to de-skew a text image and retrieve the new bounding box of that image Python OpenCV? OpenCV:如何从黑白图像的特定部分删除文本 - OpenCV: How to remove text from specific part of a black and white image 将边界框的内容保存为新图像 - opencv python - saving contents of bounding box as a new image - opencv python 使用 OpenCV 像素化 ROI 边界框并将其覆盖在原始图像上 - Pixelate ROI bounding box and overlay it on original image using OpenCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM