我想读取压缩程度很高的jpeg图像,并将其转换为更清晰的阈值图像,如下所示:
输入图片
获得良好的阈值图像
但是由于jpeg压缩,图像的线条较小,因此阈值的结果是:
阈值图像错误
我想要一个图像,其中也包含较小的线条,那么我该怎么做?
现在我正在使用opencv阈值功能使图像变为黑白,然后使用侵蚀使线条变大。
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, BlackWhite) = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
kernel = np.ones((4, 4),np.uint8)
erosion = cv2.erode(BlackWhite, kernel,iterations = 1)
cv2_imshow(erosion)
答案 0 :(得分:0)
我为您的问题编写了此代码,请尝试将其与u一起使用:)
def auto_canny(image, sigma=0.33):
# compute the median of the single channel pixel intensities
v = np.median(image)
# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(image, lower, upper)
# return the edged image
return edged
image = cv2.imread('asd.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (1,1), 0)
edges = auto_canny(blur)
thresh = cv2.threshold(edges, 3, 255,cv2.ADAPTIVE_THRESH_MEAN_C | cv2.THRESH_OTSU)[1]
cv2.imshow('thres',thresh)
cv2.waitKey()