matlab imadjust 用 opencv改写
原文:http://blog.csdn.net/yeyang911/article/details/18256393
自己测试的效果不是很好
实现函数功能
J = low_out +(high_out - low_out).* ((I - low_in)/(high_in - low_in)).^ gamma
- IplImage* ImageAdjust(IplImage *src, IplImage *dst,
- double low_in, double high_in,
- double low_out, double high_out, double gamma )
- {
- double low2 = low_in*255;
- double high2 = high_in*255;
- double bottom2 = low_out*255;
- double top2 = high_out*255;
- double err_in = high2 - low2;
- double err_out = top2 - bottom2;
- int x,y;
- double val0,val1,val2;
- // intensity transform
- for( y = 0; y < src->height; y++) {
- for (x = 0; x < src->width; x++){
- val0 = ((uchar*)(src->imageData+src->widthStep*y))[x*src->nChannels];
- val0 = pow((val0 - low2)/err_in,gamma)*err_out+bottom2;
- if(val0>255) val0=255;
- if(val0<0) val0=0;
- ((uchar*)(dst->imageData+dst->widthStep*y))[x*src->nChannels]=(uchar)val0;
- val1 = ((uchar*)(src->imageData+src->widthStep*y))[x*src->nChannels+1];
- val1 = pow((val1- low2)/err_in, gamma)*err_out+bottom2;
- if(val1>255) val1=255;
- if(val1<0) val1=0;
- ((uchar*)(dst->imageData+dst->widthStep*y))[x*src->nChannels+1]=(uchar)val1;
- val2 = ((uchar*)(src->imageData + src->widthStep*y))[x*src->nChannels+2];
- val2 = pow((val2-low2)/err_in,gamma)*err_out+bottom2;
- if(val2>255) val2=255;
- if(val2<0) val2=0; // Make sure src is in the range [low,high]
- ((uchar*)(dst->imageData+dst->widthStep*y))[x*src->nChannels+2]=(uchar)val2;
- }
- }
- return 0;
- }
测试代码:
- int main()
- {
- IplImage *src=cvLoadImage("d:/111.JPG",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 c = dst;
- imshow("ss",c);
- waitKey();
- return 0;
- }
效果图 原图
EMGU CV 版本
- private void imageAdjust(Image<Bgr,byte>src,Image<Bgr,byte>dst,double low_in, double high_in,
- double low_out, double high_out, double gamma)
- {
- double low2 = low_in * 255;
- double high2 = high_in * 255;
- double bottom2 = low_out * 255;
- double top2 = high_out * 255;
- double err_in = high2 - low2;
- double err_out = top2 - bottom2;
- int x, y;
- double val0, val1, val2;
- // intensity transform
- for (y = 0; y < src.Height; y++)
- {
- for (x = 0; x < src.Width; x++)
- {
- val0 = src.Data[y, x, 0];
- val0 = Math.Pow((val0 - low2) / err_in, gamma) * err_out + bottom2;
- if (val0 > 255) val0 = 255;
- if (val0 < 0) val0 = 0;
- dst.Data[y,x,0] = (byte)val0;
- val1 = src.Data[y, x, 1];
- val1 = Math.Pow((val1 - low2) / err_in, gamma) * err_out + bottom2;
- if (val1 > 255) val1 = 255;
- if (val1 < 0) val1 = 0;
- dst.Data[y, x, 1] = (byte)val1;
- val2 = src.Data[y, x, 2];
- val2 = Math.Pow((val2 - low2) / err_in, gamma) * err_out + bottom2;
- if (val2 > 255) val2 = 255;
- if (val2 < 0) val2 = 0; // Make sure src is in the range [low,high]
- dst.Data[y, x, 2] = (byte)val2;
- }
- }
- }
上面的代码对于大图运行效率低,下面我写个优化版的
代码如下:
- private void imadjust(Image<Gray,byte> src)
- {
- double minV , maxV ;
- int [] minP = new int[2];
- int [] maxP = new int[2];
- CvInvoke.MinMaxIdx(src, out minV, out maxV, minP, maxP);
- Mat m = src.Mat;
- m.ConvertTo(m, Emgu.CV.CvEnum.DepthType.Cv32F);
- Mat n = new Mat(m.Size,Emgu.CV.CvEnum.DepthType.Cv32F,1);
- MCvScalar p = new MCvScalar();
- p.V0 = 1.0/(maxV-minV);
- n.SetTo(p);
- Mat dst = new Mat(m.Size,Emgu.CV.CvEnum.DepthType.Cv32F,1);
- CvInvoke.Multiply(m, n, dst);
- p.V0 = 255;
- n.SetTo(p);//设置矩阵为p.V0 的值
- CvInvoke.Multiply(dst, n, dst);
- pictureBox1.Image = dst.ToImage<Gray, byte>().ToBitmap();// 显示到pictureBox上
- }
都是采用向量化(矢量化)编程方式,效率不会差,但是我是简单实现,假设输出为最小为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改写相关推荐
- matlab中caitu_tiqu,车牌识别matlab语音版+OpenCV版+测试图像和设计文档
[实例简介] 该文件包含了车牌识别的两个版本:matlab语音版+OpenCV版,有丰富的测试实例和文档信息,测试结果能满足大部分的车牌识别. [实例截图] [核心代码] 5d61aaaa-2457- ...
- 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 ...
- 根据MATLAB的histeq函数改写的运行在OpenCV下的直方图规定化C源码
图像处理开发需求.图像处理接私活挣零花钱,请加微信/QQ 2487872782 图像处理开发资料.图像处理技术交流请加QQ群,群号 271891601 2016-6-8日用C++也实现了直方图规定化, ...
- matlab中调用histeq函数命令,根据MATLAB的histeq函数改写的运行在OpenCV下的直方图规定化源码!...
据说,图像的直方图规定化比直方图均衡化用得更多,但是很奇怪的是OpenCV居然没有图像直方图规定化的源码!所以,我就有必要在OpenCV下写一个图像直方图规定化处理的函数,以方便将来使用. 我在网上找 ...
- 如何把opencv编译到matlab,c – 为OpenCV编译MATLAB绑定
我正在尝试编译OpenCV 3.0的MATLAB绑定,即 Github的当前版本.我一直收到以下错误: CMake Error at /opencv/modules/matlab/compile.cm ...
- 【图像处理】gamma校正通俗解释及python实现(替代matlab imadjust)
Motivation 之前把matlab代码转python的时候转过这个函数.最近自己动手发现还会用到,遂贴上来方便有需要的朋友们自取. Gamma校正(gamma correction) 一句话解释 ...
- 3.6总结半年来的专业学习——图像处理、光栅投影、四步相移、多频外差,从matlab入门到opencv入门
2019.9.17 研究生导师定了,老师给了一些论文,并推荐了几本书 1.计算机辅助光学测量/金观昌著 作者是在美国奥克兰大学杨老师实验室做过访问学者,所以书的内容和实验室的研究方向非常切合 2.基于 ...
- matlab imadjust函数,imadjust从用法到原理—Matlab灰度变换函数之一
imadjust函数是MATLAB的一个工具箱函数,一般的语法调用格式为: f1=imadjust(f,[low_in high_in],[low_out high_out],gamma) (注: ...
- matlab imadjust将暗图像分别在RGB与HSV域增加亮度
对于RGB来说,增加亮度是直接对三个颜色维做变换,而对于HSV来说,增加亮度只需要在V这一维做变换即可.这两种方法得出来的结果并不相同,效果图如下所示. 代码如下: srcImage=imread(' ...
最新文章
- python中list作为全局变量无需global声明的原因
- 计算机图形学在数学中的应用,计算机图形学的数学工具与C#实现:数学C
- halcon边缘检测的方法及各种方法的适用范围
- 线程可以kill吗_还在用 kill -9 停机?这才是最优雅的姿势
- 浅谈android的am命令
- ni max不能连续采集图像_1.6视觉检测项目过程分解——程序的连续运行
- 表格如何调出好看的样式?
- 固态硬盘计算机怎么自定义分区,固态硬盘分区,详细教您固态硬盘怎么分区
- 棋盘中正方形,长方形个数
- 在excel中如何筛选重复数据_Excel快速筛选数据方法集锦
- 基于微信小程序的药店管理系统毕业设计
- 如何在Mac Finder中查找/Usr 路径?
- c语言汇率转换代码_基于C语言实现的货币转换器.doc
- 整理--linux设备驱动模型
- QEMU文档之虚拟NVDIMM
- JS获取跨域的cookie实例
- cogs 1341 永无乡
- pythonmultiprocessing之 queue线程_python中的进程、线程(threading、multiprocessing、Queue、subprocess)...
- 阿里云配置https,配置SSL证书
- Windows Server 2008 R2 活动目录的安装和配置