ace 图像增强

举报
风吹稻花香 发表于 2021/06/05 00:52:31 2021/06/05
【摘要】 有的图像有效果,有的效果不好   # -*- coding: utf-8 -*-import osimport numpy as npimport cv2 import cv2import numpy as npimport math def stretchImage(data, s=0.005, bins=2000): # 线性拉伸,去掉最大最小0.5%的...

有的图像有效果,有的效果不好

 


  
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import numpy as np
  4. import cv2
  5. import cv2
  6. import numpy as np
  7. import math
  8. def stretchImage(data, s=0.005, bins=2000): # 线性拉伸,去掉最大最小0.5%的像素值,然后线性拉伸至[0,1]
  9. ht = np.histogram(data, bins);
  10. d = np.cumsum(ht[0]) / float(data.size)
  11. lmin = 0;
  12. lmax = bins - 1
  13. while lmin < bins:
  14. if d[lmin] >= s:
  15. break
  16. lmin += 1
  17. while lmax >= 0:
  18. if d[lmax] <= 1 - s:
  19. break
  20. lmax -= 1
  21. return np.clip((data - ht[1][lmin]) / (ht[1][lmax] - ht[1][lmin]), 0, 1)
  22. g_para = {}
  23. def getPara(radius=5): # 根据半径计算权重参数矩阵
  24. global g_para
  25. m = g_para.get(radius, None)
  26. if m is not None:
  27. return m
  28. size = radius * 2 + 1
  29. m = np.zeros((size, size))
  30. for h in range(-radius, radius + 1):
  31. for w in range(-radius, radius + 1):
  32. if h == 0 and w == 0:
  33. continue
  34. m[radius + h, radius + w] = 1.0 / math.sqrt(h ** 2 + w ** 2)
  35. m /= m.sum()
  36. g_para[radius] = m
  37. return m
  38. def zmIce(I, ratio=4, radius=300): # 常规的ACE实现
  39. para = getPara(radius)
  40. height, width = I.shape
  41. zh, zw = [0] * radius + list(range(height)) + [height - 1] * radius, [0] * radius + list(range(width)) + [width - 1] * radius
  42. Z = I[np.ix_(zh, zw)]
  43. res = np.zeros(I.shape)
  44. for h in range(radius * 2 + 1):
  45. for w in range(radius * 2 + 1):
  46. if para[h][w] == 0:
  47. continue
  48. res += (para[h][w] * np.clip((I - Z[h:h + height, w:w + width]) * ratio, -1, 1))
  49. return res
  50. def zmIceFast(I, ratio, radius): # 单通道ACE快速增强实现
  51. height, width = I.shape[:2]
  52. if min(height, width) <= 2:
  53. return np.zeros(I.shape) + 0.5
  54. Rs = cv2.resize(I, ((width + 1) // 2, (height + 1) // 2))
  55. Rf = zmIceFast(Rs, ratio, radius) # 递归调用
  56. Rf = cv2.resize(Rf, (width, height))
  57. Rs = cv2.resize(Rs, (width, height))
  58. return Rf + zmIce(I, ratio, radius) - zmIce(Rs, ratio, radius)
  59. def zmIceColor(I, ratio=4, radius=3): # rgb三通道分别增强,ratio是对比度增强因子,radius是卷积模板半径
  60. res = np.zeros(I.shape)
  61. for k in range(3):
  62. res[:, :, k] = stretchImage(zmIceFast(I[:, :, k], ratio, radius))
  63. return res
  64. if __name__ == '__main__':
  65. m = zmIceColor(cv2.imread('sRGB/wu1.jpg') / 255.0) * 255
  66. cv2.imwrite('zmIce.jpg', m)
  67. img = np.clip(img, 0, 255)
  68. img = img.astype(np.uint8)
  69. cv2.imshow("asdf",img)
  70. cv2.waitKey(10)

 


  
  1. //ace 自适应对比度均衡研究
  2. //by jsxyhelu
  3. //感谢 imageshop
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include "opencv2/core/core.hpp"
  7. #include "opencv2/highgui/highgui.hpp"
  8. #include "opencv2/imgproc/imgproc.hpp"
  9. using namespace std;
  10. using namespace cv;
  11. //点乘法 elementWiseMultiplication
  12. cv::Mat EWM(cv::Mat m1,cv::Mat m2){
  13. Mat dst=m1.mul(m2);
  14. return dst;
  15. }
  16. //图像局部对比度增强算法
  17. cv::Mat ACE(cv::Mat src,int C = 4,int n=20,int MaxCG = 5){
  18. Mat meanMask;
  19. Mat varMask;
  20. Mat meanGlobal;
  21. Mat varGlobal;
  22. Mat dst;
  23. Mat tmp;
  24. Mat tmp2;
  25. blur(src.clone(),meanMask,Size(50,50));//meanMask为局部均值
  26. tmp = src - meanMask;
  27. varMask = EWM(tmp,tmp);
  28. blur(varMask,varMask,Size(50,50)); //varMask为局部方差
  29. //换算成局部标准差
  30. varMask.convertTo(varMask,CV_32F);
  31. for (int i=0;i<varMask.rows;i++){
  32. for (int j=0;j<varMask.cols;j++){
  33. varMask.at<float>(i,j) = (float)sqrt(varMask.at<float>(i,j));
  34. }
  35. }
  36. meanStdDev(src,meanGlobal,varGlobal); //meanGlobal为全局均值 varGlobal为全局标准差
  37. tmp2 = varGlobal/varMask;
  38. for (int i=0;i<tmp2.rows;i++){
  39. for (int j=0;j<tmp2.cols;j++){
  40. if (tmp2.at<float>(i,j)>MaxCG){
  41. tmp2.at<float>(i,j) = MaxCG;
  42. }
  43. }
  44. }
  45. tmp2.convertTo(tmp2,CV_8U);
  46. tmp2 = EWM(tmp2,tmp);
  47. dst = meanMask + tmp2;
  48. imshow("D方法",dst);
  49. dst = meanMask + C*tmp;
  50. imshow("C方法",dst);
  51. return dst;
  52. }
  53. void main()
  54. {
  55. Mat src = imread("plant.bmp",0);
  56. imshow("src",src);
  57. ACE(src);
  58. waitKey();
  59. }

 

https://zhuanlan.zhihu.com/p/57666772

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/112303300

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。