×
現在地 >> メニュー >> OpenCV >> highgui編02 >> highgui編02::まとめ

問題

画像を読み込んで、[q]or[esc]キーが押されるまで、画像上に適当に描画をせよ。
その他条件:
  • cvWaitKey関数で500ミリ秒待つ

答え

#include <iostream>
#include <ctime>
#include <cv.h>
#include <highgui.h>

#pragma comment(lib,"cv200.lib")
#pragma comment(lib,"cxcore200.lib")
#pragma comment(lib,"highgui200.lib")

const char *filename = "testimg.png";


int main()
{
	IplImage *imgA = cvLoadImage(filename);
	if(imgA ==NULL )
	{
		std::cerr << "Can't load image\n";
		return -1;
	}

	cvNamedWindow("key and idle");
	cvShowImage("key and idle",imgA);//1度表示
	int key;

	CvRNG rng = cvRNG(static_cast<int64>(time(NULL)));//乱数のseed設定
	CvPoint pt;
	unsigned char color[3];
	for(;;)
	{	
		key = cvWaitKey(500);
		if (key == '\033' || key == 'q')//[esc]or[q]で終了
		{
			break;
		}
		pt = cvPoint(cvRandInt(&rng)%imgA->width, cvRandInt(&rng)%imgA->height);
		color[0] = cvRandInt(&rng)%256;
		color[1] = cvRandInt(&rng)%256;
		color[2] = cvRandInt(&rng)%256;
		cvCircle(imgA,pt,50,CV_RGB(color[0],color[1],color[2]),2);
		cvShowImage("key and idle",imgA);//描画

	}
	cvReleaseImage(&imgA);
	cvDestroyAllWindows();

	return 0;
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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