opencv c++ 以灰度图方式加载一张图片,并统计该图像素的最大值、最小值、均值以及方差,完整代码
2个回答
展开全部
void main()
{
IplImage *src = cvLoadImage("xxx",1);
int width = src->width;
int height = src->height;
uchar *ptr = src->imageData;
int Iij;
double Imax = 0, Imin = 255, Iave = 0, Idelta = 0;
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
Iij = (int) ptr[i*width+j];
if(Iij > Imax)
Imax = Iij;
if(Iij < Imin)
Imin = Iij;
Iave = Iave + Iij;
}
}
Iave = Iave/(width*height);
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
Iij = (int) ptr[i*width+j];
Idelta = Idelta + (Iij-Iave)*(Iij-Iave);
}
}
Idelta = Idelta/(width*height);
printf("灰度最大值 = %d\n",Imax);
printf("灰度最小值 = %d\n",Imin);
printf("灰度均值 = %d\n",Iave);
printf("灰度方差 = %d\n",Idelta);
return;
}
{
IplImage *src = cvLoadImage("xxx",1);
int width = src->width;
int height = src->height;
uchar *ptr = src->imageData;
int Iij;
double Imax = 0, Imin = 255, Iave = 0, Idelta = 0;
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
Iij = (int) ptr[i*width+j];
if(Iij > Imax)
Imax = Iij;
if(Iij < Imin)
Imin = Iij;
Iave = Iave + Iij;
}
}
Iave = Iave/(width*height);
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
Iij = (int) ptr[i*width+j];
Idelta = Idelta + (Iij-Iave)*(Iij-Iave);
}
}
Idelta = Idelta/(width*height);
printf("灰度最大值 = %d\n",Imax);
printf("灰度最小值 = %d\n",Imin);
printf("灰度均值 = %d\n",Iave);
printf("灰度方差 = %d\n",Idelta);
return;
}
追问
解决了,谢谢你啦!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-06-16
展开全部
不及到耶!!!!!!!!!!!!!!!!!!!!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询