OpenCV 2轮廓查找器返回a vector<Point2i>,但有时您想要使用需要的函数vector<Point2f>.什么是最快,最优雅的转换方式?
这是一些想法.一个非常通用的转换函数,可以转换为Mat:
template <class SrcType, class DstType>
void convert1(std::vector<SrcType>& src, std::vector<DstType>& dst) {
cv::Mat srcMat = cv::Mat(src);
cv::Mat dstMat = cv::Mat(dst);
cv::Mat tmpMat;
srcMat.convertTo(tmpMat, dstMat.type());
dst = (vector<DstType>) tmpMat;
}
Run Code Online (Sandbox Code Playgroud)
但是这会使用额外的缓冲区,因此它并不理想.这是一种预先分配向量然后调用的方法copy():
template <class SrcType, class DstType>
void convert2(std::vector<SrcType>& src, std::vector<DstType>& dst) {
dst.resize(src.size());
std::copy(src.begin(), src.end(), dst.begin());
}
Run Code Online (Sandbox Code Playgroud)
最后,使用back_inserter:
template <class SrcType, class DstType>
void convert3(std::vector<SrcType>& src, std::vector<DstType>& dst) {
std::copy(src.begin(), src.end(), std::back_inserter(dst));
}
Run Code Online (Sandbox Code Playgroud) 我有一个OpenCv Mat,我将用于每像素重新映射,称为remap有CV_32FC2元素.
其中一些元素可能超出了重映射的允许范围.所以我需要将它们夹在Point2f(0, 0)和之间Point2f(w, h).使用OpenCv 2.x实现此目的的最短或最有效的方法是什么?
这是一个解决方案:
void clamp(Mat& mat, Point2f lowerBound, Point2f upperBound) {
vector<Mat> matc;
split(mat, matc);
min(max(matc[0], lowerBound.x), upperBound.x, matc[0]);
min(max(matc[1], lowerBound.y), upperBound.y, matc[1]);
merge(matc, mat);
}
Run Code Online (Sandbox Code Playgroud)
但我不确定它是否是最短的,或者分裂/合并是否有效.
我想将鼠标移动限制在 OS X 10.11 中屏幕的特定矩形区域。我修改了MouseTools(下面)中的一些代码来执行此操作,但是当您碰到屏幕边缘时会感到紧张。我怎样才能摆脱这种抖动?
// gcc -Wall constrain.cpp -framework ApplicationServices -o constrain
#include <ApplicationServices/ApplicationServices.h>
int main (int argc, const char * argv[]) {
if(argc != 5) {
printf("Usage: constrain left top right bottom\n");
return 0;
}
int leftBound = strtol(argv[1], NULL, 10);
int topBound = strtol(argv[2], NULL, 10);
int rightBound = strtol(argv[3], NULL, 10);
int bottomBound = strtol(argv[4], NULL, 10);
CGEventTapLocation tapLocation = kCGHIDEventTap;
CGEventSourceRef sourceRef = CGEventSourceCreate(kCGEventSourceStatePrivate);
while(true) {
CGEventRef mouseEvent = CGEventCreate(NULL);
CGPoint mouseLoc = …Run Code Online (Sandbox Code Playgroud)