原文:http://blog.csdn.net/yeyang911/article/details/18256393

自己测试的效果不是很好

实现函数功能

J = low_out +(high_out - low_out).* ((I - low_in)/(high_in - low_in)).^ gamma

[cpp] view plaincopyprint?
  1. IplImage* ImageAdjust(IplImage *src, IplImage *dst,
  2. double low_in, double high_in,
  3. double low_out, double high_out, double gamma )
  4. {
  5. double low2 = low_in*255;
  6. double high2 = high_in*255;
  7. double bottom2 = low_out*255;
  8. double top2 = high_out*255;
  9. double err_in = high2 - low2;
  10. double err_out = top2 - bottom2;
  11. int x,y;
  12. double val0,val1,val2;
  13. // intensity transform
  14. for( y = 0; y < src->height; y++) {
  15. for (x = 0; x < src->width; x++){
  16. val0 = ((uchar*)(src->imageData+src->widthStep*y))[x*src->nChannels];
  17. val0 = pow((val0 - low2)/err_in,gamma)*err_out+bottom2;
  18. if(val0>255) val0=255;
  19. if(val0<0) val0=0;
  20. ((uchar*)(dst->imageData+dst->widthStep*y))[x*src->nChannels]=(uchar)val0;
  21. val1 = ((uchar*)(src->imageData+src->widthStep*y))[x*src->nChannels+1];
  22. val1 = pow((val1- low2)/err_in, gamma)*err_out+bottom2;
  23. if(val1>255) val1=255;
  24. if(val1<0) val1=0;
  25. ((uchar*)(dst->imageData+dst->widthStep*y))[x*src->nChannels+1]=(uchar)val1;
  26. val2 = ((uchar*)(src->imageData + src->widthStep*y))[x*src->nChannels+2];
  27. val2 = pow((val2-low2)/err_in,gamma)*err_out+bottom2;
  28. if(val2>255) val2=255;
  29. if(val2<0) val2=0; // Make sure src is in the range [low,high]
  30. ((uchar*)(dst->imageData+dst->widthStep*y))[x*src->nChannels+2]=(uchar)val2;
  31. }
  32. }
  33. return 0;
  34. }

测试代码:

[cpp] view plaincopyprint?
  1. int main()
  2. {
  3. IplImage *src=cvLoadImage("d:/111.JPG",1);
  4. CvSize a;
  5. a.width=src->width;
  6. a.height=src->height;
  7. IplImage *dst = cvCreateImage(a,8,3);
  8. ImageAdjust(src, dst,
  9. 0, 0.5,
  10. 0.5, 1, 1)  ;
  11. Mat c = dst;
  12. imshow("ss",c);
  13. waitKey();
  14. return 0;
  15. }

  

效果图 原图

EMGU CV 版本

[csharp] view plaincopyprint?
  1. private void imageAdjust(Image<Bgr,byte>src,Image<Bgr,byte>dst,double low_in, double high_in,
  2. double low_out, double high_out, double gamma)
  3. {
  4. double low2 = low_in * 255;
  5. double high2 = high_in * 255;
  6. double bottom2 = low_out * 255;
  7. double top2 = high_out * 255;
  8. double err_in = high2 - low2;
  9. double err_out = top2 - bottom2;
  10. int x, y;
  11. double val0, val1, val2;
  12. // intensity transform
  13. for (y = 0; y < src.Height; y++)
  14. {
  15. for (x = 0; x < src.Width; x++)
  16. {
  17. val0 = src.Data[y, x, 0];
  18. val0 =   Math.Pow((val0 - low2) / err_in, gamma) * err_out + bottom2;
  19. if (val0 > 255) val0 = 255;
  20. if (val0 < 0) val0 = 0;
  21. dst.Data[y,x,0] = (byte)val0;
  22. val1 = src.Data[y, x, 1];
  23. val1 = Math.Pow((val1 - low2) / err_in, gamma) * err_out + bottom2;
  24. if (val1 > 255) val1 = 255;
  25. if (val1 < 0) val1 = 0;
  26. dst.Data[y, x, 1] = (byte)val1;
  27. val2 = src.Data[y, x, 2];
  28. val2 = Math.Pow((val2 - low2) / err_in, gamma) * err_out + bottom2;
  29. if (val2 > 255) val2 = 255;
  30. if (val2 < 0) val2 = 0; // Make sure src is in the range [low,high]
  31. dst.Data[y, x, 2] = (byte)val2;
  32. }
  33. }
  34. }

上面的代码对于大图运行效率低,下面我写个优化版的

代码如下:

