OpenCV(2)——基本图像处理

1. 灰度处理图像

函数接口:cv2.cvtColor(存储彩色图像的三维数组,cv2.COLOR_BGR2GRAY)

# 灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('Lenna_gray.png', img_gray)

2. 二值化图像

2.1 固定阈值法

函数接口:cv2.thresholed(原图像数组, 固定二值化阈值, 像素最大值255, 二值化类型的选择)

  • @param type:
    cv2.THRESH_BINARY(黑白二值)
    cv2.THRESH_BINARY_INV(黑白二值反转)
    cv2.THRESH_TRUNC (得到的图像为多像素值)
    cv2.THRESH_TOZERO
    cv2.THRESH_TOZERO_INV
  • @return:阈值大小, 二值化后图像数组
# 二值化
_, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) #_, 表示不需要返回的第一个参数
cv2.imwrite('Lenna_bin.png', img_bin)

2.2 自适应阈值

函数接口:cv2.adaptiveThreshold(灰度图像数组, 像素最大值, 自适应阈值算法的选择, 二值化类型的选择, blockSize, C)
函数功能:把图片每个像素点作为中心取N*N的区域,然后计算这个区域的阈值,来决定这个像素点变0还是变255

  • @param:src Source 8-bit single-channel image.(单通道图像,灰度图)
  • @param:maxValue Non-zero value assigned to the pixels for which the condition is satisfied
  • @param:adaptiveMethod Adaptive thresholding algorithm to use
    • AdaptiveThresholdTypes:
    • ADAPTIVE_THRESH_MEAN_C
    • ADAPTIVE_THRESH_GAUSSIAN_C
  • @param:thresholdType Thresholding type that must be either #THRESH_BINARY or #THRESH_BINARY_INV
  • @param:blockSize 一般为奇数
  • @param:C:常数,得到的阈值-C
  • @return:Destination image of the same size and the same type as src.(返回二值化图像)
# import the libraries
import numpy as np
import matplotlib.pyplot as plt
import cv2

# ADAPTIVE THRESHOLDING
gray_image = cv2.imread('578.jpg', 0)

ret, thresh_global = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
# here 11 is the pixel neighbourhood that is used to calculate the threshold value
thresh_mean = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

thresh_gaussian = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

names = ['Original Image', 'Global Thresholding', 'Adaptive Mean Threshold', 'Adaptive Gaussian Thresholding']
images = [gray_image, thresh_global, thresh_mean, thresh_gaussian]

for i in range(4):
    plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray')
    plt.title(names[i])
    plt.xticks([]), plt.yticks([])

plt.show()

3. 模糊图像

# 平滑
img_blur = cv2.blur(img, (5, 5))
cv2.imwrite('Lenna_blur.png', img_blur)

4. 边缘检测

4.1 Canny边缘检测

函数接口:cv2.Canny(图像数组, minVal, maxVal)

  • @param:minVal,低于这个阈值的点会被抛弃
  • @param:maxVal,高于这个阈值的点会被保留
  • @return:边缘检测完成的图像数组
# 边缘检测
edges = cv2.Canny(img,100,200)
# 显示图像
cv2.imshow('picture show', edges)
cv2.waitKey(0)

滞后阈值:
现在要确定那些边界才是真正的边界,需要设置两个阈值:minVal和maxVal。当图像的灰度梯度高于maxVal时被认为是真的边界,那些低于minVal的边界会被抛弃。如果介于两者之间的话,就要看这个点是否与某个被确定为真正边界点相连,如果是,就认为它也是边界点,如果不是就抛弃。

4.2 根据二值化图像边缘提取

函数接口:cv2.findContours(二值化图像数组, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

  • @return:
    1. contours:存放图像轮廓的列表
    2. hierarchy:层次结构
# 边缘提取
contours, _ = cv2.findContours(img_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 描绘轮廓
img = cv2.drawContours(img, contours, -1, (0, 255, 0), 5)  # img为三通道才能显示轮廓

参考资料

【建议收藏】16个OpenCV函数开始你的计算机视觉之旅
opencv二值化的cv2.threshold函数
opencv3 学习笔记(Canny边缘检测:cv2.Canny())

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值