OpenGL de プログラミング - OpenCV::フィルタリング
現在地 >> メニュー >> サンプルコード::OpenCV >> OpenCV::フィルタリング
関連OpenCV::基本編07

問題


適当な行列を作成し、注目データとその4近傍のデータで平均を求め、出力せよ。

その他条件:
  • フィルタを定義し、フィルタをかける事で実現する。

答え

#include <iostream>
#include <algorithm>
#include <cv.h>

const float AVERAGE = 0.2;  // 1/5
//サンプル行列
float data[]=
{
	1,2,3,4,
	5,6,7,8,
	9,10,11,12,
	13,14,15,16
};

//フィルタ用データ
float filter[]=
{
	0,AVERAGE,0,
	AVERAGE,AVERAGE,AVERAGE,
	0,AVERAGE,0
};

int main(int argc,char*argv[])
{

	CvMat Input = cvMat (4, 4, CV_32F, data); //縦4 横4
	CvMat kernel = cvMat (3, 3, CV_32F, filter); //縦3 横3

	cvFilter2D (&Input, &Input, &kernel, cvPoint (-1, -1));

	std::copy(Input.data.fl,Input.data.fl+16,std::ostream_iterator<float>(std::cout,"\n"));

	return EXIT_SUCCESS;

}