0
こんにちは、ありがとう。OpenCV:AffineTransformerの使い方
OpenCVでのマッチングのためにシェイプの使用をテストして、マッチングする部分を管理したいと思います。 回転した形状を見つけるには、私はAffineTransformer Classを選んでください。マッチングが内部的にどのように機能するのかわからないので、誰かが手続きが記述されたリンクを持っているといいでしょう。
shawshankは、次のコードでは、推測変換に渡されたときに変数の一致が空であるため、アサーション失敗エラーをスローします。だれかが正しい方法でこの関数を使用する方法を知っていますか?それは本当に何をしていますか?
#include<opencv2/opencv.hpp>
#include<algorithm>
#include<iostream>
#include<string>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
bool rotateImage(Mat src, Mat &dst, double angle)
{
// get rotation matrix for rotating the image around its center
cv::Point2f center(src.cols/2.0, src.rows/2.0);
cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0);
// determine bounding rectangle
cv::Rect bbox = cv::RotatedRect(center,src.size(), angle).boundingRect();
// adjust transformation matrix
rot.at<double>(0,2) += bbox.width/2.0 - center.x;
rot.at<double>(1,2) += bbox.height/2.0 - center.y;
cv::warpAffine(src, dst, rot, bbox.size());
return 1;
}
static vector<Point> sampleContour(const Mat& image, int n=300)
{
vector<vector<Point>> contours;
vector<Point> all_points;
findContours(image, contours, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
for (size_t i=0; i <contours.size(); i++)
{
for (size_t j=0; j<contours[i].size(); j++)
{
all_points.push_back(contours[i][j]);
}
}
int dummy=0;
for (int add=(int)all_points.size(); add<n; add++)
{
all_points.push_back(all_points[dummy++]);
}
// shuffel
random_shuffle(all_points.begin(), all_points.end());
vector<Point> sampled;
for (int i=0; i<n; i++)
{
sampled.push_back(all_points[i]);
}
return sampled;
}
int main(void)
{
Mat img1, img2;
vector<Point> img1Points, img2Points;
float distSC, distHD;
// read images
string img1Path = "testimage.jpg";
img1 = imread(img1Path, IMREAD_GRAYSCALE);
rotateImage(img1, img2, 45);
imshow("original", img1);
imshow("transformed", img2);
waitKey();
// Contours
img1Points = sampleContour(img1);
img2Points = sampleContour(img2);
//Calculate Distances
Ptr<ShapeContextDistanceExtractor> mysc = createShapeContextDistanceExtractor();
Ptr<HausdorffDistanceExtractor> myhd = createHausdorffDistanceExtractor();
distSC = mysc->computeDistance(img1Points, img2Points);
distHD = myhd -> computeDistance(img1Points, img2Points);
cout << distSC << endl << distHD << endl;
vector<DMatch> matches;
Ptr<AffineTransformer> transformerHD = createAffineTransformer(0);
transformerHD -> estimateTransformation(img1Points, img2Points, matches);
return 0;
}
アサーションエラーは何でしたかわかりますか? –
もちろん、私はこのエラーが発生します:OpenCVエラー:warpAffine、ファイル/build/opencv/src/opencv-3.2.0/modules/imgproc/src/でアサーションが失敗しました(src.cols> 0 && src.rows> 0) imgwarp.cpp、行5977 'cv :: Exception'のインスタンスをスローした後に終了します what():/build/opencv/src/opencv-3.2.0/modules/imgproc/src/imgwarp.cpp:5977:エラー:(-215)src.cols> 0 && src.rows> 0 in function warpAffine – FranzKaiser
このアサーションは、あなたの入力画像が 'warpAffine'に空であることを意味します。どのイメージが関数に渡されているかを確認するには、コードをデバッグする必要があります。 –