图像增强(对数,指数,曝光,马赛克)

#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream> 

using namespace std;
using namespace cv;

//对数图像增强是图像增强的一种常见方法,其公式为: S = c log(r+1),其中c是常数(以下算法c=255/(log(256)),这样可以实现整个画面的亮度增大。
void LogEnhance(IplImage* img, IplImage* dst)
{
 // 由于oldPixel:[1,256],则可以先保存一个查找表 
 uchar lut[256] = { 0 };

 double temp = 255 / log(256);

 for (int i = 0; i<255; i++)
 {
  lut[i] = (uchar)(temp* log(i + 1) + 0.5);
 }

 for (int row = 0; row <img->height; row++)
 {
  uchar *data = (uchar*)img->imageData + row* img->widthStep;
  uchar *dstData = (uchar*)dst->imageData + row* dst->widthStep;

  for (int col = 0; col<img->width; col++)
  {
   for (int k = 0; k<img->nChannels; k++)
   {
    uchar t1 = data[col*img->nChannels + k];
    dstData[col*img->nChannels + k] = lut[t1];
   }
  }
 }
}

//指数图像增强的表达为:S = cR^r,通过合理的选择c和r可以压缩灰度范围,算法以c=1.0/255.0, r=2实现。
void ExpEnhance(IplImage* img, IplImage* dst)
{
 // 由于oldPixel:[1,256],则可以先保存一个查找表 
 uchar lut[256] = { 0 };

 double temp = 1.0 / 255.0;

 for (int i = 0; i<255; i++)
 {
  lut[i] = (uchar)(temp*i*i + 0.5);
 }

 for (int row = 0; row <img->height; row++)
 {
  uchar *data = (uchar*)img->imageData + row* img->widthStep;
  uchar *dstData = (uchar*)dst->imageData + row* dst->widthStep;

  for (int col = 0; col<img->width; col++)
  {
   for (int k = 0; k<img->nChannels; k++)
   {
    uchar t1 = data[col*img->nChannels + k];
    dstData[col*img->nChannels + k] = lut[t1];
   }
  }
 }
}

// 在日常中有时候保密或其他需要将图像马赛克,下面的算法实现图像马赛克功能(原理:用中心像素来表示邻域像素)。
uchar getPixel(IplImage* img, int row, int col, int k)
{
 return ((uchar*)img->imageData + row* img->widthStep)[col*img->nChannels + k];
}

void setPixel(IplImage* img, int row, int col, int k, uchar val)
{
 ((uchar*)img->imageData + row* img->widthStep)[col*img->nChannels + k] = val;
}

// nSize:为尺寸大小,奇数 
// 将邻域的值用中心像素的值替换 
void Masic(IplImage* img, IplImage* dst, int nSize)
{
 int offset = (nSize - 1) / 2;
 for (int row = offset; row <img->height - offset; row = row + offset)
 {
  for (int col = offset; col<img->width - offset; col = col + offset)
  {
   int val0 = getPixel(img, row, col, 0);
   int val1 = getPixel(img, row, col, 1);
   int val2 = getPixel(img, row, col, 2);
   for (int m = -offset; m<offset; m++)
   {
    for (int n = -offset; n<offset; n++)
    {
     setPixel(dst, row + m, col + n, 0, val0);
     setPixel(dst, row + m, col + n, 1, val1);
     setPixel(dst, row + m, col + n, 2, val2);
    }
   }
  }
 }
}

// 对于曝光过度问题,可以通过计算当前图像的反相(255-image),然后取当前图像和反相图像的较小者为当前像素位置的值。
// 过度曝光原理:图像翻转,然后求原图与反图的最小值
void ExporeOver(IplImage* img, IplImage* dst)
{
 for (int row = 0; row <img->height; row++)
 {
  uchar *data = (uchar*)img->imageData + row* img->widthStep;
  uchar *dstData = (uchar*)dst->imageData + row* dst->widthStep;
  for (int col = 0; col<img->width; col++)
  {
   for (int k = 0; k<img->nChannels; k++)
   {
    uchar t1 = data[col*img->nChannels + k];
    uchar t2 = 255 - t1;
    dstData[col*img->nChannels + k] = min(t1, t2);
   }
  }
 }
}


//高反差保留主要是将图像中颜色、明暗反差较大两部分的交界处保留下来,其表达形式为:dst = r*(img - Blur(img))。
//比如图像中有一个人和一块石头,那么石头的轮廓线和人的轮廓线以及面部、服装等有明显线条的地方会变被保留,而其他大面积无明显明暗变化的地方则生成中灰色。
Mat HighPass(Mat img)
{
 Mat temp;
 GaussianBlur(img, temp, Size(7, 7), 1.6, 1.6);

 int r = 3;
 Mat diff = img + r*(img - temp); //高反差保留算法 
 return diff;
}


int main(int argc, char* argv[])
{
 const char* Path = "2.jpg";
 IplImage *img = cvLoadImage(Path, CV_LOAD_IMAGE_ANYCOLOR);
 IplImage *dst = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
 cout << "输入你要选择的操作:" << endl;
 cout << "1、曝光过度" << endl;
 cout << "2、加马赛克" << endl;
 cout << "3、对数增强" << endl;
 cout << "4、指数增强" << endl;
 cout << "请输入你的选择:";
 int choice = 1;
 cin >> choice;
 switch (choice)
 {
 case 1:
  ExporeOver(img, dst);   //这四个算法中总觉得某个算法有问题 
  break;
 case 2:
  Masic(img, dst, 21);
  break;
 case 3:
  LogEnhance(img, dst);
  break;
 case 4:
  ExpEnhance(img, dst);
  break;
 default:
  cout << "输入错误" << endl;
  break;
 }
 cvSaveImage("dst.jpg", dst);
 cvNamedWindow("SRC", 1);
 cvNamedWindow("DST", 1);
 cvShowImage("SRC", img);
 cvShowImage("DST", dst);
 cvWaitKey();
 return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值