資源簡介
利用opencv實現圖像任意點5*5鄰域的均值和方差計算
代碼片段和文件信息
#include
#include
#include
#include
using?namespace?std;
using?namespace?cv;
int?main(int?argcchar*?argv[])
{
IplImage*?src=cvLoadImage(“grad.jpg“0);
//cvSetImageROI(srccvRect(005050));
IplImage*?img1=cvCreateImage(cvGetSize(src)src->depthsrc->nChannels);
cvCopyImage(srcimg1);
cout<width< CvMat*?img2=cvCreateMat(img1->heightimg1->widthCV_64F);
CvMat*?sum=cvCreateMat(img1->height+1img1->width+1CV_64F);//積分圖,首行首列都是0,所以高和寬均加1
CvMat*?sqsum=cvCreateMat(img1->height+1img1->width+1CV_64F);//平方積分圖
CvMat*?tilted_sum=cvCreateMat(img1->height+1img1->width+1CV_64F);//旋轉45度積分圖
for(int?i=0;iheight;i++)
{
for(int?j=0;jwidth;j++)
{
cvmSet(img2ijimg1->imageData[i*img1->widthStep+j]);//img2是src的復制圖
}
}
//求積分圖和平方積分圖
cvIntegral(img2sumsqsumtilted_sum);
//求各點5*5領域的積分平方e2x
CvMat*?square=cvCreateMat(img1->height+4img1->width+4CV_64F);
CvMat*?e2x=cvCreateMat(img1->height+1img1->width+1CV_64F);//Ex
cvZero(?square?);
????????cvZero(?e2x?);
????????//5*5
for(int?i=2;irows-2;i++)
{
for(int?j=2;jcols-2;j++)
{
cvmSet(squareijcvmGet(sumi-1j-1));
}
}
????????//5*5鄰域的均值e2x
long?double?wh=0.0w2h2=0.0w2h2_=0.0w2_h2=0.0w2_h2_=0.0;
for(int?i=2;irows-2;i++)
{
for(int?j=2;jcols-2;j++)
{
w2h2=cvmGet(squarei+2j+2);?//?Get?sum(i+2j+2);
w2h2_=cvmGet(squarei+2j-2);
w2_h2=cvmGet(squarei-2j+2);
w2_h2_=cvmGet(squarei-2j-2);
wh=w2h2-w2h2_-w2_h2+w2_h2_;
cvmSet(e2xi-1j-1wh/25);
}
}
//求各點5*5領域的平方積分ex2
CvMat*?square2=cvCreateMat(img1->height+4img1->width+4CV_64F);
CvMat*?ex2=cv
- 上一篇:C++ blackjack經典21點小游戲 課程設計
- 下一篇:藍噪聲采樣代碼
評論
共有 條評論