Skip to content

Instantly share code, notes, and snippets.

@sanmaozhao sanmaozhao/line.py
Last active Jan 4, 2019

Embed
What would you like to do?
曲线检测
import numpy as np
import matplotlib.pyplot as plt
import cv2
img = cv2.imread("arrow.png")
# 增加对比度
contrast = np.zeros(img.shape, img.dtype)
alpha = 10.0 # Simple contrast control
beta = 0 # Simple brightness control
for y in range(img.shape[0]):
for x in range(img.shape[1]):
for c in range(img.shape[2]):
contrast[y,x,c] = np.clip(alpha*img[y,x,c] + beta, 0, 255)
# 转成灰度
gray = cv2.cvtColor(contrast,cv2.COLOR_BGR2GRAY)
# 转成二进制(自适应)
ret2,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# 开操作,去掉较小的白色块
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (8,8))
morph_open = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
plt.figure()
plt.subplot(321)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.subplot(322)
plt.imshow(cv2.cvtColor(contrast, cv2.COLOR_BGR2RGB))
plt.subplot(323)
plt.imshow(gray, cmap='gray')
plt.subplot(324)
plt.imshow(binary, cmap='gray')
plt.subplot(325)
plt.imshow(morph_open, cmap='gray')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.