******************************************************************************************************************************************
紅:數字圖像處理視頻教程(兩部)
{中科院版36講視頻教程 + 電子科大版70講視頻教程(岡薩雷斯 第二版)}
? ? 橙:halcon軟件、halcon軟件手把手教破解視頻教程
? ? 黃:數字圖像模式識別demo(C++編寫,在公司也是用C++哦)
? ? 綠:halcon軟件視頻教程、halcon軟件在vs2010中配置
? ? 青:面向對象C++視頻教程
? ? 藍:MFC C++視頻教程
? ? 紫:海量相關文檔資料
? ? http://item.taobao.com/item.htm?spm=a1z10.3.w4002-9510581636.11.VUYzOY&id=43025290175
******************************************************************************************************************************************
本code通過直方圖變換增強了圖像對比度,實現了單通道圖像增強。將圖像灰度閾值拉伸到0-255,圖像檢測結果見底部
Keywords: 圖像增強 增強對比度 直方圖變換
- int?ImageStretchByHistogram(IplImage?*src1,IplImage?*dst1)??
- /*************************************************?
- Function:??????通過直方圖變換進行圖像增強,將圖像灰度的域值拉伸到0-255?
- src1:???????????????單通道灰度圖像???????????????????
- dst1:??????????????同樣大小的單通道灰度圖像??
- *************************************************/??
- {??
- ????assert(src1->width==dst1->width);??
- ????double?p[256],p1[256],num[256];??
- ??????
- ????memset(p,0,sizeof(p));??
- ????memset(p1,0,sizeof(p1));??
- ????memset(num,0,sizeof(num));??
- ????int?height=src1->height;??
- ????int?width=src1->width;??
- ????long?wMulh?=?height?*?width;??
- ??????
- ????//statistics??
- ????for(int?x=0;x<src1->width;x++)??
- ????{??
- ????????for(int?y=0;y<src1->?height;y++){??
- ????????????uchar?v=((uchar*)(src1->imageData?+?src1->widthStep*y))[x];??
- ????????????????num[v]++;??
- ????????}??
- ????}??
- ????//calculate?probability??
- ????for(int?i=0;i<256;i++)??
- ????{??
- ????????p[i]=num[i]/wMulh;??
- ????}??
- ??
- ????//p1[i]=sum(p[j]);??j<=i;??
- ????for(int?i=0;i<256;i++)??
- ????{??
- ????????for(int?k=0;k<=i;k++)??
- ????????????p1[i]+=p[k];??
- ????}??
- ??
- ????//?histogram?transformation??
- ????for(int?x=0;x<src1->width;x++)??
- ????{??
- ????????for(int?y=0;y<src1->?height;y++){??
- ????????????uchar?v=((uchar*)(src1->imageData?+?src1->widthStep*y))[x];??
- ????????????????((uchar*)(dst1->imageData?+?dst1->widthStep*y))[x]=?p1[v]*255+0.5;??????????????
- ????????}??
- ????}??
- ????return?0;??
- }??
- ??
- void?CCVMFCView::OnImageAdjustContrast()??
- {??
- ????if(workImg->nChannels>1)??
- ????????OnColorToGray();??
- ????Invalidate();??
- ????dst=cvCreateImage(cvGetSize(workImg),workImg->depth,workImg->nChannels);??
- ????ImageStretchByHistogram(workImg,dst);??
- ????m_dibFlag=imageReplace(dst,&workImg);??
- ????Invalidate();??
- }??
Experiment Result:
? ? ? ? ? ? ? ??? ? ? ??? ? ? ? ? ? 原圖灰度化
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?檢測結果1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 灰度化并增強對比度
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?檢測結果2