OpenCV 将关键点转换为像素坐标
OpenCV transform key points into pixel coordinates
我正在尝试从给定的 cv::KeyPoint
中提取 像素坐标 。这个 class 有一个名为 pt
的 cv::Point2f
类型的变量,它只是一个包含两个 floats.
的元组
我不确定将 cv::Point2f
转换为 cv::Point2i
是否有效,因为我在文档中找不到这些浮点数代表什么。
我正尝试在 python 中执行此操作,但找不到合适的 OpenCV 3 文档对于 python 两者都没有。
我的代码:
import cv2
feature_detector = cv2.xfeatures2d.SURF_create()
key_points = feature_detector.detect(img, None)
# this is a list of float tuples e.g. (100.3224, 451.2334)
float_coordinates = list(map(lambda key_point: key_point.pt))
# pixel_coordinates = ?
回答我自己的问题:
看起来cv::Point2f
是亚像素坐标。只需将 floats 四舍五入到最近的 ints 就可以了。
有一个 OpenCV 函数已经做到了这一点,叫做 cv::cvRound。
这实际上就是 OpenCV 在 /modules/features2d/src/draw.cpp
中的函数 cv::_drawKeypoint
中所做的
我正在尝试从给定的 cv::KeyPoint
中提取 像素坐标 。这个 class 有一个名为 pt
的 cv::Point2f
类型的变量,它只是一个包含两个 floats.
我不确定将 cv::Point2f
转换为 cv::Point2i
是否有效,因为我在文档中找不到这些浮点数代表什么。
我正尝试在 python 中执行此操作,但找不到合适的 OpenCV 3 文档对于 python 两者都没有。
我的代码:
import cv2
feature_detector = cv2.xfeatures2d.SURF_create()
key_points = feature_detector.detect(img, None)
# this is a list of float tuples e.g. (100.3224, 451.2334)
float_coordinates = list(map(lambda key_point: key_point.pt))
# pixel_coordinates = ?
回答我自己的问题:
看起来cv::Point2f
是亚像素坐标。只需将 floats 四舍五入到最近的 ints 就可以了。
有一个 OpenCV 函数已经做到了这一点,叫做 cv::cvRound。
这实际上就是 OpenCV 在 /modules/features2d/src/draw.cpp
中的函数cv::_drawKeypoint
中所做的