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.
Related
I need to read simple step files (turning parts) with C++. Therefore I try to use Open Cascade. I already can open my step file and read the number of shapes in my file. Now I need to get the geometrical data (length, width, diameters..), but I dont know how it works, although I read all of the documentations.
Is there anyone who already worked with Open Cascade and can help me with my Problem? I would be very happy, thank alot !!
That's my Code since then
#include <iostream>
#include <STEPControl_Reader.hxx>
#include <string>
using namespace std;
int main() {
STEPControl_Reader reader;
IFSelect_ReturnStatus stat = reader.ReadFile("C:\\Users\\Kelevradesktop.Kelevra-desktop\\Desktop\\Studienarbeit\\steptest.step");
IFSelect_PrintCount mode = IFSelect_ListByItem;
reader.PrintCheckLoad(false, mode);
Standard_Integer NbRoots = reader.NbRootsForTransfer(); //Transfer whole file
Standard_Integer num = reader.TransferRoots();
Standard_Integer NbTrans = reader.TransferRoots();
TopoDS_Shape result = reader.OneShape();
TopoDS_Shape shape = reader.Shape();
cout << NbRoots << endl;
cout << NbTrans << endl;
cout << num << endl;
system("pause");
return 0;
}
Check the FreeCad source code. They use OpenCascade and can import step and iges. It should get you started. The function ImportStepParts(...) in https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/Part/App/ImportStep.cpp is what you search for.
Use TopExpExplorer class to iterate through the objects (vertexes, edges, faces..) of a shape. An iterating example you can find in this tutorial.
Use GProp_GProps class to get properties of a shape. Example:
GProp_GProps propertiesSystemFace;
BRepGProp::VolumeProperties(shape, propertiesSystemFace);
double shapeVolume = propertiesSystemFace.Mass();
gp_Pnt centerOfMass = propertiesSystemFace.CentreOfMass();
Also you can convert TopoDS_Edge to curve object to get some other parameters according to the edge type:
BRepAdaptor_Curve adaptCrv = BRepAdaptor_Curve(edge);
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.
I would just like a quick simple function that I can give a row and col number for a mat object and have it output the value of the element at that location...I googled and found a few sites recommending 'Mat.get(int row, int col)' but I'm getting error when I try to use it
Here is my code the error is in the post title
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "b = " << endl << " " << M.get(0, 0) << endl << endl;
I don't want to have to write a long for loop to retrieve on pixel...can some one help me figure out why I'm getting this error or recommend a quick 1 liner to get just the value of a single element in a mat object...But the one liner needs to be able to be run without the type being known so Mat.at won't work....Googling a lot brought up no easy 1 liners....any help is appreciated
Edit:
Using M.at as you suggested as this
cout << "M.at = " << endl << " " <<a.at<uchar>(0, 0) << endl << endl;
The ouput is this
M.at =
^#
the kind of weird symbol you get when you don't do it right...I was trying to avoid M.at because I thought it needed a type which I don't want to have to enter....Any help is appreciated
Edit 2: thanks for getting back to me so soon...I found one error my mat was a CV_64F I changed it for another reason...I changed it back to CV_8UC3 and also tried CV_8UC1 and got the following output for both
M =
d(yes letter d)
Any advice from here is appreciated....
There is no get() method for cv::Mat, you should use Mat::at() instead:
M.at<uchar>(0, 0)
Edit: If your Mat is of type 'CV_64F', you should use
M.at<double>(0, 0)
As a C++ beginner, I am currently facing a problem I somehow can't solve, even if the code is pretty simple.
I've been searching for answers all over the Internet, but none was applicable for my problem.
I am currently coding basic SVMs with C++, under VS2013, using OpenCV 2.4.8.
I was able to work on images of same size, specifying fixed height, width at the beginning of my code.
Now, I'm trying to : open images of different sizes, resize them to a certain lower size, and apply the previous code to the now-resized dataset. Simple as that.
Here's the beginning of my code :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp>
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>
using namespace cv;
using namespace std;
int main(){
Input parameters are :
int Nb_Data_Class_1 = 10;
int Nb_Data_Class_0 = 5;
int Height_Zone = 200;
int Width_Zone = 200;
so I will resize all my files to 200x200 format.
string Path = "C:\\Users\\....";
string Format = ".jpg";
int Nb_Files = Nb_Data_Class_1 + Nb_Data_Class_0;
Mat TrainingMat(Nb_Files, Zone_Image, CV_32FC1);
Mat TrainingLabels(Nb_Files, 1, CV_32FC1);
For every file of the class labelled {1} - they are all named Tree01, Tree02, etc. - I open, and resize.
for (int i = 0; i < Nb_Data_Class_1; ++i)
{
stringstream ss;
ss << Path << "\\Tree0" << i + 1 << Format;
Mat Image = cv::imread(ss.str(), 0);
resize(Image, Image, Size(Width_Zone, Height_Zone));}
Things worked perfectly without the last line. I had a Mat array, filled with 0-t0-255 numbers. Now, I get the following error :
OpenCV Error: Assertion failed <ssize.area<> >0> in cv::resize,
file C:\builds\2-4-PackSlave-win32-vc12-shared\opencv\modules\imgproc\serc\imgwarp.cpp, line 1824
What could be the problem ?
I thought that maybe OpenCV wasn't properly opening the files ; but, in that case, how everything could have been previously working ?
Still wondering.
Any help would be much appreciated ! Thanks in advance.
The only reason for resize to crush is absence of Image. Even if you checked that some of the images were read properly it doesn't mean that all of them were - some of them may be missing. Reading files from disk is a very common point of failure for programs because you never can be sure if the read was successfully or not. As a result every time you read an image you really really should verify that it is not empty:
if (Image.cols == 0) {
cout << "Error reading file " << ss << endl;
return -1;
}
Not going to solve the problem in this case, but this assertion can also be caused by trying to resize a Mat with a signed type like CV_8SC3. For example:
Mat wrong = Mat::zeros(4, 4, CV_8SC3); // <- Notice 'S'
Mat right = Mat::zeros(4, 4, CV_8UC3); // <- Notice 'U'
imshow("OK", right);
imshow("ASSERTS", wrong);
Note that checking wrong.cols != 0 will not prevent this from crashing.
Your line:
ss << Path << "\Tree0" << i + 1 << Format;
will produce (where i=0):
"C:\Users\....\Tree01.jpg".
Solution
Change "string Path = "C:\Users\....";" line to:
string Path = "C:\Users";
and
change "ss << Path << "\Tree0" << i + 1 << Format;" line to:
ss << Path << "Tree0" << i + 1 << Format;
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