【Python】OpenCV库学习笔记(六)

红太狼 2023-01-08 10:26 200阅读 0赞

OpenCV库学习笔记(六)

    1. 图像二值化
    • 1.1 二值图像(Binary Image)
    • 1.2 图像二值化方法
    • 1.3 OpenCV相关API
      • 1.3.1 OTSU
      • 1.3.2 Triangle
      • 1.3.3 自动与手动
      • 1.3.4 局部阈值
    1. 图像金字塔
    • 图像金字塔原理
    • 高斯金字塔和拉普拉斯金字塔

1. 图像二值化

1.1 二值图像(Binary Image)

(1)二值图像的概念

  • 一张图像只有黑白两个颜色
  • 下面图像是就是一个二值图像,其中的1代表黑色,其中的0代表

在这里插入图片描述

(2)二值图像的存储

  • 使用四叉树存储平面图像

在这里插入图片描述

1.2 图像二值化方法

图像二值化的方法:

  1. 全局阈值
  2. 局部阈值

在这里插入图片描述

1.3 OpenCV相关API

1.3.1 OTSU

  1. import cv2 as cv
  2. import numpy
  3. def threshold_demo(image):
  4. gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
  5. ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
  6. print("threshold value %s" % ret)
  7. cv.imshow("Binary IMage", binary)
  8. image = cv.resize(cv.imread("01.jpg"), (300,400))
  9. cv.imshow("image", image)
  10. threshold_demo(image)
  11. cv.waitKey(0)
  12. cv.destroyAllWindows()
  13. threshold value 171.0

在这里插入图片描述

1.3.2 Triangle

  1. import cv2 as cv
  2. import numpy
  3. def threshold_demo(image):
  4. gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
  5. ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
  6. print("threshold value %s" % ret)
  7. cv.imshow("Binary IMage", binary)
  8. image = cv.resize(cv.imread("01.jpg"), (300,400))
  9. cv.imshow("image", image)
  10. threshold_demo(image)
  11. cv.waitKey(0)
  12. cv.destroyAllWindows()
  13. threshold value 189.0

在这里插入图片描述

1.3.3 自动与手动

  1. import cv2 as cv
  2. import numpy
  3. def threshold_demo(image):
  4. gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
  5. ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_BINARY)
  6. print("threshold value %s" % ret)
  7. cv.imshow("Binary IMage", binary)
  8. image = cv.resize(cv.imread("01.jpg"), (300,400))
  9. cv.imshow("image", image)
  10. threshold_demo(image)
  11. cv.waitKey(0)
  12. cv.destroyAllWindows()
  13. threshold value 150.0

在这里插入图片描述

1.3.4 局部阈值

  • 使用ADAPTIVE_THRESH_MEAN_C

    import cv2 as cv
    import numpy

    def local_threshold_demo(image):

    1. gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    2. binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 25, 10)
    3. cv.imshow("Binary IMage", binary)

    image = cv.resize(cv.imread(“01.jpg”), (300,400))
    cv.imshow(“image”, image)
    local_threshold_demo(image)
    cv.waitKey(0)
    cv.destroyAllWindows()

在这里插入图片描述

  • 使用ADAPTIVE_THRESH_GAUSSIAN_C

    import cv2 as cv
    import numpy

    def local_threshold_demo(image):

    1. gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    2. binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)
    3. cv.imshow("Binary IMage", binary)

    image = cv.resize(cv.imread(“01.jpg”), (300,400))
    cv.imshow(“image”, image)
    local_threshold_demo(image)
    cv.waitKey(0)
    cv.destroyAllWindows()

在这里插入图片描述

2. 图像金字塔

图像金字塔原理

  • 图像金字塔是图像多尺度表达的一种,是一种以多分辨率来解释图像的有效但概念简单的结构。
  • 一幅图像的金字塔是一系列以金字塔形状排列的分辨率逐步降低,且来源于同一张原始图的图像集合。其通过梯次向下采样获得,直到达到某个终止条件才停止采样。
  • 我们将一层一层的图像比喻成金字塔,层级越高,则图像越小,分辨率越低。

在这里插入图片描述

高斯金字塔和拉普拉斯金字塔

  • Reduce:先对图像进行高斯模糊,再取出所有行和列中的偶数行和偶数列进行降采样

在这里插入图片描述

  • Expand:先对图像进行扩大,再对扩大的图像进行卷积

    import cv2 as cv

  1. def pyramid_demo(image):
  2. level = 3
  3. temp = image.copy()
  4. pyramid_images = []
  5. for i in range(level):
  6. dst = cv.pyrDown(temp)
  7. pyramid_images.append(dst)
  8. cv.imshow("pyramid_down_" + str(i), dst)
  9. temp = dst.copy()
  10. return pyramid_images
  11. def ladpalian_demo(image):
  12. pyramid_images = pyramid_demo(image)
  13. level = len(pyramid_images)
  14. for i in range(level - 1, -1, -1):
  15. if (i - 1) < 0:
  16. expand = cv.pyrUp(pyramid_images[i], dstsize=image.shape[:2])
  17. lpls = cv.subtract(image, expand)
  18. cv.imshow("laplalian_down_" + str(i), lpls)
  19. else:
  20. expand = cv.pyrUp(pyramid_images[i], dstsize=pyramid_images[i - 1].shape[:2])
  21. lpls = cv.subtract(pyramid_images[i - 1], expand)
  22. cv.imshow("laplalian_down_" + str(i), lpls)
  23. image = cv.imread("01.jpg")
  24. pyramid_demo(image)
  25. cv.waitKey(0)
  26. cv.destroyAllWindows()
  • pyramid_demo

在这里插入图片描述

  • ladpalian_demo
    在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,200人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++学习笔记)函数

      C++除了使用库函数,还可以自定义函数来完成一些功能。 函数可以分为有返回值的函数和无返回值的函数,没有返回值的通用格式 void 函数名(参数列表) \{ //