Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/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中的文本外,如何删除图像上的所有内容?_Python_Image_Opencv_Image Processing_Text - Fatal编程技术网

除了python中的文本外,如何删除图像上的所有内容?

除了python中的文本外,如何删除图像上的所有内容?,python,image,opencv,image-processing,text,Python,Image,Opencv,Image Processing,Text,我有一个带有测量值的图像,我需要用python读取,现在它读取的文本最多,但不是全部,因为有些行挡住了我的去路。我不能使用原始图像,所以我制作了一个看起来像我正在使用的图像 def腐蚀(img): 内核=np.ones((3,3),np.uint8) 侵蚀=cv2.侵蚀(img,内核,迭代次数=1) 灰色=cv2.CVT颜色(腐蚀,cv2.COLOR_BGR2灰色) 边缘=cv2.Canny(灰色,50150,光圈尺寸=3) minLineLength=10 maxLineGap=1 lines

我有一个带有测量值的图像,我需要用python读取,现在它读取的文本最多,但不是全部,因为有些行挡住了我的去路。我不能使用原始图像,所以我制作了一个看起来像我正在使用的图像

def腐蚀(img):
内核=np.ones((3,3),np.uint8)
侵蚀=cv2.侵蚀(img,内核,迭代次数=1)
灰色=cv2.CVT颜色(腐蚀,cv2.COLOR_BGR2灰色)
边缘=cv2.Canny(灰色,50150,光圈尺寸=3)
minLineLength=10
maxLineGap=1
lines=cv2.HoughLinesP(边,1,np.pi/180120,minLineLength,maxLineGap)
对于行中的行:
对于直线中的x1、y1、x2、y2:
cv2.线(腐蚀,(x1,y1),(x2,y2),(255255),7)
我曾尝试使用
OpenCV
函数
houghLinesP
在这些线上画一条线,但这并没有删除所有线,仍然会在整个位置留下一些点,如下所示:

我想做的是提供如下内容作为输入:

并得到如下输出:

我需要删除所有行但不更改
文本是因为我需要保存文本坐标。

方法是将文本放大并连接在一起,形成一个轮廓。从这里我们可以找到轮廓并使用最小阈值区域进行过滤。如果它通过了这个过滤器,那么我们就有一个想要的文本ROI要保留,我们把这个ROI画到一个面具上

导入cv2
将numpy作为np导入
image=cv2.imread('3.png')
掩码=np.one(image.shape,dtype=np.uint8)*255
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
thresh=cv2.阈值(灰色,0,255,cv2.thresh\u二进制\u INV+cv2.thresh\u OTSU)[1]
kernel=cv2.getStructuringElement(cv2.morp_RECT,(5,5))
explate=cv2.explate(阈值、内核、迭代次数=3)
cnts=cv2.查找对象(扩张、cv2.RETR\u树、cv2.链近似\u简单)
如果len(cnts)==2个其他cnts[1],则cnts=cnts[0]
对于碳纳米管中的碳:
面积=cv2。轮廓面积(c)
如果面积小于5000:
x、 y,w,h=cv2.boundingRect(c)
遮罩[y:y+h,x:x+w]=图像[y:y+h,x:x+w]
cv2.imshow('thresh',thresh)
cv2.imshow(“扩张”,扩张)
cv2.imshow(“面具”,面具)
cv2.waitKey()

你有你正在使用的代码的示例吗?@Barb将其添加到了帖子中
import cv2
import numpy as np

image = cv2.imread('3.png')
mask = np.ones(image.shape, dtype=np.uint8) * 255
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=3)

cnts = cv2.findContours(dilate, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area < 5000:
        x,y,w,h = cv2.boundingRect(c)
        mask[y:y+h, x:x+w] = image[y:y+h, x:x+w]

cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('mask', mask)
cv2.waitKey()