<jupyter_start><jupyter_text>非局部平均去噪：[link1](https://blog.csdn.net/qq_38410428/article/details/93046099)、[link2](https://zh.wikipedia.org/wiki/%E9%9D%9E%E5%B1%80%E9%83%A8%E5%B9%B3%E5%9D%87)<jupyter_code># load original image
img = cv2.imread('D:/Python_Project/dataset/THSR_captcha/F5Q9.jpg')  # 讀取 RGB 三個 channels 的彩色圖片，而忽略透明度的 channel
print(img)
print(img.shape)
print(img.size)
plt.imshow(img)
plt.axis('on')
plt.title('sample image')
plt.show()
# denoise1
img1 = cv2.fastNlMeansDenoisingColored(img, None, 3, 3, 7, 21)  # default setting
print(img1.shape)
print(img1.size)
plt.imshow(img1)
plt.axis('on')
plt.title('denoise1 image')
plt.show()
# denoise2
img2 = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)  # 參數數值再自己挑看看
print(img2.shape)
print(img2.size)
plt.imshow(img2)
plt.axis('on')
plt.title('denoise2 image')
plt.show()
# denoise3
img3 = cv2.fastNlMeansDenoisingColored(img, None, 20, 20, 7, 21)  # 參數數值再自己挑看看
print(img3.shape)
print(img3.size)
plt.imshow(img3)
plt.axis('on')
plt.title('denoise3 image')
plt.show()
# denoise4
img4 = cv2.fastNlMeansDenoisingColored(img, None, 30, 30, 7, 21)  # 參數數值再自己挑看看
print(img4.shape)
print(img4.size)
plt.imshow(img4)
plt.axis('on')
plt.title('denoise4 image')
plt.show()
# denoise5
img5 = cv2.fastNlMeansDenoisingColored(img, None, 30, 30, 7, 25)  # 參數數值再自己挑看看
print(img5.shape)
print(img5.size)
plt.imshow(img5)
plt.axis('on')
plt.title('denoise5 image')
plt.show()
# denoise6
img6 = cv2.fastNlMeansDenoisingColored(img, None, 30, 30, 7, 31)  # 參數數值再自己挑看看
print(img6)
print(img6.shape)
print(img6.size)
plt.imshow(img6)
plt.axis('on')
plt.title('denoise6 image')
plt.show()<jupyter_output>[[[206 206 206]
  [206 206 206]
  [206 206 206]
  ...
  [204 204 204]
  [204 204 204]
  [204 204 204]]

 [[206 206 206]
  [206 206 206]
  [206 206 206]
  ...
  [204 204 204]
  [204 204 204]
  [204 204 204]]

 [[206 206 206]
  [206 206 206]
  [206 206 206]
  ...
  [202 202 202]
  [199 200 200]
  [199 200 200]]

 ...

 [[204 204 204]
  [204 204 204]
  [204 204 204]
  ...
  [202 202 202]
  [202 202 202]
  [204 204 204]]

 [[204 204 204]
  [204 204 204]
  [204 204 204]
  ...
  [204 204 204]
  [202 202 202]
  [204 204 204]]

 [[206 206 206]
  [204 204 204]
  [206 206 206]
  ...
  [202 202 202]
  [204 204 204]
  [202 202 202]]]
