Ask Your Question
0

Assertion error in cvtColor

asked 2016-09-04 07:18:28 -0600

CLeBeR gravatar image

updated 2016-09-08 05:21:53 -0600

Hello, I use the Face Recognition's tutorial with OpenCV in C++ avaible on the official documentation. I have no problem during the compilation but when I run the program I get the following error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, line 3737 New image readError opening file "data.csv". Reason: /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

I read a lot of topics about this problem but I don't arrive to solve it. I put some cout in the code to see where the program crash. The problem is in this line :

cvtColor(m,m2,CV_BGR2GRAY);

This is the complete code :

    /*
 * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
 * Released to public domain under terms of the BSD Simplified license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the organization nor the names of its contributors
 *     may be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 *   See <http://www.opensource.org/licenses/bsd-license>
 */

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(CV_StsBadArg, error_message);
    }
    string line, path, classlabel;
    int numberImageReaded = 0;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat m = imread(path, 1);
            Mat m2;
            cout << endl << "New image read";
            cvtColor(m,m2,CV_BGR2GRAY);
            numberImageReaded++;
            cout << endl << "Number of image read = " << numberImageReaded;
            images.push_back(m2);
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
    cout << endl << "Read finish";
}

int main(int argc, const char *argv[]) {
    // Check for valid command line arguments, print usage
    // if no arguments were given.
    if (argc != 4) {
        cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
        cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
        exit(1);
    }
    // Get the path to your CSV:
    string fn_haar = string(argv[1]);
    string fn_csv = string(argv[2]);
    int deviceId ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-09-04 07:47:17 -0600

berak gravatar image

the Mat you tried to feed into cvtColor was simply invalid / empty. most likely your csv file contained invalid filenames.

add a check after imread(), to make sure, you're working on valid data :

       Mat m = imread(path, 1);
       if (m.empty())
       {
             cerr << path << " could not be read." << endl;
             continue;
       }
edit flag offensive delete link more

Comments

Hello, thanks for your response. Effectively, for each picture, I have this result in the console : /home/user/project/S1/12.png could not be read. It was a JPG at origin but I convert it in PNG to use with OpenCV. I just installed libjpeg-dev as said in the documentation but I have the same problem, I don't know what is the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake. I compile with this command : clang++ -std=c++11 main.cpp -o w pkg-config --cflags --libs opencv Thanks a lot !

CLeBeR gravatar imageCLeBeR ( 2016-09-04 08:42:58 -0600 )edit

No idea how to solve the problem?

CLeBeR gravatar imageCLeBeR ( 2016-09-06 14:12:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-04 07:18:28 -0600

Seen: 2,986 times

Last updated: Sep 08 '16