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%的...

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

 


      # -*- coding: utf-8 -*-
      import os
      import numpy as np
      import cv2
      import cv2
      import numpy as np
      import math
      def stretchImage(data, s=0.005, bins=2000):  # 线性拉伸,去掉最大最小0.5%的像素值,然后线性拉伸至[0,1]
       ht = np.histogram(data, bins);
       d = np.cumsum(ht[0]) / float(data.size)
       lmin = 0;
       lmax = bins - 1
      while lmin < bins:
      if d[lmin] >= s:
      break
       lmin += 1
      while lmax >= 0:
      if d[lmax] <= 1 - s:
      break
       lmax -= 1
      return np.clip((data - ht[1][lmin]) / (ht[1][lmax] - ht[1][lmin]), 0, 1)
      g_para = {}
      def getPara(radius=5):  # 根据半径计算权重参数矩阵
      global g_para
       m = g_para.get(radius, None)
      if m is not None:
      return m
       size = radius * 2 + 1
       m = np.zeros((size, size))
      for h in range(-radius, radius + 1):
      for w in range(-radius, radius + 1):
      if h == 0 and w == 0:
      continue
       m[radius + h, radius + w] = 1.0 / math.sqrt(h ** 2 + w ** 2)
       m /= m.sum()
       g_para[radius] = m
      return m
      def zmIce(I, ratio=4, radius=300):  # 常规的ACE实现
       para = getPara(radius)
       height, width = I.shape
       zh, zw = [0] * radius + list(range(height)) + [height - 1] * radius, [0] * radius + list(range(width)) + [width - 1] * radius
       Z = I[np.ix_(zh, zw)]
       res = np.zeros(I.shape)
      for h in range(radius * 2 + 1):
      for w in range(radius * 2 + 1):
      if para[h][w] == 0:
      continue
       res += (para[h][w] * np.clip((I - Z[h:h + height, w:w + width]) * ratio, -1, 1))
      return res
      def zmIceFast(I, ratio, radius):  # 单通道ACE快速增强实现
       height, width = I.shape[:2]
      if min(height, width) <= 2:
      return np.zeros(I.shape) + 0.5
       Rs = cv2.resize(I, ((width + 1) // 2, (height + 1) // 2))
       Rf = zmIceFast(Rs, ratio, radius)  # 递归调用
       Rf = cv2.resize(Rf, (width, height))
       Rs = cv2.resize(Rs, (width, height))
      return Rf + zmIce(I, ratio, radius) - zmIce(Rs, ratio, radius)
      def zmIceColor(I, ratio=4, radius=3):  # rgb三通道分别增强,ratio是对比度增强因子,radius是卷积模板半径
       res = np.zeros(I.shape)
      for k in range(3):
       res[:, :, k] = stretchImage(zmIceFast(I[:, :, k], ratio, radius))
      return res
      if __name__ == '__main__':
       m = zmIceColor(cv2.imread('sRGB/wu1.jpg') / 255.0) * 255
       cv2.imwrite('zmIce.jpg', m)
       img = np.clip(img, 0, 255)
       img = img.astype(np.uint8)
       cv2.imshow("asdf",img)
       cv2.waitKey(10)
  
 

 


      //ace 自适应对比度均衡研究
      //by jsxyhelu
      //感谢 imageshop
      #include "stdafx.h"
      #include <iostream>
      #include "opencv2/core/core.hpp"
      #include "opencv2/highgui/highgui.hpp"
      #include "opencv2/imgproc/imgproc.hpp"
      using namespace std;
      using namespace cv;
      //点乘法 elementWiseMultiplication
      cv::Mat EWM(cv::Mat m1,cv::Mat m2){
       Mat dst=m1.mul(m2);
      return dst;
      }
      //图像局部对比度增强算法
      cv::Mat ACE(cv::Mat src,int C = 4,int n=20,int MaxCG = 5){
       Mat meanMask;
       Mat varMask;
       Mat meanGlobal;
       Mat varGlobal;
       Mat dst;
       Mat tmp;
       Mat tmp2;
       blur(src.clone(),meanMask,Size(50,50));//meanMask为局部均值 
       tmp = src - meanMask;
       varMask = EWM(tmp,tmp);
       blur(varMask,varMask,Size(50,50)); //varMask为局部方差 
      //换算成局部标准差
       varMask.convertTo(varMask,CV_32F);
      for (int i=0;i<varMask.rows;i++){
      for (int j=0;j<varMask.cols;j++){
       varMask.at<float>(i,j) =  (float)sqrt(varMask.at<float>(i,j));
       }
       }
       meanStdDev(src,meanGlobal,varGlobal); //meanGlobal为全局均值 varGlobal为全局标准差
       tmp2 = varGlobal/varMask;
      for (int i=0;i<tmp2.rows;i++){
      for (int j=0;j<tmp2.cols;j++){
      if (tmp2.at<float>(i,j)>MaxCG){
       tmp2.at<float>(i,j) = MaxCG;
       }
       }
       }
       tmp2.convertTo(tmp2,CV_8U);
       tmp2 = EWM(tmp2,tmp);
       dst = meanMask + tmp2;
       imshow("D方法",dst);
       dst = meanMask + C*tmp;
       imshow("C方法",dst);
      return dst;
      }
      void main()
      {
       Mat src = imread("plant.bmp",0);
       imshow("src",src);
       ACE(src);
       waitKey();
      }
  
 

 

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个月内不可修改。