Error Using CvMoments to calculate the HU moments - c++

I am doing my FYP and I am a bit new to both OpenCV and C++. I have looked for info regarding CvMoments and all i found (in theory examples that work) do not solve my problem. I want to load a set of images ("1.png" to "5.png") and write the HU moments into a text file. The code is as shows:
CvMoments moments;
CvHuMoments hu_moments;
char filename[80] = "";
ofstream myfile;
myfile.open ("HU_moments.txt");
for (int i=0;i<5;i++){
sprintf(filename,"%u.png",i);
IplImage* image = cvLoadImage(filename);
cvMoments(image,&moments);
cvGetHuMoments(&moments, &hu_moments);
myfile << "Hu1: " << hu_moments.hu1 <<
"Hu2: " << hu_moments.hu2 <<
"Hu3: " << hu_moments.hu3 <<
"Hu4: " << hu_moments.hu4 <<
"Hu5: " << hu_moments.hu5 <<
"Hu6: " << hu_moments.hu6 <<
"Hu7: " << hu_moments.hu7 << ".\n";
cvReleaseImage(&image);
}
myfile.close();
The problem occurs when i get to cvMoments(image,&moments). I get:
Unhandled exception at 0x759fb9bc in Viewer.exe: Microsoft C++ exception: cv::Exception at memory location 0x002fce00..
I have tried declaring moments as a pointer (with its corresponding melloc) but still i get the same error. The funny thing is if i click the option to continue debugging (5 times, one for each loop) i will get results that are printed into my text file. i am using visual studio 2008.
I hope someone knows what is going on here and how to solve.

You are calling it right, but I suspect that your problem is that the previous call is failing:
IplImage* image = cvLoadImage(filename);
You need to check the result of cvLoadImage() and make sure that you are passing a valid argument to cvMoments():
IplImage* image = cvLoadImage(filename);
if (!image)
{
cout << "cvLoadImage() failed!" << endl;
// deal with error! return, exit or whatever
}
cvMoments(image, &moments);
It's also a good idea to check if myfile was successfully opened.
The root of the problem, according to my crystal ball, is a misconception of where you should put the files that the application needs to load when its executed from Visual Studio, and that would be the directory where your source code files are.
If you put the image files on the same directory as the source code, you should be OK.
On the other hand, when you execute your application manually (by double clicking the executable), the image files need to be in the same directory as the executable.
EDIT:
I'm convinced that cvMoments() takes a single-channel image as input. This example doesn't throw any exceptions:
CvMoments moments;
CvHuMoments hu_moments;
IplImage* image = cvLoadImage(argv[1]);
if (!image)
{
std::cout << "Failed cvLoadImage\n";
return -1;
}
IplImage* gray = cvCreateImage(cvSize(image->width, image->height), image->depth, 1);
if (!gray)
{
std::cout << "Failed cvCreateImage\n";
return -1;
}
cvCvtColor(image, gray, CV_RGB2GRAY);
cvMoments(gray, &moments, 0);
cvGetHuMoments(&moments, &hu_moments);
cvReleaseImage(&image);
cvReleaseImage(&gray);
Before converting the colored image to gray, I was getting:
OpenCV Error: Bad argument (Invalid image type) in cvMoments, file OpenCV-2.3.0/modules/imgproc/src/moments.cpp, line 373
terminate called after throwing an instance of 'cv::Exception'
OpenCV-2.3.0/modules/imgproc/src/moments.cpp:373: error: (-5) Invalid image type in function cvMoments

Related

Magick++ and Qt - Unable to open image reads directory as random gibberish

I was working on a small project for understanding Qt.
But Magick+ is creating a weird error.
"Read" function of the "Image" can't work with strings.
When I tried to printf/cout the exception, I realized It just reads the filename as random gibberish like this:
"Unable to open image '~—': Invalid argument # error/blob.c/OpenBlob/3537"
When I call it multiple times -even though the strings are different- it gives the same gibberish every time in a session. But when I restart the program, it changes.
I replaced it with a small, hardcoded filename ("image.jpg"); it doesn't work. Same error.
The code:
void ConvertImage(string filedir, string newext, QString destPath, string fileName)
{
Image image;
try
{
replace(filedir.begin(),filedir.end(),'/','\\');
cout << filedir << endl; // Debug print
image.read(filedir);
newext.insert(0,".");
image.write(QstToStd(destPath).append(fileName).append(newext));
}
catch( Exception &error_ )
{
cout << error_.what() << endl; // Debug print
}
}
The repo:
https://github.com/edgarbarney/BatchImageConverter

Train_hog.cpp OpenCV 3.1 Example - Cannot pass the proper path parameters

