Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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_Python Imaging Library - Fatal编程技术网

Python 从图像中删除颜色

Python 从图像中删除颜色,python,image,opencv,image-processing,python-imaging-library,Python,Image,Opencv,Image Processing,Python Imaging Library,我想从下面的图像中删除颜色,由于这种颜色,我无法从图像中清晰地提取文本 我正在使用下面的代码,但我没有得到清晰的文本 import numpy as np from PIL import Image im = Image.open('my_file.tif') im = im.convert('RGBA') data = np.array(im) # just use the rgb values for comparison rgb = data[:,:,:3] color = [246,

我想从下面的图像中删除颜色,由于这种颜色,我无法从图像中清晰地提取文本


我正在使用下面的代码,但我没有得到清晰的文本

import numpy as np
from PIL import Image

im = Image.open('my_file.tif')
im = im.convert('RGBA')
data = np.array(im)
# just use the rgb values for comparison
rgb = data[:,:,:3]
color = [246, 213, 139]   # Original value
black = [0,0,0, 255]
white = [255,255,255,255]
mask = np.all(rgb == color, axis = -1)
# change all pixels that match color to white
data[mask] = white

# change all pixels that don't match color to black
##data[np.logical_not(mask)] = black
new_im = Image.fromarray(data)
new_im.save('new_file.tif')

请帮我做这个

图2:


尝试OpenCV转换,但请记住使用3个通道,否则会出现错误

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

我假设你想提取报价。为此,可以执行一系列过滤操作以删除非文本轮廓。获得处理结果后,可以使用OCR工具(如PyteSeract)进行文本提取

光学字符识别结果

On behalf of the hundreds of ACLU activists who
called on Governor Walker to veto House Bill
156, we are disappointed that he did not put
students or the Constitution first today.”
—Joshua A. Decker
Executive Director
代码

导入cv2
导入pytesseract
pytesseract.pytesseract.tesseract_cmd=r“C:\Program Files\tesseract OCR\tesseract.exe”
#加载图像和阈值
image=cv2.imread('1.png')
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
thresh=cv2.阈值(灰色,0,255,cv2.thresh\u二进制+cv2.thresh\u大津)[1]
#用水平形状的内核连接文本
kernel=cv2.getStructuringElement(cv2.morp_RECT,(10,3))
explate=cv2.explate(阈值、内核、迭代次数=3)
#使用纵横比过滤删除非文本轮廓
cnts=cv2.找到的孔(扩张,cv2.外部翻新,cv2.链近似简单)
如果len(cnts)==2个其他cnts[1],则cnts=cnts[0]
对于碳纳米管中的碳:
x、 y,w,h=cv2.boundingRect(c)
纵横比=w/h
如果相位<3:
cv2.拉深轮廓(阈值[c],-1,(0,0,0),-1)
#反转图像与OCR
结果=255-阈值
data=pytesseract.image_to_字符串(结果,lang='eng',config='--psm 6')
打印(数据)
cv2.imshow(“结果”,结果)
cv2.waitKey()

使用opencv以无色模式加载,然后甚至可能将其反转?“我没有得到明文”-您得到的是什么?预期的输出是什么?就像@ForceBru说的,请提供您当前的输入和输出。您的问题很模糊。你想去除哪种颜色-至少有3种深浅的蓝色、白色和灰色?当你去除颜色时,你认为会留下什么?黑色白色灰色透明度?我想删除该图像中的所有颜色,除了你的答案适用于该图像的文本颜色。但对于另一个图像,它不起作用。放大后,整个数据将消失,最终图像结果为零(全白色)。我把这个图像添加到我的问题中。请检查并建议一个动态解决方案。它可能不适用于文本在背景色上有不同颜色阴影的图像。是否有任何方法从这些图像中提取数据。否则,请给出一些想法,以提高这些图像的OCR准确性。是的,这是可能的,但解决方案不会是动态的,它将适用于每个特定情况。使用传统的图像处理技术获得一个在所有情况下都能工作的动态解决方案是很困难的。您能解释一下如何用通用方法处理该图像吗?这对我真的很有帮助。
On behalf of the hundreds of ACLU activists who
called on Governor Walker to veto House Bill
156, we are disappointed that he did not put
students or the Constitution first today.”
—Joshua A. Decker
Executive Director
import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Load image and threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Connect text with a horizontal shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,3))
dilate = cv2.dilate(thresh, kernel, iterations=3)

# Remove non-text contours using aspect ratio filtering
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    aspect = w/h
    if aspect < 3:
        cv2.drawContours(thresh, [c], -1, (0,0,0), -1)

# Invert image and OCR
result = 255 - thresh
data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
print(data)

cv2.imshow('result', result)
cv2.waitKey()