“La fonction n'est pas déclarée dans le champ” Erreur lors de la compilation de code openCV
Je suis en train d'écrire du code qui utilise openCV fonctions. J'ai commencé par la prise de certains de l'exemple de code disponible dans la documentation:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1]); //Read the file
if(! image.data ) //Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );//Create a window for display.
imshow( "Display window", image ); //Show our image inside it.
waitKey(0); //Wait for a keystroke in the window
return 0;
}
Quand j'essaie de le construire dans Eclipse-CDT, j'obtiens ceci:
**** Build of configuration Debug for project openCV1 ****
make all
Building target: openCV1
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "openCV1" ./src/displayImage.o
./src/displayImage.o: In function `main':
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:25: undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:33: undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:36: undefined reference to `cv::waitKey(int)'
./src/displayImage.o: In function `~Mat':
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
./src/displayImage.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
./src/displayImage.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [openCV1] Error 1
**** Build Finished ****
Le même code, lorsque je compiler avec g++ (g++ -o displayImageInput displayImageInput.cpp
pkg-config opencv --cflags --libs).
J'ai ensuite changé le code pour faire l'image en niveaux de gris,
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1]); //Read the file
if(! image.data ) //Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
Mat grey;
cvtColor(image, grey, CV_BGR2GRAY);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", grey);
waitKey(); //Wait for a keystroke in the window
return 0;
}
Il a donné l'erreur suivante lors de la construction avec g++:
dispImgSobel.cpp: In function ‘int main(int, char**)’:
dispImgSobel.cpp:34:27: error: ‘CV_BGR2GRAY’ was not declared in this scope
dispImgSobel.cpp:34:38: error: ‘cvtColor’ was not declared in this scope
J'ai besoin d'aide pour deux choses, la Façon de le faire fonctionner dans Eclipse, et, deuxièmement, comment résoudre cette marge d'erreur, pour le présent et l'avenir de cas d'utilisation.
OriginalL'auteur jackStinger | 2013-07-26
Vous devez vous connecter pour publier un commentaire.
la première erreur est un problème de linker. vous n'avez pas de lien contre opencv_core.un et opencv_highgui.un
règle de base: pour chaque en-tête de module vous comprennent, vous aurez besoin de lier la bibliothèque appropriée.
le 2ème est un problème de compilateur, vous avez oublié un en-tête,
#include <opencv2/imgproc/imgproc.hpp>
et, de l'ofc. besoin de lien opencv_imgproc
OriginalL'auteur berak
Assurez-vous que tout dans la sortie de
pkg-config opencv --cflags --libs
est dans votre chemin de recherche pour l'éditeur de liens.Il y a aussi un pkg-config add-on pour Eclipse qui vous permettra de mettre la chaîne exacte ci-dessus directement, plutôt que d'ajouter manuellement chaque élément de la sortie.
OriginalL'auteur jam