Last active
January 4, 2019 01:46
-
-
Save sanmaozhao/00e354d2800c9932c2325045eb8b68c3 to your computer and use it in GitHub Desktop.
曲线检测
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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