[csharp] view plaincopyprint?
  1. private void imadjust(Image<Gray,byte> src)
  2. {
  3. double minV , maxV ;
  4. int [] minP = new int[2];
  5. int [] maxP = new int[2];
  6. CvInvoke.MinMaxIdx(src, out minV, out maxV, minP, maxP);
  7. Mat m = src.Mat;
  8. m.ConvertTo(m, Emgu.CV.CvEnum.DepthType.Cv32F);
  9. Mat n = new Mat(m.Size,Emgu.CV.CvEnum.DepthType.Cv32F,1);
  10. MCvScalar p = new MCvScalar();
  11. p.V0 = 1.0/(maxV-minV);
  12. n.SetTo(p);
  13. Mat dst = new Mat(m.Size,Emgu.CV.CvEnum.DepthType.Cv32F,1);
  14. CvInvoke.Multiply(m, n, dst);
  15. p.V0 = 255;
  16. n.SetTo(p);//设置矩阵为p.V0 的值
  17. CvInvoke.Multiply(dst, n, dst);
  18. pictureBox1.Image = dst.ToImage<Gray, byte>().ToBitmap();// 显示到pictureBox上
  19. }

都是采用向量化(矢量化)编程方式,效率不会差,但是我是简单实现,假设输出为最小为0 最大值为255的情况,且gamma 为1时的情况

如果需要别的情形需要自己根据情况编写。

这是另一个版本,只能灰度图:

参考:http://stackoverflow.com/questions/31647274/is-there-any-function-equivalent-to-matlabs-imadjust-in-opencv-with-c

网页还有一个别的,可以测试一下

#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
/*
  src and dst are grayscale, 8-bit images;
  Default input value:
           [low, high] = [0,1];  X-Direction
           [bottom, top] = [0,1]; Y-Direction
           gamma ;
  if adjust successfully, return 0, otherwise, return non-zero.
*/
#include <algorithm>
using namespace std;
using namespace cv;
void imadjust(const Mat1b& src, Mat1b& dst, int tol = 1, Vec2i in = Vec2i(0, 255), Vec2i out = Vec2i(0, 255))
{    // src : input CV_8UC1 image
    // dst : output CV_8UC1 imge
    // tol : tolerance, from 0 to 100.
    // in  : src image bounds
    // out : dst image buonds
    dst = src.clone();
    tol = max(0, min(100, tol));
    if (tol > 0)
    {        // Compute in and out limits
        // Histogram
        vector<int> hist(256, 0);
        for (int r = 0; r < src.rows; ++r) {            for (int c = 0; c < src.cols; ++c) {                hist[src(r,c)]++;
            }
        }
        // Cumulative histogram
        vector<int> cum = hist;
        for (int i = 1; i < hist.size(); ++i) {            cum[i] = cum[i - 1] + hist[i];
        }
        // Compute bounds
        int total = src.rows * src.cols;
        int low_bound = total * tol / 100;
        int upp_bound = total * (100-tol) / 100;
        in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
        in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), upp_bound));
    }
    // Stretching
    float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
    for (int r = 0; r < dst.rows; ++r)
    {        for (int c = 0; c < dst.cols; ++c)
        {            int vs = max(src(r, c) - in[0], 0);
            int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
            dst(r, c) = saturate_cast<uchar>(vd);
        }
    }
}
int main()
{    Mat3b img = imread("fish.png");
    Mat1b gray;
    cvtColor(img, gray, COLOR_RGB2GRAY);
    Mat1b adjusted;
    imadjust(gray, adjusted);
    imshow("ss",adjusted);
       waitKey();
    // int low_in, high_in, low_out, high_out
    // imadjust(gray, adjusted, 0, Vec2i(low_in, high_in), Vec2i(low_out, high_out));
    return 0;
}
//int main111( int argc, char** argv )
//{//    IplImage *src=cvLoadImage("fish.png",1);
//       CvSize a;
//       a.width=src->width;
//       a.height=src->height;
//       IplImage *dst = cvCreateImage(a,8,3);
//       ImageAdjust(src, dst,
//             0, 0.5,
//             0.5, 1, 1)  ;
//       Mat gray_image = cvarrToMat(dst);
//       imshow("ss",gray_image);
//       waitKey();
//    return 0;
//}

