cv2 数字图像后处理
cv2 digit-image postprocessing
我正在尝试自己实现数字分类器。我遇到了一些麻烦。我在 MNIST 手写数据集上训练 NN,MNIST sample digit. But when I'm trying to predict what the digit is, I predicted from the image that I found and processed using cv2 - cv2 processed digit,正如你所看到的,我自己的图像具有更胖的电路和清晰的边界。
这是我处理前的数字图像 - Before, and after - After. But I want to image be like this。
处理后。
我使用以下代码来处理每个数字:
def main():
image = cv2.imread('digit.jpg', cv2.IMREAD_GRAYSCALE)
image = image.reshape((32,32,1))
image = postprocess(image)
def postprocess(gray):
kernel_size = 15
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size), 0)
thresh = cv2.adaptiveThreshold(blur_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 3)
return thresh
我使用 11 作为阈值参数来丢弃大部分伪像,但我的数字电路仍然是 bold/fat 并且边界太清晰。
问题是:如何处理 image to make it look like a training sample image(较厚且边界模糊)?
我通过使用 内核过滤 找到了解决我的问题的方法。今天我偶然发现了一个article about kernel image processing,里面有一些"Edge detection"的内核,我都试过了,但没有一个足够好。但是我由于疏忽而做了我自己的内核,它对我来说非常有效!
所以,有代码:
def main():
image = cv2.imread('digit.jpg', cv2.IMREAD_GRAYSCALE)
image = postprocess(image)
image = image.reshape((32,32,1))
def postprocess(gray):
gray_big = cv2.resize(gray, (256,256))
kernel = np.array([[0,-2,0],[-2,10,-2],[0,-2,0]])
filtered = cv2.filter2D(gray_big, -1, kernel)
filtered = 255 - filtered
filtered = filtered / 255
filtered = cv2.resize(filtered, (32,32))
return filtered
在内核处理后将图像调整到更大的尺寸可以使图像在 "artifacts" 中保持清晰,我尝试在原始图像尺寸上进行,但图像不如在我的最终代码版本。
我正在尝试自己实现数字分类器。我遇到了一些麻烦。我在 MNIST 手写数据集上训练 NN,MNIST sample digit. But when I'm trying to predict what the digit is, I predicted from the image that I found and processed using cv2 - cv2 processed digit,正如你所看到的,我自己的图像具有更胖的电路和清晰的边界。
这是我处理前的数字图像 - Before, and after - After. But I want to image be like this。 处理后。 我使用以下代码来处理每个数字:
def main():
image = cv2.imread('digit.jpg', cv2.IMREAD_GRAYSCALE)
image = image.reshape((32,32,1))
image = postprocess(image)
def postprocess(gray):
kernel_size = 15
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size), 0)
thresh = cv2.adaptiveThreshold(blur_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 3)
return thresh
我使用 11 作为阈值参数来丢弃大部分伪像,但我的数字电路仍然是 bold/fat 并且边界太清晰。
问题是:如何处理 image to make it look like a training sample image(较厚且边界模糊)?
我通过使用 内核过滤 找到了解决我的问题的方法。今天我偶然发现了一个article about kernel image processing,里面有一些"Edge detection"的内核,我都试过了,但没有一个足够好。但是我由于疏忽而做了我自己的内核,它对我来说非常有效! 所以,有代码:
def main():
image = cv2.imread('digit.jpg', cv2.IMREAD_GRAYSCALE)
image = postprocess(image)
image = image.reshape((32,32,1))
def postprocess(gray):
gray_big = cv2.resize(gray, (256,256))
kernel = np.array([[0,-2,0],[-2,10,-2],[0,-2,0]])
filtered = cv2.filter2D(gray_big, -1, kernel)
filtered = 255 - filtered
filtered = filtered / 255
filtered = cv2.resize(filtered, (32,32))
return filtered
在内核处理后将图像调整到更大的尺寸可以使图像在 "artifacts" 中保持清晰,我尝试在原始图像尺寸上进行,但图像不如在我的最终代码版本。