現在地 >> メニュー >> TBB >> OpenCV+TBB::2値化 >> CV+TBB::2値化(多重ループ)

問題


OpenCV+TBB::2値化を変更して、2重ループの並列化で2値化を行え。

答え

#include <iostream>
#include <string>
#include <tbb/task_scheduler_init.h>
#include <tbb/blocked_range2d.h>
#include <tbb/parallel_for.h>
#include <cv.h>
#include <highgui.h>


const std::string filename = "test2.jpg";

/********[データ構造]***********/
class TbbThresh
{
private:
	IplImage *imgT;

public:
	//コンストラクタ
	TbbThresh(IplImage *img):imgT(img){}

	//オペレータ
	void operator()(const tbb::blocked_range2d<int,int>& range) const
	{

		for(int m = range.rows().begin(); m != range.rows().end();++m)  // y
		{
			for(int n = range.cols().begin(); n != range.cols().end();++n) // x
			{

				/**** 閾値処理 *****/
				if( unsigned( imgT->imageData[m*imgT->widthStep + n]) > unsigned char(127) )
				{
					imgT->imageData[m*imgT->widthStep + n] = char(255);
				}else
				{
					imgT->imageData[m*imgT->widthStep + n] = char(0);
				}

			} //xループ
		}//yループ

	}
};

/******** [プロトタイプ宣言] ***********/
void Tbb_Img_Process(IplImage *imgA);


/*******[ここからメイン関数]*********/
int main()
{
	IplImage *imgA = cvLoadImage(filename.c_str(),CV_LOAD_IMAGE_GRAYSCALE);
	if(imgA == NULL)
	{
		std::cerr << "file not found \n";
		return -1;
	}

	Tbb_Img_Process(imgA);

	cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
	cvShowImage("window",imgA);

	cvWaitKey(0); // 0秒待つ => ずっと入力待ち

	cvReleaseImage( & imgA);
	cvDestroyAllWindows();

	return EXIT_SUCCESS;
}


void Tbb_Img_Process(IplImage *imgA)
{
	tbb::task_scheduler_init TbbInit;
	tbb::parallel_for(tbb::blocked_range2d<int,int>(0,imgA->height,0,imgA->widthStep),TbbThresh(imgA),tbb::auto_partitioner());
	
	TbbInit.terminate();
}


複数の画像を読み込んでみる


複数の画像を読み込んだ時もほとんど同じ

#include <iostream>
#include <string>
#include <tbb/task_scheduler_init.h>
#include <tbb/blocked_range2d.h>
#include <tbb/parallel_for.h>
#include <cv.h>
#include <highgui.h>


const std::string filename[] = {"test2.jpg","test3.jpg"};

/********[データ構造]***********/
class TbbThresh
{
private:
	IplImage *imgT;

public:
	//コンストラクタ
	TbbThresh(IplImage *img):imgT(img){}

	//オペレータ
	void operator()(const tbb::blocked_range2d<int,int>& range) const
	{
		for(int m = range.rows().begin(); m != range.rows().end();++m)  // y
		{
			for(int n = range.cols().begin(); n != range.cols().end();++n) // x
			{

				/**** 閾値処理 *****/
				if( unsigned( imgT->imageData[m*imgT->widthStep + n]) > unsigned char(127) )
				{
					imgT->imageData[m*imgT->widthStep + n] = char(255);
				}else
				{
					imgT->imageData[m*imgT->widthStep + n] = char(0);
				}

			} //xループ
		}//yループ

	}
};

/******** [プロトタイプ宣言] ***********/
void Tbb_Img_Process(IplImage *imgA);


/*******[ここからメイン関数]*********/
const int NUM = 2;
int main()
{

	IplImage *imgA[NUM];

	tbb::task_scheduler_init TbbInit;
	for(int loop = 0; loop < NUM; ++loop)
	{
		imgA[loop] = cvLoadImage(filename[loop].c_str(),CV_LOAD_IMAGE_GRAYSCALE);
		if(imgA[loop] == NULL)
		{
			std::cerr << "file not found \n";
			return -1;
		}

		Tbb_Img_Process(imgA[loop]);
		
		cvNamedWindow(filename[loop].c_str(),CV_WINDOW_AUTOSIZE);
		cvShowImage(filename[loop].c_str(),imgA[loop]);
	}
	TbbInit.terminate();
	

	cvWaitKey(0); // 0秒待つ => ずっと入力待ち
	for(int loop = 0; loop < NUM; ++loop)
	{
		cvReleaseImage( & imgA[loop]);
	}
	cvDestroyAllWindows();

	return EXIT_SUCCESS;
}


void Tbb_Img_Process(IplImage *imgA)
{
	tbb::parallel_for(tbb::blocked_range2d<int,int>(0,imgA->height,0,imgA->widthStep),TbbThresh(imgA),tbb::auto_partitioner());
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます