opencv中的graphcut产生奇怪的错误

时间:2014-03-01 15:53:42

标签: c++ opencv image-processing graph-algorithm

我有这段代码:

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);  生成错误。

我尝试将图片转换为浮动图片来修复问题,但也清除了图像中的所有数据。

所以我的代码目前是:

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);

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

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

初始化图像蒙版。使用:

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);