当前位置:   article > 正文

python opencv彩色图像自适应直方图均衡化_clahe.apply(channels[0], channels[0])

clahe.apply(channels[0], channels[0])
  1. import numpy as np
  2. import cv2 as cv
  3. # 彩色图像进行自适应直方图均衡化
  4. def hisEqulColor(img):
  5. ## 将RGB图像转换到YCrCb空间中
  6. ycrcb = cv.cvtColor(img, cv.COLOR_BGR2YCR_CB)
  7. # 将YCrCb图像通道分离
  8. channels = cv.split(ycrcb)
  9. # 以下代码详细注释见官网:
  10. # https://docs.opencv.org/4.1.0/d5/daf/tutorial_py_histogram_equalization.html
  11. clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
  12. clahe.apply(channels[0], channels[0])
  13. cv.merge(channels, ycrcb)
  14. cv.cvtColor(ycrcb, cv.COLOR_YCR_CB2BGR, img)
  15. return img
  16. img = cv.imread(r'C:\Users\thorne\PycharmProjects\biyesheji\image\2.jpeg')
  17. img1 = img.copy()
  18. #自适应直方图均衡化后的图res1
  19. res1 = hisEqulColor(img1)
  20. #拼接图res
  21. res = np.hstack((img, res1))
  22. #例图太大了,缩小一下
  23. #正常显示的话就是cv.imshow('img+img1',res)
  24. img_test2=cv.resize(res, (0, 0), fx=0.5, fy=0.5, interpolation=cv.INTER_NEAREST)
  25. cv.imshow('img+img1',img_test2)
  26. cv.waitKey(0)

运行结果:


补充:还有全局自适应直方图均衡化

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import cv2 as cv
  4. # 彩色图像全局直方图均衡化
  5. def hisEqulColor1(img):
  6. # 将RGB图像转换到YCrCb空间中
  7. ycrcb = cv.cvtColor(img, cv.COLOR_BGR2YCR_CB)
  8. # 将YCrCb图像通道分离
  9. channels = cv.split(ycrcb)
  10. # 对第1个通道即亮度通道进行全局直方图均衡化并保存
  11. cv.equalizeHist(channels[0], channels[0])
  12. # 将处理后的通道和没有处理的两个通道合并,命名为ycrcb
  13. cv.merge(channels, ycrcb)
  14. # 将YCrCb图像转换回RGB图像
  15. cv.cvtColor(ycrcb, cv.COLOR_YCR_CB2BGR, img)
  16. return img
  17. img = cv.imread(r'C:\Users\thorne\PycharmProjects\biyesheji\image\2.jpeg')
  18. img1 = img.copy()
  19. #全局自适应直方图均衡化
  20. res1 = hisEqulColor1(img1)
  21. #例图太大了,缩小一下
  22. #正常显示的话就是cv.imshow('img1',res1)
  23. img_test=cv.resize(res1, (0, 0), fx=0.5, fy=0.5, interpolation=cv.INTER_NEAREST)
  24. cv.imshow('img1',img_test)
  25. cv.waitKey(0)

运行结果:(我就显示了全局均衡化后的图) 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号