在opencvc++中打开图像

IplImage Open Image in openCV c++

本文关键字:图像 opencvc++      更新时间:2023-10-16

我有图片保存为1.jpg, 2.jpg和3.jpg在一个文件夹(在C:/images/result/template/)

我正在尝试加载所有的图像如下:

string link="C:/images/result/template/";
int i=1;
while (i<4)
{
link=link+i+".jpg";
IplImage* templat  = cvLoadImage(link, 1);
IplImage* templat2 = cvCreateImage(cvSize(templat->width, templat->height), 
IPL_DEPTH_8U, 1);
i++
}

然而,我得到错误。

错误C2678:二进制'+':找不到左操作数类型为'std::string'的操作符(或者没有可接受的转换)

您可以使用sprintfstd::stringstream加载一系列图像

下面是如何使用sprintf:

char link[512];
int i=1;
while (i<4)
{
  sprintf(link,"C:/images/result/template/%d.jpg",i);
  IplImage* templat  = cvLoadImage(link, 1);
  IplImage* templat2 = cvCreateImage(cvSize(templat->width, templat->height),IPL_DEPTH_8U, 1);
  i++
}

链接=链接+我+"jpg";

并不像你想象的那样——事实上我很惊讶它能编译。

p。您需要检查cvLoadImage()的返回,以检查它是否实际加载了任何东西。
pp。你应该使用cv::imread()

检查路径,当路径不正确时:link = null

试试这个

string link="C://images//result//template//";  //Please put '//' instead of '/'
int i=1;
while (i<4)
{
link=link+i+".jpg";
IplImage* templat  = cvLoadImage(link.c_str(), 1); // instead of string, use char*
IplImage* templat2 = cvCreateImage(cvSize(templat->width, templat->height),IPL_DEPTH_8U,1);
i++; //put semi-colon
}