- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前有两个匹配点集内置到 float32 的 numpy 数组中:
points1 =
[[ 346.70220947 9076.38476562]
[ 922.99554443 9096.4921875 ]
[ 776.96466064 9108.79101562]
[ 449.0173645 9080.61816406]
[ 2843.19433594 1226.93212891]
[ 779.95275879 9094.76855469]
[ 451.46853638 9092.5078125 ]
[ 3981.4621582 1237.50964355]
[ 132.38700867 9086.7890625 ]
[ 819.10943604 8286.74023438]
[ 1963.64025879 1220.06921387]
[ 1253.79321289 9095.75292969]]
points2 =
[[ 55110.36328125 9405.07519531]
[ 55686.71875 9423.63574219]
[ 55540.8515625 9435.80078125]
[ 55212.58203125 9408.00585938]
[ 57598.76171875 1551.92956543]
[ 55543.78125 9421.88769531]
[ 55214.40625 9420.46972656]
[ 58737.41796875 1561.14831543]
[ 54895.9296875 9414.58203125]
[ 55581.87109375 8613.87011719]
[ 56718.76953125 1546.02197266]
[ 56017.8125 9422.52050781]]
我正在尝试运行:
affine = cv2.estimateRigidTransform(points2,points1,True)
print affine
这样我就可以生成一个仿射矩阵,然后可以将其转换为世界文件 (.tfw)。世界文件用于 GIS 软件,可以动态投影这些文件。
目前我收到一个错误:
Both input images must have either 8uC1 or 8uC3 type in function cvEstimateRigidTransform
我不太确定这里发生了什么。我以为我可以使用两个点集作为参数,只要我有 6 个或更多对。
如有任何想法或建议,我们将不胜感激!
最佳答案
我有同样奇怪的错误,但在 Java 中。就我而言,estimateRigidTransform
似乎无法识别出我提供的两个 Mat 图像实际上是 2D 点集。因此,我应用了一种解决方法,以便将我的匹配点从 MatOfKeyPoint
转换为 MatOfPoint2f
类型。
这是完整的 Java 代码(它不是 Python,但也许会对你有所帮助):
更新:过滤您的匹配项很重要,因为如果不这样做,您可能会因为转换而得到一个空数组。
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
// Load First Image
Mat img1 = Imgcodecs.imread("img1_path", Imgcodecs.IMREAD_GRAYSCALE);
Mat img1_descriptors = new Mat();
MatOfKeyPoint img1_keypoints_mat = new MatOfKeyPoint();
// Detect KeyPoints
detector.detect(img1, img1_keypoints_mat);
descriptor.compute(img1, img1_keypoints_mat, img1_descriptors);
// Load Second Image
Mat img2 = Imgcodecs.imread("img2_path", Imgcodecs.IMREAD_GRAYSCALE);
Mat img2_descriptors = new Mat();
MatOfKeyPoint img2_keypoints_mat = new MatOfKeyPoint();
// Detect KeyPoints
detector.detect(img2, img2_keypoints_mat);
descriptor.compute(img2, img2_keypoints_mat, img2_descriptors);
// Match KeyPoints
MatOfDMatch matOfDMatch = new MatOfDMatch();
matcher.match(img1_descriptors, img2_descriptors, matOfDMatch);
// Filtering the matches
List<DMatch> dMatchList = matOfDMatch.toList();
Double max_dist = 0.0;
Double min_dist = 100.0;
for(int i = 0; i < img1_descriptors.rows(); i++){
Double dist = (double) dMatchList.get(i).distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
LinkedList<DMatch> good_matches = new LinkedList<>();
for(int i = 0; i < img1_descriptors.rows(); i++){
if(dMatchList.get(i).distance < 3*min_dist){
good_matches.addLast(dMatchList.get(i));
}
}
// Converting to MatOfPoint2f format
LinkedList<Point> img1_points_list = new LinkedList<>();
LinkedList<Point> img2_points_list = new LinkedList<>();
List<KeyPoint> img1_keyPoints_list = img1_keypoints_mat.toList();
List<KeyPoint> img2_keyPoints_list = img2_keypoints_mat.toList();
int limit = good_matches.size();
for(int i = 0; i < limit; i++){
img1_points_list.addLast(img1_keyPoints_list.get(good_matches.get(i).queryIdx).pt);
img2_points_list.addLast(img2_keyPoints_list.get(good_matches.get(i).trainIdx).pt);
}
MatOfPoint2f img1_point2f_mat = new MatOfPoint2f();
img1_point2f_mat.fromList(img1_points_list);
MatOfPoint2f img2_point2f_mat = new MatOfPoint2f();
img2_point2f_mat.fromList(img2_points_list);
// Draw match points
Mat output = new Mat();
Features2d.drawMatches(img1, img1_keypoints_mat, img2, img2_keypoints_mat, matOfDMatch, output);
Imgcodecs.imwrite("output.png", output);
Mat result = Video.estimateRigidTransform(img1_point2f_mat, img2_point2f_mat, true);
printMat(result); // Printing the optimal affine transformation 2x3 array
// The following variables correspond to the estimateRigidTransform result as shown here: https://stackoverflow.com/a/29511091/5165833
double a = result.get(0,0)[0];
double b = result.get(0,1)[0];
double d = result.get(1,1)[0];
double c = result.get(1,0)[0];
// Solving for scale as shown in the link above
double scale_x = Math.signum(a) * Math.sqrt( (a*a) + (b*b) );
double scale_y = Math.signum(d) * Math.sqrt( (c*c) + (d*d) );
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("scale_x = "+scale_x);
System.out.println("scale_y = "+scale_y);
}
public static void printMat(Mat m)
{
for (int x=0; x < m.height(); x++)
{
for (int y=0; y < m.width(); y++)
{
System.out.printf("%f",m.get(x,y)[0]);
System.out.printf("%s"," ");
}
System.out.printf("\n");
}
}
关于Python:在 opencv/python 中运行 estimateRigidTransform; 8uC1 或 8uC3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24344628/
cv2.estimateRigidTransform 所需的最小点数是多少? 据我所知,fullAffine=False 它有 4 个自由度,所以 2 个点就足够了。 但是: 使用 2 个 numpy
我已经从 3.4 升级到 4.0,cv2 缺少 estimateRigidTransform 函数。文档显示它仍然存在于 C++ 库中,但不在 cv2 中。它是否已重命名,或者我在构建它时是否遗漏了一
我在 python 中使用 OpenCV。该文件是杀了我。如何正确使用 estimateRigidTransform? 我正在尝试提取两个图像之间的相似性变换,所以我假设我不能使用 findHomog
下面链接中的示例使用的是 findHomography得到两组点之间的转换。我想限制转换中使用的自由度所以想替换 findHomography与 estimateRigidTransform . ht
我将 estimateRigidTransform 与大约两个 100 点的向量一起使用,并且工作正常。但不知何故 getAffineTransform 不起作用。 我知道 findHomograph
我正在使用 OpenCV 进行视频稳定项目,我有一个 CPU 实现在工作,但性能需要改进,所以我试图将大部分处理转移到 GPU。 当前的实现主要使用这四个 OpenCV 函数: cv::goodFea
这对我来说真是一团糟,我很难尝试解决这个问题。我想在 Python 中将 cv2.estimateRigitTransform 方法与 numpy 数组一起使用,但我找不到任何示例或其他内容来解释所需
我尝试在图像中转换两组点。为此,我使用了 OpenCV 的函数 estimateRigidTransform()。使用生成的单应矩阵(包含旋转矩阵和平移 vector ),我使用 OpenCV 的函数
我目前有两个匹配点集内置到 float32 的 numpy 数组中: points1 = [[ 346.70220947 9076.38476562] [ 922.99554443 9096
我是一名优秀的程序员,十分优秀!