首页 > 解决方案 > OpenCV warpPerspective 错误:通道数错误

问题描述

使用时warpPerspective

OpenCV 错误:cvConvertImage 中的通道数错误(源图像必须有 1、3 或 4 个通道),文件 /build/opencv-ys8xiq/opencv-2.4.9.1+dfsg/modules/highgui/src/utils.cpp,第 622 行抛出'cv :: Exception'实例后调用终止what():/build/opencv-ys8xiq/opencv-2.4.9.1+dfsg/modules/highgui/src/utils.cpp:622:错误:(-15)源图像在函数 cvConvertImage 中必须有 1、3 或 4 个通道

但是,正在使用的源图像是 1 个通道并且具有所需的大小。

这段代码基本上是为了得到一张图像的鸟瞰图。

cv::Mat warped;
std::vector<cv::Point2f> src  ;
src.push_back(cv::Point2f(640, 470));
src.push_back(cv::Point2f(0, 470));
src.push_back(cv::Point2f(150, 250));
src.push_back(cv::Point2f(490, 250));


std::vector<cv::Point2f> dst  ;
dst.push_back(cv::Point2f(640, 480));
dst.push_back(cv::Point2f(0, 480));
dst.push_back(cv::Point2f(0, 0));
dst.push_back(cv::Point2f(640, 0));

cv::Mat M = cv::getPerspectiveTransform(src,dst);

cv::warpPerspective(src, warped, M, image.size());

标签: c++opencv

解决方案


在主题中进行了讨论:https ://stackoverflow.com/a/17863381

简短的回答:

对点使用 cv::perspectiveTransform 或矩阵乘法,对图像使用 cv::warpPerspective

我希望它会有所帮助。


推荐阅读