how to display images from .csv file using opencv

asked 2018-02-19 04:53:04 -0600

shive gravatar image

updated 2018-02-19 06:45:22 -0600

Hi... i am trying to display the images from the .csv file. I am a begginer with opencv and c++ concepts, but am uanble to understand the need of some functions ....Here is the code which am trying to figure out.It showing the following error One of arguments' values is out of range (Bad new number of rows) in reshape Someone help me with this problem it wil be a great help.

#include "opencv2/core/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/face.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
using namespace cv;
using namespace cv::face;

 static Mat norm_0_255(InputArray _src)
 {
 Mat src = _src.getMat();
 Mat dst;
 switch(src.channels())
 {
 case 1:
 cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
 break;
 case 2:
 cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
 break;
 default:
 src.copyTo(dst);
 break;
 }
 return dst;
 }

 void read_csv(const String filename, vector<Mat> & images,vector<int> & labels, char separator=';')
 {
   std::ifstream myfile(filename.c_str(),ifstream::in);
   if(!myfile)
    {
      string error_message="File does not exist";
      CV_Error(Error::StsBadArg, error_message);
     }
    string line, path,classlabel;
    while(getline(myfile,line))
     {
    stringstream liness(line);
    getline(liness,path,separator);
    getline(liness,classlabel);
    if(!path.empty() && !classlabel.empty())
    {
     images.push_back(imread(path,0));
     labels.push_back(atoi(classlabel.c_str()));
     }
    }
   }
   int main(int argc, const char *argv[])
     {

    if(argc!=2)
        {
      cout<<"usage:"<<"argv[0]"<<"<csv.txt> <output_folder>\n"<<endl;
        return -1;
      }
       string output_folder =".";
       if(argc==3)
        {
         output_folder = String(argv[2]);
         }
        String fn_csv = String(argv[1]);

vector<Mat> images;
vector<int> labels;

try {
    read_csv(fn_csv, images, labels);
} catch (cv::Exception& e) {
    cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;

    exit(1);
}

if(images.size() <= 1) {
    string error_message = "This demo needs at least 2 images";
    CV_Error(CV_StsError, error_message);
}

int height = images[0].rows;

Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
images.pop_back();
labels.pop_back();
  Ptr<BasicFaceRecognizer>model = createFisherFaceRecognizer();
  model->train(images, labels);
  Mat mean = model->getMean();
  if(argc == 2) {
  imshow("mean", norm_0_255(mean.reshape(1,height)));
  } else {
    imwrite(format("%s/mean.jpeg", output_folder.c_str()), norm_0_255(mean.reshape(1,height)));
  }
  if(argc==2)
   {
    waitKey(0);
  }

 return 0;

 }
edit retag flag offensive close merge delete

Comments

Use a debugger

LBerger gravatar imageLBerger ( 2018-02-19 06:25:23 -0600 )edit

All images must have same sizes for fischer face. check image size

LBerger gravatar imageLBerger ( 2018-02-19 07:19:47 -0600 )edit

Thank you for your reply...i have checked ... images all are of same size. For images am using yale database.

shive gravatar imageshive ( 2018-02-22 04:13:59 -0600 )edit

please copy full error message (line number and function)

LBerger gravatar imageLBerger ( 2018-02-22 05:52:53 -0600 )edit

This is the error which am getting....

OpenCV Error: One of arguments' values is out of range (Bad new number of rows) in reshape, file /home/veena/Downloads/opencv-3.2.0/modules/core/src/matrix.cpp, line 1050 terminate called after throwing an instance of 'cv::Exception' what(): /home/veena/Downloads/opencv-3.2.0/modules/core/src/matrix.cpp:1050: error: (-211) Bad new number of rows in function reshape Aborted (core dumped)

shive gravatar imageshive ( 2018-02-22 06:00:18 -0600 )edit

error is here. Please update your code to opencv 3.4.

check getMean size

LBerger gravatar imageLBerger ( 2018-02-22 07:29:30 -0600 )edit