笔者刚开始学习opencv几天,简单写一些常见的处理函数,以便以后查询使用
代码
# 读图
img = cv2.imread("C://Users//intellectual_yao//Desktop//new_04.jpg")
# 转为灰度图
cvt_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.namedWindow("cvt_img", 0)
cv2.imshow("cvt_img", cvt_img)
# 二值化
ref, threshold_img = cv2.threshold(cvt_img, 127, 255, cv2.THRESH_BINARY_INV)
cv2.namedWindow("threshold_img", 0)
cv2.imshow("threshold_img", threshold_img)
#轮廓检测
refCnts, hierarchy = cv2.findContours(threshold_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, refCnts, -1, (0, 0, 255), 3)
cv2.namedWindow("refCnts", 0)
cv2.imshow("refCnts", img)
cv2.waitKey(0)
检测结果如下图所示