(52, 120, 3)
18720<jupyter_text>影像處理二值化：透過閥值對像素值篩選使原圖成為二值圖(黑白效果)  [Link1](https://zh.wikipedia.org/wiki/%E7%81%B0%E5%BA%A6%E5%9B%BE%E5%83%8F)、[Link2](https://smasoft-tech.com/%e4%bb%80%e9%ba%bc%e6%98%af%e5%bd%b1%e5%83%8f%e5%89%8d%e8%99%95%e7%90%86/)、[Link3](https://blog.csdn.net/u012566751/article/details/77046445)、[Link4](https://blog.csdn.net/on2way/article/details/46812121)<jupyter_code>ret,threshold = cv2.threshold(img6,127,255,cv2.THRESH_BINARY_INV)
print(ret)
print(threshold)
print(threshold.shape)
print(threshold.size)
plt.imshow(threshold)
plt.axis('on')
plt.title('binary image')
plt.show()<jupyter_output>127.0
[[[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 ...

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]]
(52, 120, 3)
18720<jupyter_text>轉換色彩空間<jupyter_code>image6_gray = cv2.cvtColor(threshold,cv2.COLOR_BGR2GRAY)  # convert RGB to Grayscale image.
height, width = image6_gray.shape
print(image6_gray)
print(image6_gray.shape)
print(image6_gray.size)
plt.imshow(image6_gray)
plt.axis('on')
plt.title('grayscale image')
plt.show()<jupyter_output>[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]
(52, 120)
6240<jupyter_text>去除弧線<jupyter_code>image6_gray[:,5:width-5] = 0
image_idx = where(image6_gray == 255)
print('x-axis: shape={} index={} '.format(image_idx[1].shape, image_idx[1]))
print('y-axis: shape={} index={} '.format(image_idx[0].shape, image_idx[0]))
# 影像的座標(0,0)於左上角，作圖時的座標(0,0)於左下角。
plt.scatter(image_idx[1], height - image_idx[0], s=10, c='red')
plt.ylim(ymin=0, ymax=height)
plt.show()
xx = array([image_idx[1]])
yy = height - image_idx[0]
print(xx.shape, yy.shape)
poly_reg= PolynomialFeatures(degree = 2)
l_reg = LinearRegression(n_jobs=-1)
xx_poly = poly_reg.fit_transform(xx.T)
l_reg.fit(xx_poly, yy)
xx2 = array([[i for i in range(width)]])
xx2_poly = poly_reg.fit_transform(xx2.T)
plt.scatter(xx,yy, s=10, color="black")
plt.ylim(ymin=0, ymax=height)
plt.plot(xx2.T, l_reg.predict(xx2_poly), color="blue", linewidth=5)
plt.show()
print('Coefficient:{}'.format(l_reg.coef_) )
print('Intercept:{}'.format(l_reg.intercept_) )
# newimg = cv2.resize(image6_gray, (140, 48), interpolation=cv2.INTER_CUBIC)
newimg = cv2.cvtColor(threshold,cv2.COLOR_BGR2GRAY)
for ele in column_stack([l_reg.predict(xx2_poly).round(0), xx2[0]] ):
    # print(ele)
    pos = height - int(ele[0])
    # if newimg[pos-4:pos+4,int(ele[1])] == 255:
    # newimg[pos-3:pos+3,int(ele[1])] = 0 
    newimg[pos-3: pos+3,int(ele[1])] = 255 - newimg[pos-3: pos+3,int(ele[1])]
plt.subplot(121)
plt.imshow(threshold)
plt.subplot(122)
plt.imshow(newimg)
plt.show()
# resize
image = cv2.resize(newimg, (140, 48), interpolation=cv2.INTER_CUBIC)
print(image)
print(image.shape)
print(image.size)
plt.imshow(image)
plt.axis('on')
plt.title('image')
plt.show()<jupyter_output>[[ 0  0  0 ...  0  0  0]
 [ 0  0  0 ...  0  0  0]
 [ 0  0  0 ... 54 54 54]
 ...
 [ 0  0  0 ...  0  0  0]
 [ 0  0  0 ...  0  0  0]
 [ 0  0  0 ...  0  0  0]]
(48, 140)
6720
{'fraction_non_alphanumeric': 0.19504809405058782, 'fraction_numerical': 0.14998218738867117, 'mean_word_length': 3.9822537710736468, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '469932', 'license': 'MIT', 'n_tokens_mistral': 3351, 'n_tokens_neox': 2779, 'n_words': 549, 'path': 'image_preprocessing.ipynb', 'repo_name': 'hyiche/THSR_Captcha_Recognition', 'size': '5614'}