Opencv 图像增强算法 图像检测结果

来源:互联网 发布:哥伦比亚号 知乎 编辑:程序博客网 时间:2024/05/01 00:34

本code通过直方图变换增强了图像对比度,实现了单通道图像增强。将图像灰度阈值拉伸到0-255,图像检测结果见底部

Keywords: 图像增强 增强对比度 直方图变换

[cpp] view plaincopyprint?
  1. int ImageStretchByHistogram(IplImage *src1,IplImage *dst1)  
  2. /************************************************* 
  3. Function:      通过直方图变换进行图像增强,将图像灰度的域值拉伸到0-255 
  4. src1:               单通道灰度图像                   
  5. dst1:              同样大小的单通道灰度图像  
  6. *************************************************/  
  7. {  
  8.     assert(src1->width==dst1->width);  
  9.     double p[256],p1[256],num[256];  
  10.       
  11.     memset(p,0,sizeof(p));  
  12.     memset(p1,0,sizeof(p1));  
  13.     memset(num,0,sizeof(num));  
  14.     int height=src1->height;  
  15.     int width=src1->width;  
  16.     long wMulh = height * width;  
  17.       
  18.     //statistics   
  19.     for(int x=0;x<src1->width;x++)  
  20.     {  
  21.         for(int y=0;y<src1-> height;y++){  
  22.             uchar v=((uchar*)(src1->imageData + src1->widthStep*y))[x];  
  23.                 num[v]++;  
  24.         }  
  25.     }  
  26.     //calculate probability   
  27.     for(int i=0;i<256;i++)  
  28.     {  
  29.         p[i]=num[i]/wMulh;  
  30.     }  
  31.   
  32.     //p1[i]=sum(p[j]);  j<=i;   
  33.     for(int i=0;i<256;i++)  
  34.     {  
  35.         for(int k=0;k<=i;k++)  
  36.             p1[i]+=p[k];  
  37.     }  
  38.   
  39.     // histogram transformation  
  40.     for(int x=0;x<src1->width;x++)  
  41.     {  
  42.         for(int y=0;y<src1-> height;y++){  
  43.             uchar v=((uchar*)(src1->imageData + src1->widthStep*y))[x];  
  44.                 ((uchar*)(dst1->imageData + dst1->widthStep*y))[x]= p1[v]*255+0.5;              
  45.         }  
  46.     }  
  47.     return 0;  
  48. }  
  49.   
  50. void CCVMFCView::OnImageAdjustContrast()  
  51. {  
  52.     if(workImg->nChannels>1)  
  53.         OnColorToGray();  
  54.     Invalidate();  
  55.     dst=cvCreateImage(cvGetSize(workImg),workImg->depth,workImg->nChannels);  
  56.     ImageStretchByHistogram(workImg,dst);  
  57.     m_dibFlag=imageReplace(dst,&workImg);  
  58.     Invalidate();  
  59. }  

Experiment Result:

原图灰度化

                                    原图灰度化

检测结果1

                                     检测结果1

灰度化并增强对比度

                                灰度化并增强对比度

检测结果2

                                   检测结果2

原创粉丝点击