gpt4 book ai didi

c++ - opencv中的graphcut产生奇怪的错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:29 25 4
gpt4 key购买 nike

我有这个代码:

int main()
{
Mat image0=imread("C:\\Working Dir\\Tests\\TestBlending\\image0.jpg");
Mat image1=imread("C:\\Working Dir\\Tests\\TestBlending\\image1.jpg");

// our corners are just at (0,0)
cv::Point corner1;
corner1.x = 0;
corner1.y = 0;

cv::Point corner2;
corner2.x = 0;
corner2.y = 0;

std::vector<cv::Point> corners;

corners.push_back(corner1);
corners.push_back(corner2);

std::vector<cv::Mat> masks;
Mat imageMask0;
Mat imageMask1;
masks.push_back(imageMask0);
masks.push_back(imageMask1);

std::vector<cv::Mat> sources;

sources.push_back(image0);
sources.push_back(image1);

cv::detail::GraphCutSeamFinder *seam_finder = new cv::detail::GraphCutSeamFinder();
seam_finder->find(sources, corners, masks);

printf("%lu\n", masks.size());
for(int i = 0; i < masks.size(); i++)
{
std::cout << "MASK = "<< std::endl << " " << masks.at(i) << std::endl << std::endl;
}

return 0;
}

当我运行这段代码时,出现了这个错误:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in unknown function, file C:\slave\builds\WinInstallerMegaPack\src\opencv\modules\core\include\opencv2/core/mat.hpp, line 537

堆栈失败的地方是:

    [Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]    
TestApplication.exe!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 152 C++

TestApplication.exe!cv::error(class cv::Exception const &) Unknown

TestApplication.exe!cv::Mat::at<class cv::Point3_<float> >(int,int) Unknown

TestApplication.exe!cv::detail::GraphCutSeamFinder::Impl::findInPair(unsigned int,unsigned int,class cv::Rect_<int>) Unknown

TestApplication.exe!cv::detail::PairwiseSeamFinder::run(void) Unknown

TestApplication.exe!cv::detail::PairwiseSeamFinder::find(class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > const &,class std::vector<class cv::Point_<int>,class std::allocator<class cv::Point_<int> > > const &,class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > &) Unknown

TestApplication.exe!cv::detail::GraphCutSeamFinder::Impl::find(class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > const &,class std::vector<class cv::Point_<int>,class std::allocator<class cv::Point_<int> > > const &,class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > &) Unknown

TestApplication.exe!cv::detail::GraphCutSeamFinder::find(class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > const &,class std::vector<class cv::Point_<int>,class std::allocator<class cv::Point_<int> > > const &,class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > &) Unknown

TestApplication.exe!main(int argc, char * * argv) Line 58 C++

我正在使用 OpenCV 2.4.6(Visual Studio 2012 上的 C++)

生成此错误的代码有什么问题?

是否有示例代码显示如何在混合前使用 GraphCut 找到两个图像之间的接缝线?

编辑1

我做了一些挖掘,发现问题出在这一行:

   subimg1.at<Point3f>(y + gap, x + gap) = img1.at<Point3f>(y1, x1);

这是在 seam_finders.cpp 的第 1241 行。

问题是图像 1 不是浮点格式,因此 img1.at(y1, x1); 产生错误。

我尝试将图像转换为 float ,这解决了问题,但也清除了图像中的所有数据。

所以我现在的代码是:

Mat image0=imread("C:\\Working Dir\\Tests\\TestBlending\\layer0000.jpg");
Mat image1=imread("C:\\Working Dir\\Tests\\TestBlending\\layer0001.jpg");

image0.convertTo(image0,CV_32FC3);
image1.convertTo(image1,CV_32FC3);

我可以看到图像已正确加载,但在 convertto 之后它们都是空的(值为零)。

我该如何解决这个问题?

最佳答案

初始化您的图像 mask 。使用:

vector<Mat> masks;
Mat mask0(cropped_scene.size(), CV_8U);
mask0(Rect(0, 0, mask0.cols, mask0.rows)).setTo(255);
Mat mask1(cropped_object.size(), CV_8U);
mask1(Rect(0, 0, mask1.cols, mask1.rows)).setTo(255);
masks.push_back(mask0);
masks.push_back(mask1);

关于c++ - opencv中的graphcut产生奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com