如何在Xcode中使用imread工作

时间:2014-02-22 23:03:24

标签: c++ opencv

这段代码应该采用一些图像并加载它们,在代码中尝试3种不同的方法,但它们似乎都不起作用。这里的答案Getting OpenCV Error "Image step is wrong" in Fisherfaces.train() method对我不起作用,因为我没有这些库。

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.h>
using namespace cv;

int main(int argc, char** argv)
{

    //The code segments commented (not mine) are taken from     http://docs.opencv.org/modules/contrib/doc/facerec/facerec_api.html

    // holds images and labels (not mine)
    cv::vector<cv::Mat> images;
    cv::vector<int> labels;


    //this doesnt assign anything to img
    Mat img = cv::imread("/Users/STUFF/Desktop/Projects/OpenCV_testing/person0/0.jpg", 0);

    //this also does nothing
    IplImage *img=cvLoadImage("Image_Name);
    Mat mat(img);


   // images for first person (not mine) except incresed pathing/
   //repeat for multiple people in future
   images.push_back(cv::imread("/snip", CV_LOAD_IMAGE_GRAYSCALE));
   labels.push_back(0);

   images.push_back(cv::imread("/snip", CV_LOAD_IMAGE_GRAYSCALE));
   labels.push_back(0);

   images.push_back(cv::imread("/snip", CV_LOAD_IMAGE_GRAYSCALE));
   labels.push_back(0);

   // Create a new Fisherfaces model and retain all available Fisherfaces,
   // this is the most common usage of this specific FaceRecognizer:
   //(not mine)
   Ptr<FaceRecognizer> model =  createFisherFaceRecognizer();

   // This is the common interface to train all of the available cv::FaceRecognizer
   // implementations:
   //(not mine)
   model->train(images, labels);

   model->save("/snip.xml");

    return 0;
}

imread并没有做任何使得其余代码无用的事情。我是openCV的新手,并不真正理解文档所以也许我实现了一些错误的东西。我很感激有关imread的帮助以及如何阅读opencv文档。

1 个答案:

答案 0 :(得分:0)

不是真正的答案(因为评论中的代码不起作用..)

但在加载资源时检查总是一个好主意:

Mat img = imread(path,flags);
if ( img.empty() ) 
{   
    // either the path was wrong, or it could not decompress it
    cerr << "could not load " << path << endl;
    return -1;
}