使用以下代码,我可以删除图像中的水平线。参见下面的结果。
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.png',0)
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果是非常好的,不是完美的但很好。我要实现的是这里显示的那个。我正在使用此代码。
我的问题之一是:如何保存Sobel X没有应用灰色效果的情况?作为原始但已处理..
另外,还有更好的方法吗?
编辑
对源图像使用以下代码是好的。效果很好。
import cv2
import numpy as np
img = cv2.imread("image.png")
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = cv2.bitwise_not(img)
th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2)
cv2.imshow("th2", th2)
cv2.imwrite("th2.jpg", th2)
cv2.waitKey(0)
cv2.destroyAllWindows()
horizontal = th2
vertical = th2 …Run Code Online (Sandbox Code Playgroud) 所以,从我可以开始的..
我正在使用OCR。该脚本可以很好地满足我的需求。它以对我来说可以的准确性来检测单词。
结果就是:附加图像的准确性为100%。
from PIL import Image
import pyocr.builders
import os
os.putenv("TESSDATA_PREFIX", "C:\\Program Files (x86)\\Tesseract-OCR")
tools = pyocr.get_available_tools()
tool = tools[0]
langs = tool.get_available_languages()
lang = langs[0] #eng
file = "test.png"
txt = tool.image_to_string(Image.open(file), lang=lang, builder=pyocr.builders.TextBuilder())
print(txt + '\n')
'''
word = ['SHINE','ON','YOU','CRAZY','DIAMOND','SYD']
if word[2] in txt:
print("## WORD IN LIST ##")
else:
print("## NOT IN LIST ##")'''
Run Code Online (Sandbox Code Playgroud)
现在的问题是:如何从图像中删除输出OCR列表中存在的单词(在名为的代码中txt)?我的意思是,如果控制台中(以及列表中)的输出中存在SHINE字样,如何在image中删除它?或者,如果不移除,请创建一个遮罩,以便将其隐藏...
我认为ocr可以通过选择文本区域并在文本周围创建边框来工作。在这种情况下,如何删除(甚至显示)此ROI /边界框?在pyocr文档中,有一些关于此功能的提示(显示边界框),但我不知道如何使用它。
任何帮助/提示表示赞赏。
谢谢
编辑:这段代码向我展示了每个字符的边界框
import csv
import cv2
from pytesseract import pytesseract as pt
pt.run_tesseract('test.png', 'output', …Run Code Online (Sandbox Code Playgroud) 我正在尝试查找/计算下一张图像中的轮廓区域:
目标是删除您在图像中可以看到的所有点,因此斑点的轮廓面积小于我给出的值。
我该如何设置这个?
这是我使用的代码...
import cv2
im = cv2.imread('source.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(im, contours, -1, (0,255,0), 1)
cv2.imshow('contour',img)
cv2.waitKey(0)
cv2.imwrite('contour.png',img)
Run Code Online (Sandbox Code Playgroud)
...这是源图像:
谢谢
这是一个非常棘手的问题。
我正在使用以下代码来检测手写图像中的文本。我不希望它识别字符,在这种情况下,它只会在他找到的每个字符/单词周围创建一个边框。
这是代码:
import cv2
import tesserocr as tr
from PIL import Image
import numpy as np
img = cv2.imread('1.png')
idx = 0
# since tesserocr accepts PIL images, converting opencv image to pil
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# initialize api
api = tr.PyTessBaseAPI()
alphabet_min = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
alphabet_max = ['A', 'B', 'C', 'D', 'E', 'F', …Run Code Online (Sandbox Code Playgroud) 我有这个图像:
我想做的是检测其内部轮廓(数字 3)的质心。
这是我现在的代码:
import cv2
import numpy as np
im = cv2.imread("three.png")
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts = cv2.drawContours(im, contours[1], -1, (0, 255, 0), 1)
cv2.imshow('number_cnts', cnts)
cv2.imwrite('number_cnts.png', cnts)
m = cv2.moments(cnts[0])
cx = int(m["m10"] / m["m00"])
cy = int(m["m01"] / m["m00"])
cv2.circle(im, (cx, cy), 1, (0, 0, 255), 3)
cv2.imshow('center_of_mass', im)
cv2.waitKey(0)
cv2.imwrite('center_of_mass.png', cnts)
Run Code Online (Sandbox Code Playgroud)
这是(错误的..)结果:
为什么质心被绘制在图像的左侧而不是(或多或少)中心?
有什么办法解决这个问题吗?
我正在开展一个项目,要求我检测图像中的文本区域.这是我到目前为止使用下面的代码实现的结果.
代码如下:
import cv2
import numpy as np
# read and scale down image
img = cv2.pyrDown(cv2.imread('C:\\Users\\Work\\Desktop\\test.png', cv2.IMREAD_UNCHANGED))
# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
127, 255, cv2.THRESH_BINARY)
# find contours and get the external one
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
# with each contour, draw boundingRect in green
# a minAreaRect in red and
# a minEnclosingCircle in blue
for c in contours:
# get the bounding rect
x, y, w, h = cv2.boundingRect(c) …Run Code Online (Sandbox Code Playgroud) 鉴于以下代码:
import numpy as np
import cv2
gray = cv2.imread('image.png')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)
a,b,c = lines.shape
for i in range(a):
cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1, cv2.LINE_AA)
cv2.imwrite('houghlines.jpg', gray)
cv2.imshow('img', gray)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
我可以实现此(源)图像中的水平线:
这是结果:
如何删除红色的行?我想要实现的是删除这些行,使图像更干净并可供另一个进程使用。代码取自此处。
通过此代码,我在下图中的字符周围创建了一些边界框:
import csv
import cv2
from pytesseract import pytesseract as pt
pt.run_tesseract('bb.png', 'output', lang=None, boxes=True, config="hocr")
# To read the coordinates
boxes = []
with open('output.box', 'rt') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
if len(row) == 6:
boxes.append(row)
# Draw the bounding box
img = cv2.imread('bb.png')
h, w, _ = img.shape
for b in boxes:
img = cv2.rectangle(img, (int(b[1]), h-int(b[2])), (int(b[3]), h-int(b[4])), (0, 255, 0), 2)
cv2.imshow('output', img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
输出值
我想拥有的是:
程序应在边框的X轴上绘制一条垂直线(仅适用于第一个和第三个文本区域。中间的那个区域对此过程不应有兴趣)。
目标是这样(还有另一种实现方法,请解释):一旦我有了这两条线(或者更好的是一组坐标),就使用遮罩覆盖这两个区域。
可能吗 ? …
正如@Silencer所建议的,我使用他在这里发布的代码来绘制图像中数字的轮廓.在某些时候,处理数字,就像0,6,8,9我看到他们的内部轮廓(圆圈)也被填充.我怎么能阻止这个?是否有为cv2.drawContours()设置的最小/最大动作区域,所以我可以排除内部区域?
我试图传递cv2.RETR_EXTERNAL但是使用此参数只考虑整个外部区域.
代码就是这样(再次感谢Silencer.几个月来一直在寻找这个......):
import numpy as np
import cv2
im = cv2.imread('imgs\\2.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#contours.sort(key=lambda x: int(x.split('.')[0]))
for i, cnts in enumerate(contours):
## this contour is a 3D numpy array
cnt = contours[i]
res = cv2.drawContours(im, [cnt], 0, (255, 0, 0), 1)
cv2.imwrite("contours.png", res)
'''
## Method 1: crop the region
x,y,w,h = cv2.boundingRect(cnt)
croped = res[y:y+h, x:x+w] …Run Code Online (Sandbox Code Playgroud) 我有以下图片:
我想要做的是保留所有红色数字。
使用此代码..
import cv2
import numpy as np
def callback(x):
pass
cap = cv2.VideoCapture(0)
cv2.namedWindow('image')
ilowH = 0
ihighH = 179
ilowS = 0
ihighS = 255
ilowV = 0
ihighV = 255
# create trackbars for color change
cv2.createTrackbar('lowH', 'image', ilowH, 179, callback)
cv2.createTrackbar('highH', 'image', ihighH, 179, callback)
cv2.createTrackbar('lowS', 'image', ilowS, 255, callback)
cv2.createTrackbar('highS', 'image', ihighS, 255, callback)
cv2.createTrackbar('lowV', 'image', ilowV, 255, callback)
cv2.createTrackbar('highV', 'image', ihighV, 255, callback)
while True:
# grab the frame
frame = cv2.imread('color_test.png')
# …Run Code Online (Sandbox Code Playgroud) 我想列一个清单。这些列表必须包含 RGB 调色板的所有组合(1600 万)。
像这样的东西:
all_colours = [[0,0,0],[0,0,1],[0,0,2],[0,0,3] ... [255,255,253],[255,255,254],[255,255,255]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
这就是我真正拥有的,而不是那么多......
rgb = []
for r in range(0, 256):
rgb.append([r])
print(rgb)
Run Code Online (Sandbox Code Playgroud) 我有这些列表包含RGB格式的蓝色值.
low = [
[0, 0, 128],
[65, 105, 225],
[70, 130, 180],
[72, 61, 139],
[83, 104, 120]
]
Run Code Online (Sandbox Code Playgroud)
我想要做的是:将所有值从例如第一个列表从RGB转换为HSV.
我做了这个代码:
import cv2
import numpy as np
for v in low:
rgb = np.uint8([[v]])
print("RGB: ", rgb)
hsv = cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV)
print("HSV: ", hsv)
print("\n")
Run Code Online (Sandbox Code Playgroud)
问题是当我去检查颜色(RGB-HSV)是否相同时.在这里,我发现它不是.
我们从low列表中取最后一个值.
RGB: [[[ 83 104 120]]]
HSV: [[[103 79 120]]]
Run Code Online (Sandbox Code Playgroud)
RGB是RGB输入值,输出HSV.但最后一个它与RGB的颜色不同.首先是蓝色的阴影,最后是绿色.为什么?
我用这个工具来检查值.它还说这个RGB的正确HSV应该是205, 30, 47.
我的错误在哪里?
python ×13
opencv ×12
bounding-box ×4
rgb ×2
tesseract ×2
contour ×1
hsv ×1
matplotlib ×1
mser ×1
ocr ×1