This post describes a very similar problem to mine however I am new here and was told to post a new question. Would very much appreciated anyone's help.
#Franksye I am stuck with the same problem. I am passing the path in this line
{#pd|C:/Cars/|pos_dir}{#p|Pos.lst|pos.lst}{#nd|C:/Cars/|neg_dir} {#n|Neg.lst|neg.lst}");
In the text file Pos.lst I wrote for example image0000.png, image0001.png underneath each other.
However when I run the debugger after build it gives me the below error
The program '[0x3CF0] opencv.exe' has exited with code -1 (0xffffffff).
When creating Brake points i realized that it is exiting on the load_images function when executing file.open((prefix + filename).c_str());
void load_images(const string & prefix, const string & filename, vector< Mat > & img_lst)
{
string line;
ifstream file;
file.open((prefix + filename).c_str());
if (!file.is_open())
{
cerr << "Unable to open the list of images from " << filename << " filename." << endl;
exit(-1);
}
bool end_of_parsing = false;
while (!end_of_parsing)
{
getline(file, line);
if (line.empty()) // no more file to read
{
end_of_parsing = true;
break;
}
Mat img = imread((prefix + line).c_str()); // load the image
if (img.empty()) // invalid image, just skip it.
continue;
#ifdef _DEBUG
imshow("image", img);
waitKey(10);
#endif
img_lst.push_back(img.clone());
}
}
I believe that I am doing something wrong when passing the path of the directory since the load_images function is not able to open the file of the images.
Can someone point me in the right direction or tell me what it is that I am doing wrong please.
Thank you in advance.
Solved it! Such a stupid mistake thanks to Windows.
pos.lst and neg.lst where actually pos.lst.txt and neg.lst.txt because of file extension being hidden. Thanks to This post managed to solve my problem.
Had to switch to Windows because i needed to use Visual Studio will revert back to Ubuntu once this project is over!

how to load jpeg file using DLIB libarary?

After attempting to run a example program downloaded from Here, I understand for working with jpeg files , I must add #define DLIB_JPEG_SUPPORT directive to the project. but before that It's necessary to download jpeg library and add it to the project. I did These steps:
1.Download jpegsr9a.zip from here and unzipped it.
2.Download WIN32.mak and paste it into the jpeg root folder
3.Open Developer Command Prompt from visual studio 2013 tools
4.In command prompt type : nmake -f makefile.vc setup-v10
5.After these steps jpeg.sln created ,the note is when I open jpeg.sln in VS2013 the message come:
maybe base of the problem start from here , I don't know
6.Build the jpeg.sln with the proper configuration (I built it many times with different configurations, recently I built it using this .)
at the end of building the error came :"unable to start jpeg.lib"
but in release folder or debug folder (depend on configuration) jpeg.lib was created
open main project which is using DLIB for detecting face,I added jpeg root folder to Additonal Include Directory and jepegroot/release to Additional Libarary Directories ,then change the UseLibrary dependencies to "yes" and I also added jpeg.lib to the dependecies.
during building the project errors come:
This is the source which I trying to build and run
//#define HAVE_BOOLEAN
#define DLIB_JPEG_SUPPORT
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include<dlib/image_transforms.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>
//
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
// This example takes in a shape model file and then a list of images to
// process. We will take these filenames in as command line arguments.
// Dlib comes with example images in the examples/faces folder so give
// those as arguments to this program.
if (argc == 1)
{
cout << "Call this program like this:" << endl;
cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
return 0;
}
// We need a face detector. We will use this to get bounding boxes for
// each face in an image.
frontal_face_detector detector = get_frontal_face_detector();
// And we also need a shape_predictor. This is the tool that will predict face
// landmark positions given an image and face bounding box. Here we are just
// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// as a command line argument.
shape_predictor sp;
deserialize(argv[1])>>sp;
image_window win, win_faces;
// Loop over all the images provided on the command line.
for (int i = 2; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
array2d<rgb_pixel> img;
load_image(img, argv[i]);
// Make the image larger so we can detect small faces.
pyramid_up(img);
// Now tell the face detector to give us a list of bounding boxes
// around all the faces in the image.
std::vector<rectangle> dets = detector(img);
cout << "Number of faces detected: " << dets.size() << endl;
// Now we will go ask the shape_predictor to tell us the pose of
// each face we detected.
std::vector<full_object_detection> shapes;
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(img, dets[j]);
cout << "number of parts: " << shape.num_parts() << endl;
cout << "pixel position of first part: " << shape.part(0) << endl;
cout << "pixel position of second part: " << shape.part(1) << endl;
// You get the idea, you can get all the face part locations if
// you want them. Here we just store them in shapes so we can
// put them on the screen.
shapes.push_back(shape);
}
// Now let's view our face poses on the screen.
win.clear_overlay();
win.set_image(img);
win.add_overlay(render_face_detections(shapes));
// We can also extract copies of each face that are cropped, rotated upright,
// and scaled to a standard size as shown here:
dlib::array<array2d<rgb_pixel> > face_chips;
extract_image_chips(img, get_face_chip_details(shapes), face_chips);
win_faces.set_image(tile_images(face_chips));
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
// ----------------------------------------------------------------------------------------
I can choose other alternatives but I spend too much time to reach here , I want to know How I can solve this problem and load jpeg file when using DLIB
I also read these links:
Compiling libjpeg
http://www.dahlsys.com/misc/compiling_ijg_libjpeg/index.html
dlib load jpeg files
http://sourceforge.net/p/dclib/discussion/442518/thread/8a0d42dc/
I solved my problem by below instruction, please follow it.
- Add include directory in VC++
- Include source.cpp
- Add add files in dlib/external/libjpeg to project
- Define in Preprocessor
-- You don't need to use any additional library.

DescrpitorExtractor::create("SIFT") returns 0?

The following piece of code outputs 0.
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");
cout << descriptor << endl;
whilethis piece of code outputs a non-zero pointer.
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("ORB");
cout << descriptor << endl;
What should I do to fix the create sift function? I have tested it with opencv 2.4.7 and 2.4.6.1.
SIFT and SURF are patented, nonfree.
so, to use those, you have to
include the "opencv2/nonfree/nonfree.hpp" header,
link to the opencv_nonfree.lib and
call cv::initModule_nonfree();
in main() before doing anything else.

OpenCV: Eigenfaces with CSV file

I'm struggling with this issue for quite some time now and maybe someone here might have a suggestion of what's going wrong. I'm trying to use the libfacerec to implement Eigenfaces in OpenCV from this site: https://github.com/bytefish/libfacerec I'm using OpenCV-2.3.1 with Visual Studio 2010
The sample code usess the orl_faces dataset from this site: http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html and loads these images by using a csv file. In this file all paths to all 400 images (10 images of 40 different people) are listed and a label is attached to each persons. Both entries are seperated by a " ; ". I've added a few lines of this csv file below:
C:/Users/PIMMES/Documents/libraries/orl_faces/s1/1.pgm;0
C:/Users/PIMMES/Documents/libraries/orl_faces/s1/2.pgm;0
...
C:/Users/PIMMES/Documents/libraries/orl_faces/s2/1.pgm;1
C:/Users/PIMMES/Documents/libraries/orl_faces/s2/2.pgm;1
...
etc
I've added the piece of code below which should load the image data. This is exactly the same piece of code as listed in the main.cpp file in the /src folder from the libfacerec website:
void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';')
{
std::ifstream file(filename.c_str(), ifstream::in);
if (!file) throw std::exception();
string line, path, classlabel;
while (getline(file, line))
{
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
int main(int argc, const char *argv[])
{
// check for command line arguments
if(argc != 2)
{
cout << "usage: " << argv[0] << " <csv.ext>" << endl;
exit(1);
}
// path to your CSV
string fn_csv = string(argv[1]);
// images and corresponding labels
vector<Mat> images;
vector<int> labels;
// read in the data
try
{
read_csv(fn_csv, images, labels);
}
catch (exception& e)
{
cerr << "Error opening file \"" << fn_csv << "\"." << endl;
exit(1);
}
// get width and height
int width = images[0].cols;
int height = images[0].rows;
// get test instances
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
...
etc.
}
The whole project builds fine without any errors, but when I try to run a crash occurs. I went into Debug mode and figured that both vector< Mat > images and vector< int > labels (don't mind the spaces cause without them it doesn't display properly here) are still 0 which means no data is loaded. However when I print the variables height and width it show 140 for both (all images from orl_faces are 140x140 pixels)
So my question, what is going wrong? Why aren't both vectors filled while height and width are filled?
Edit: It seems that both vectors are filled correctly on my other pc (vector images [400], vector labels [400]. However the program still crashes and when running Debug I find this error:
Unhandled exception at 0x77c415de in Test.exe: 0xC0000005: Access violation writing location 0x00000000.
It is located in the mat.hpp file and when stepping through this file, a vector v shows these errors:
[size] CXX0030: Error: expression cannot be evaluated
[capacity CXX0030: Error: expression cannot be evaluated
I am pretty sure the problem is this.
You are linking against the libraries:
opencv_core231.lib
opencv_highgui231.lib
opencv_imgproc231.lib
And then you build with the Debug Configuration in Visual Studio. See the problem? If you want to do this switch to the opencv_core231d.lib libraries. BUT: The OpenCV2.3.1 superpack for some mysterious reasons doesn't come with the tbb_debug.dll, so the Debug build is going to fail. If you are using the superpack and want to use libfacerec, then activate the Release Build Configuration in Visual Studio, build & run and everything is going to work just fine.
I've written a tutorial on it, which should be easy to follow: http://www.bytefish.de/blog/opencv_visual_studio_and_libfacerec. Scroll to the very bottom to see the Eigenfaces in Windows. So you see, it actually works.