资源描述:
《直方图均衡化及opencv实现》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、一、直方图均衡化原理:出自Sonka“Imageprocessing,analysisandmachinevision”,3rded,2008.直方图均衡化是想把左边的直方图变换为右边的直方图,使各个灰度值上的像素个数均匀分布,这样图像整体的亮度分布较均匀,不会过亮或过暗,同时可以增加图像的对比度。假设变换前后图像的灰度等级都为k,则变换过程为:i=0kG(qi)=i=0kH(pi)otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdez
2、hen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecord
3、eroftheCountypartyCommitteeSecretary,Ministersand其中,pi为变换前的灰度值,qi为变换后的灰度值,H(p)为变换前的直方图,G(p)为变换后的直方图。假设图像大小为N*N,那么Gqi应为常量值:N2qk-q0那么用精确的连续函数表达,有:其中,称为累积直方图(cumulativehistogram).转换为离散形式,得到:二、算法实现otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdez
4、hen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecord
5、eroftheCountypartyCommitteeSecretary,Ministersand1)声明数组H,数组长度为图像的灰度等级G(一般为256),图像大小为N*M;2)扫描图像,建立灰度直方图:H(pi)=H(pi)+1;3)建立累积直方图Hc(pi):Hc(p0)=H(p0);Hc(pi)=Hc(pi-1)+H(pi);4)扫描图像,计算新像素值qi,也可以建立一个查找表:qi=(G-1)Hc(pi)/(N*M)三、代码实现:Mat&MyHistEqual(Mat&I){CV_Assert(I.data);//acceptonlychartypematricesCV
6、_Assert(I.depth()!=sizeof(uchar));constintchannels=I.channels();switch(channels)otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommi
7、tteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand{case1:{intHi