ページ 11

画像処理、特徴抽出について

Posted: 2014年6月09日(月) 15:53
by yusuke01
以下のプログラムを実行すると赤文字の箇所で
0xC0000005: 場所 0x00000000 を読み込み中にアクセス違反が発生しました。
というエラーが出てしまいます。

誰かアドバイスお願いします。

コード:

 
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>

 

const std::string& filename1 = ("sample/artest.jpg");			// 画像1のファイル名
const std::string& filename2 = ("sample/artest02.jpg");			// 画像2のファイル名
const std::string& featureDetectorName = ("SURF");		// detectorType
const std::string& descriptorExtractorName = ("SURF");	// descriptorExtractorType
const std::string& descriptorMatcherName = ("BruteForce");	// descriptorMatcherType
bool crossCheck = true;			// マッチング結果をクロスチェックするかどうか

int main()
{   
	
	
	// 画像の読み込み
	cv::Mat img1 = cv::imread(filename1);
	cv::Mat img2 = cv::imread(filename2);

	// SIFT・SURFモジュールの初期化
	cv::initModule_nonfree();

	// 特徴点抽出
	cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create(featureDetectorName);
	std::vector<cv::KeyPoint> keypoint1, keypoint2;
	
	[color=#BF0000]detector->detect(img1, keypoint1);[/color]
	detector->detect(img2, keypoint2);

	// 特徴記述
	cv::Ptr<cv::DescriptorExtractor> extractor = cv::DescriptorExtractor::create(descriptorExtractorName);
	cv::Mat descriptor1, descriptor2;
	extractor->compute(img1, keypoint1, descriptor1);
	extractor->compute(img2, keypoint2, descriptor2);

	// マッチング
	cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(descriptorMatcherName);
	std::vector<cv::DMatch> dmatch;
	if (crossCheck)
	{
		// クロスチェックする場合
		std::vector<cv::DMatch> match12, match21;
		matcher->match(descriptor1, descriptor2, match12);
		matcher->match(descriptor2, descriptor1, match21);
		for (size_t i = 0; i < match12.size(); i++)
		{
			cv::DMatch forward = match12[i];
			cv::DMatch backward = match21[forward.trainIdx];
			if (backward.trainIdx == forward.queryIdx)
				dmatch.push_back(forward);
		}
	}
	else
	{
		// クロスチェックしない場合
		matcher->match(descriptor1, descriptor2, dmatch);
	}

	// マッチング結果の表示
	cv::Mat out;
	cv::drawMatches(img1, keypoint1, img2, keypoint2, dmatch, out);
	cv::imshow("matching", out);
	while (cv::waitKey(1) == -1);
}

画像処理、特徴抽出について(修正)

Posted: 2014年6月09日(月) 16:02
by yusuke01
yusuke01 さんが書きました:以下のプログラムを実行すると
detector->detect(img1, keypoint1);の箇所で
0xC0000005: 場所 0x00000000 を読み込み中にアクセス違反が発生しました。
というエラーが出てしまいます。

誰かアドバイスお願いします。

コード:

 
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>

 

const std::string& filename1 = ("sample/artest.jpg");			// 画像1のファイル名
const std::string& filename2 = ("sample/artest02.jpg");			// 画像2のファイル名
const std::string& featureDetectorName = ("SURF");		// detectorType
const std::string& descriptorExtractorName = ("SURF");	// descriptorExtractorType
const std::string& descriptorMatcherName = ("BruteForce");	// descriptorMatcherType
bool crossCheck = true;			// マッチング結果をクロスチェックするかどうか

int main()
{   
	
	
	// 画像の読み込み
	cv::Mat img1 = cv::imread(filename1);
	cv::Mat img2 = cv::imread(filename2);

	// SIFT・SURFモジュールの初期化
	cv::initModule_nonfree();

	// 特徴点抽出
	cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create(featureDetectorName);
	std::vector<cv::KeyPoint> keypoint1, keypoint2;
	
	detector->detect(img1, keypoint1);
	detector->detect(img2, keypoint2);

	// 特徴記述
	cv::Ptr<cv::DescriptorExtractor> extractor = cv::DescriptorExtractor::create(descriptorExtractorName);
	cv::Mat descriptor1, descriptor2;
	extractor->compute(img1, keypoint1, descriptor1);
	extractor->compute(img2, keypoint2, descriptor2);

	// マッチング
	cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(descriptorMatcherName);
	std::vector<cv::DMatch> dmatch;
	if (crossCheck)
	{
		// クロスチェックする場合
		std::vector<cv::DMatch> match12, match21;
		matcher->match(descriptor1, descriptor2, match12);
		matcher->match(descriptor2, descriptor1, match21);
		for (size_t i = 0; i < match12.size(); i++)
		{
			cv::DMatch forward = match12[i];
			cv::DMatch backward = match21[forward.trainIdx];
			if (backward.trainIdx == forward.queryIdx)
				dmatch.push_back(forward);
		}
	}
	else
	{
		// クロスチェックしない場合
		matcher->match(descriptor1, descriptor2, dmatch);
	}

	// マッチング結果の表示
	cv::Mat out;
	cv::drawMatches(img1, keypoint1, img2, keypoint2, dmatch, out);
	cv::imshow("matching", out);
	while (cv::waitKey(1) == -1);
}

Re: 画像処理、特徴抽出について

Posted: 2014年6月09日(月) 17:38
by h2so5
画像が正常に読み込まれているかどうかと、detectorがNULLでないかどうかを確認してください。

Re: 画像処理、特徴抽出について

Posted: 2014年6月09日(月) 20:06
by yusuke01
返信ありがとうございます。
画像が読み込まれていることは確認できました。
detectorの行の読込でエラー表示が出てしまうのですが、NULLでないかの確認はどのように行えばよいでしょうか。
未熟な質問で申し訳ないです。

Re: 画像処理、特徴抽出について

Posted: 2014年6月09日(月) 21:13
by h2so5
detector.empty() がtrueを返した場合NULLです。
http://opencv.jp/opencv-2svn/cpp/basic_ ... s.html#ptr

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 18:26
by yusuke01
何度も申し訳ないのですが
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create(featureDetectorName);
この時点でdetectorがNULLであったらコードとして不備があるのでしょうか。
またdetector->detect(img1, keypoint1);この行が読み込まれてdetectorに値が入ると認識しているのですがそれは間違いなのでしょうか。
未熟な質問で申し訳無いです。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 18:27
by yusuke01
何度も申し訳ないのですが
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create(featureDetectorName)
この時点でdetectorがNULLであったらコードとして不備があるのでしょうか。
またdetector->detect(img1, keypoint1)この行が読み込まれてdetectorに値が入ると認識しているのですがそれは間違いなのでしょうか。
未熟な質問で申し訳無いです。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 18:34
by h2so5
yusuke01 さんが書きました: cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create(featureDetectorName)
この時点でdetectorがNULLであったらコードとして不備があるのでしょうか。
コードとしての不備というより、エラーですね。
yusuke01 さんが書きました: またdetector->detect(img1, keypoint1)この行が読み込まれてdetectorに値が入ると認識しているのですがそれは間違いなのでしょうか。
違います。この行が実行される前にdetectorに値が入っている必要があります。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 18:42
by yusuke01
ありがとうございます。
確認した結果NULLでした。
この場合どのような原因が考えられるのでしょうか。
よろしくおねがいします。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 19:00
by h2so5
原因は分かりませんが、SURFのモジュールが正常にロードされていないと思います。
OpenCVのバージョンやビルドオプションの問題かもしれません。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 21:20
by yusuke01
解決しました。
ありがとうございました。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 21:34
by h2so5
解決方法を書いてください。

Re: 画像処理、特徴抽出について

Posted: 2014年6月10日(火) 22:18
by yusuke01
opencvのバージョンを新しいものにしたら解決しました。