平均數跟標準差是統計學裡最基本的東西,cvAvg()為平均數的計算,也支援多通道的使用,而它使用的方法如下
簡單cvAvg()實作
#include
#include
#include
int main()
{
IplImage *Image1=cvLoadImage("grotto.jpg",1);
CvScalar Scalar1;
Scalar1 = cvAvg(Image1);
printf("Blue Channel Avg is : %.f/n",Scalar1.val[0]);
printf("Green Channel Avg is : %.f/n",Scalar1.val[1]);
printf("Red Channel Avg is : %.f/n",Scalar1.val[2]);
cvNamedWindow("grotto",1);
cvShowImage("grotto",Image1);
cvWaitKey(0);
}
執行結果:
與cvSum()相同,而它與cvSum()唯一不同的地方為它支援Mask遮罩的操作,cvAvg()將黑白圖遮罩白色的部份做平均數的統計,使用方式如下
簡單cvAvg()遮罩實作
#include
#include
#include
int main()
{
IplImage *Image1=cvLoadImage("grotto.jpg",1);
IplImage *MaskImage1=cvLoadImage("grotto_Threshold.jpg",0);
CvScalar Scalar1;
Scalar1 = cvAvg(Image1,MaskImage1);
printf("Blue Channel Avg is : %.f/n",Scalar1.val[0]);
printf("Green Channel Avg is : %.f/n",Scalar1.val[1]);
printf("Red Channel Avg is : %.f/n",Scalar1.val[2]);
cvNamedWindow("grotto",1);
cvShowImage("grotto",Image1);
cvWaitKey(0);
}
執行結果:
將之前洞穴的二值化黑白圖片當遮罩對應出白色部分做平均數的計算,而OpenCV也有另外一個算平均數的函式cvAvgSdv(),它可以同時算出平均數跟標準差,也支援Mask遮罩的實作,使用的方式如下
簡單cvAvgSdv()實作
#include
#include
#include
int main()
{
IplImage *Image1=cvLoadImage("grotto.jpg",1);
CvScalar MeanScalar;
CvScalar StandardDeviationScalar;
cvAvgSdv(Image1,&
MeanScalar,&
StandardDeviationScalar);
printf("Blue Channel Avg is : %.f/n",MeanScalar.val[0]);
printf("Blue Channel Standard Deviation is : %.f/n",StandardDeviationScalar.val[0]);
printf("Green Channel Avg is : %.f/n",MeanScalar.val[1]);
printf("Green Channel Standard Deviation is : %.f/n",StandardDeviationScalar.val[1]);
printf("Red Channel Avg is : %.f/n",MeanScalar.val[2]);
printf("Red Channel Standard Deviation is : %.f/n",StandardDeviationScalar.val[2]);
cvNamedWindow("grotto",1);
cvShowImage("grotto",Image1);
cvWaitKey(0);
}
執行結果:
上面的是用cvAvgSdv()計算平均數跟標準差,也同樣是用CvScalar資料結構來做數值的接收,再來,就是Mask的部份
簡單cvAvgSdv()遮罩實作
#include
#include
#include
int main()
{
IplImage *Image1=cvLoadImage("grotto.jpg",1);
IplImage *MaskImage1=cvLoadImage("grotto_Threshold.jpg",0);
CvScalar MeanScalar;
CvScalar StandardDeviationScalar;
cvAvgSdv(Image1,&
MeanScalar,&
StandardDeviationScalar,MaskImage1);
printf("Blue Channel Avg is : %.f/n",MeanScalar.val[0]);
printf("Blue Channel Standard Deviation is : %.f/n",StandardDeviationScalar.val[0]);
printf("Green Channel Avg is : %.f/n",MeanScalar.val[1]);
printf("Green Channel Standard Deviation is : %.f/n",StandardDeviationScalar.val[1]);
printf("Red Channel Avg is : %.f/n",MeanScalar.val[2]);
printf("Red Channel Standard Deviation is : %.f/n",StandardDeviationScalar.val[2]);
cvNamedWindow("grotto",1);
cvShowImage("grotto",Image1);
cvNamedWindow("grotto_Threshold",1);
cvShowImage("grotto_Threshold",MaskImage1);
cvWaitKey(0);
}
執行結果:
也是跟cvAvg()一樣用單通道二值化影像做遮罩,計算平均數與標準差,也不限於圖形的輸入,實際上任何的資料數據都可以做輸入,下面就是任意一筆資料來做平均數跟標準差的計算
cvMat資料結構計算平均數標準差
#include
#include
#include
uchar Array[10]={1,2,3,4,5,6,7,8,9};
int main()
{
CvMat *Matrix1=cvCreateMat(1,10,CV_8UC1);
cvSetData(Matrix1,Array,Matrix1->step);
CvScalar MeanScalar;
CvScalar StandardDeviationScalar;
cvAvgSdv(Matrix1,&
MeanScalar,&
StandardDeviationScalar);
printf("Matrix1 Avg is : %.f/n",MeanScalar.val[0]);
printf("Matrix1 Standard Deviation is : %.f/n",StandardDeviationScalar.val[0]);
system("pause");
}
執行結果:
實際上,很多的函式IplImage資料結構跟CvMat資料結構都是可以共用的,包括前面的cvCountNonZero(),cvSum(),cvAvg()及cvAvgSdv().
cvAvg()
計算IplImage資料結構或CvMat資料結構的平均數,支援Mask遮罩的運算,第一個引數輸入為IplImage資料結構或CvMat資料結構,第二個引數為IplImage資料結構單通道uchar型別的遮罩對應,輸出為CvScalar資料結構
cvAvg(輸入IplImage資料結構或CvMat資料結構,輸入單通道uchar型別IplImage資料結構)
cvAvgSdv()
計算IplImage資料結構或CvMat資料結構的平均數,標準差,支援Mask通道的計算,第一個引數為輸入IplImage資料結構或CvMat資料結構,第二個引數為輸出CvScalar平均數資料結構,第三個引數為輸出CvScalar標準差資料結構,第四個引數為IplImage資料結構單通道uchar型別遮罩的輸入
cvAvgSdv(輸入IplImage資料結構或CvMat資料結構,輸出平均數CvScalar資料結構,輸出標準差CvScalar資料結構,輸入單通道uchar型別IplImage資料結構)