Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想用openCv从图像中删除模糊的黑色边框_Python_Opencv - Fatal编程技术网

Python 我想用openCv从图像中删除模糊的黑色边框

Python 我想用openCv从图像中删除模糊的黑色边框,python,opencv,Python,Opencv,我想删除这个图像的黑色边框,这样当我打印时,我就不会用完吨 我尝试了以下步骤,但它删除了整个图像: #Remove Black Borders from images img = cv2.imread("images/myimage.jpg") gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) _,thresh = cv2.threshold(gray,10,256,cv2.THRESH_BINARY) contours,hierarchy = cv2.fi

我想删除这个图像的黑色边框,这样当我打印时,我就不会用完吨

我尝试了以下步骤,但它删除了整个图像:

#Remove Black Borders from images
img = cv2.imread("images/myimage.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,10,256,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
crop = img[y:y+h,x:x+w]
cv2.imwrite('images/crop.png',crop)

有什么线索吗?

如果“平均值”小于100,您可以尝试获取垂直线和水平线的平均值,将黑色变为白色:

import cv2
import numpy as np

img=cv2.imread("test.jpg",0).T #load image and transpose it(like rotate 90 degree)
sens=1.0 # (0-1]
meanofimg=np.mean(img)*sens #get avarage brightness of img
w,h=img.shape #get image's shape
for i in range(w): #for every horizontal line in transposed img(vertical line in normal)
    if np.mean(img[i])<meanofimg: #check if this line darker than avarage
        img[i]=(img[i]+255)%256 #add 255 for every pixel and get mod 256 this for make zeros 255 and do not touch others
        #img[i]=(img[i]*0+255) #for makin all pixels in line white
img=img.T #turn image to normal
for i in range(h): #every horizontal line in img
    if np.mean(img[i])<meanofimg: # if line darker than avarage
        img[i]=(img[i]+255)%256 #do same thing
        #img[i]=(img[i]*0+255) #for makin all pixels in line white

cv2.imwrite("output.jpg",img) #write img as output.jpg
导入cv2
将numpy作为np导入
img=cv2.imread(“test.jpg”,0).T#加载图像并将其转置(如旋转90度)
sens=1.0#(0-1)
meanofimg=np.平均值(img)*传感器#获得img的平均亮度
w、 h=img.shape#获取图像的形状
对于范围(w)中的i:#对于转置img中的每一条水平线(正常情况下的垂直线)

如果np.mean(img[i])太棒了!你在做什么?我不确定我是否理解这个过程,因为它适用于此图像,但不适用于其他图像。@ruben如果np.mean(img[i]),你可以更改。@ruben为更好地理解算法添加了一些解释
import cv2
import numpy as np

img=cv2.imread("test.jpg",0).T
w,h=img.shape
sens=1.0 # (0-1]
meanofimg=np.mean(img)*sens
dataw=[w,0]
datah=[h,0]
for i in range(w):
    if np.mean(img[i])>meanofimg:
        if i<dataw[0]:
            dataw[0]=i
        else:
            dataw[1]=i
img=img.T
meanofimg=np.mean(img)*sens
for i in range(h):
    if np.mean(img[i])>meanofimg:
        if i<datah[0]:
            datah[0]=i
        else:
            datah[1]=i
img=img[datah[0]:datah[1],dataw[0]:dataw[1]]
cv2.imwrite("output.jpg",img)