1. 灰度处理图像
函数接口:cv2.cvtColor(存储彩色图像的三维数组,cv2.COLOR_BGR2GRAY)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('Lenna_gray.png', img_gray)
2. 二值化图像
_, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('Lenna_bin.png', img_bin)
3. 模糊图像
img_blur = cv2.blur(img, (5, 5))
cv2.imwrite('Lenna_blur.png', img_blur)
4. 边缘检测
_, contours, _ = cv2.findContours(img_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_cont = np.zeros(img_bin.shape, np.uint8)
cv2.drawContours(img_cont, contours, -1, 255, 3)
cv2.imwrite('Lenna_cont.png', img_cont)
参考资料
【建议收藏】16个OpenCV函数开始你的计算机视觉之旅
|