matlab imadjust 用 opencv改写相关推荐

  1. matlab中caitu_tiqu,车牌识别matlab语音版+OpenCV版+测试图像和设计文档

    [实例简介] 该文件包含了车牌识别的两个版本:matlab语音版+OpenCV版,有丰富的测试实例和文档信息,测试结果能满足大部分的车牌识别. [实例截图] [核心代码] 5d61aaaa-2457- ...

  2. matlab fft2的作用,为什么Matlab fft2比OpenCV dft快得多(why Matlab fft2 is much faster than OpenCV dft)...

    为什么Matlab fft2比OpenCV dft快得多(why Matlab fft2 is much faster than OpenCV dft) 我只是测试比较OpenCV的dft函数和Mat ...

  3. 根据MATLAB的histeq函数改写的运行在OpenCV下的直方图规定化C源码

    图像处理开发需求.图像处理接私活挣零花钱,请加微信/QQ 2487872782 图像处理开发资料.图像处理技术交流请加QQ群,群号 271891601 2016-6-8日用C++也实现了直方图规定化, ...

  4. matlab中调用histeq函数命令,根据MATLAB的histeq函数改写的运行在OpenCV下的直方图规定化源码!...

    据说,图像的直方图规定化比直方图均衡化用得更多,但是很奇怪的是OpenCV居然没有图像直方图规定化的源码!所以,我就有必要在OpenCV下写一个图像直方图规定化处理的函数,以方便将来使用. 我在网上找 ...

  5. 如何把opencv编译到matlab,c – 为OpenCV编译MATLAB绑定

    我正在尝试编译OpenCV 3.0的MATLAB绑定,即 Github的当前版本.我一直收到以下错误: CMake Error at /opencv/modules/matlab/compile.cm ...

  6. 【图像处理】gamma校正通俗解释及python实现(替代matlab imadjust)

    Motivation 之前把matlab代码转python的时候转过这个函数.最近自己动手发现还会用到,遂贴上来方便有需要的朋友们自取. Gamma校正(gamma correction) 一句话解释 ...

  7. 3.6总结半年来的专业学习——图像处理、光栅投影、四步相移、多频外差,从matlab入门到opencv入门

    2019.9.17 研究生导师定了,老师给了一些论文,并推荐了几本书 1.计算机辅助光学测量/金观昌著 作者是在美国奥克兰大学杨老师实验室做过访问学者,所以书的内容和实验室的研究方向非常切合 2.基于 ...

  8. matlab imadjust函数,imadjust从用法到原理—Matlab灰度变换函数之一

    imadjust函数是MATLAB的一个工具箱函数,一般的语法调用格式为: f1=imadjust(f,[low_in  high_in],[low_out  high_out],gamma) (注: ...

  9. matlab imadjust将暗图像分别在RGB与HSV域增加亮度

    对于RGB来说,增加亮度是直接对三个颜色维做变换,而对于HSV来说,增加亮度只需要在V这一维做变换即可.这两种方法得出来的结果并不相同,效果图如下所示. 代码如下: srcImage=imread(' ...

最新文章

  1. python中list作为全局变量无需global声明的原因
  2. 计算机图形学在数学中的应用,计算机图形学的数学工具与C#实现:数学C
  3. halcon边缘检测的方法及各种方法的适用范围
  4. 线程可以kill吗_还在用 kill -9 停机?这才是最优雅的姿势
  5. 浅谈android的am命令
  6. ni max不能连续采集图像_1.6视觉检测项目过程分解——程序的连续运行
  7. 表格如何调出好看的样式?
  8. 固态硬盘计算机怎么自定义分区,固态硬盘分区,详细教您固态硬盘怎么分区
  9. 棋盘中正方形,长方形个数
  10. 在excel中如何筛选重复数据_Excel快速筛选数据方法集锦
  11. 基于微信小程序的药店管理系统毕业设计
  12. 如何在Mac Finder中查找/Usr 路径?
  13. c语言汇率转换代码_基于C语言实现的货币转换器.doc
  14. 整理--linux设备驱动模型
  15. QEMU文档之虚拟NVDIMM
  16. JS获取跨域的cookie实例
  17. cogs 1341 永无乡
  18. pythonmultiprocessing之 queue线程_python中的进程、线程(threading、multiprocessing、Queue、subprocess)...
  19. 阿里云配置https,配置SSL证书
  20. Windows Server 2008 R2 活动目录的安装和配置

热门文章

  1. 沾包 nagle算法等
  2. jackson 反序列化string_Java 中使用Jackson反序列化
  3. MySQL数据库开发理念_mysql之数据库基本理念
  4. 软件测试_单元测试反模式,完整列表
  5. 基于嵌入式linux 的车载定位系统设计,基于嵌入式Linux的GPS车载定位导航系统设计...
  6. 网传:Vue涉及国家安全漏洞?尤雨溪亲自发文回应!
  7. 2021年假期怎么放?都给你安排得明明白白!
  8. 真相!没项目经验高薪就无望?
  9. 共享资源的保护:锁机制
  10. 自律到极致-人生才精致:第5期