[英]Problem with running c++ opencv program in linux: functions not declared
I'm learning OpenCV and I'm using c++.我正在学习 OpenCV,我正在使用 C++。 I installed opencv in my ubuntu using the libopencv-dev and python3-opencv packegs in ubuntu 20.04 from this tutorial: https://linuxize.com/post/how-to-install-opencv-on-ubuntu-20-04/ but when I try to run a sample program using this command g++ -I/usr/local/include/opencv4 Example1.cpp -o Example1
I get this error:我使用本教程中 ubuntu 20.04 中的 libopencv-dev 和 python3-opencv packegs 在我的 ubuntu 中安装了 opencv: https ://linuxize.com/post/how-to-install-opencv-on-ubuntu-20-04/ 但是当我尝试使用此命令运行示例程序时g++ -I/usr/local/include/opencv4 Example1.cpp -o Example1
我收到此错误:
Example1.cpp: In function ‘int main(int, char**)’:
Example1.cpp:5:2: error: ‘IplImage’ was not declared in this scope
5 | IplImage* img = cvLoadImage( argv[1] );
| ^~~~~~~~
Example1.cpp:5:12: error: ‘img’ was not declared in this scope
5 | IplImage* img = cvLoadImage( argv[1] );
| ^~~
Example1.cpp:5:18: error: ‘cvLoadImage’ was not declared in this scope
5 | IplImage* img = cvLoadImage( argv[1] );
| ^~~~~~~~~~~
Example1.cpp:6:29: error: ‘CV_WINDOW_AUTOSIZED’ was not declared in this scope
6 | cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZED );
| ^~~~~~~~~~~~~~~~~~~
Example1.cpp:6:2: error: ‘cvNamedWindow’ was not declared in this scope
6 | cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZED );
| ^~~~~~~~~~~~~
Example1.cpp:7:2: error: ‘cvShowImage’ was not declared in this scope
7 | cvShowImage( "Example1", img );
| ^~~~~~~~~~~
Example1.cpp:8:2: error: ‘cvWaitKey’ was not declared in this scope
8 | cvWaitKey(0);
| ^~~~~~~~~
Example1.cpp:9:2: error: ‘cvReleaseImage’ was not declared in this scope
9 | cvReleaseImage( &img );
| ^~~~~~~~~~~~~~
Example1.cpp:10:2: error: ‘cvDestroyWindow’ was not declared in this scope
10 | cvDestroyWindow( "Example1" );
| ^~~~~~~~~~~~~~~
here is the source code:这是源代码:
#include <opencv2/highgui.h>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZED );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
and the pkg-config output(if it's needed):和 pkg-config 输出(如果需要):
p4n0@p4n0-desktop:~/Desktop/Example_Project$ pkg-config --cflags opencv p4n0@p4n0-desktop:~/Desktop/Example_Project$ pkg-config --cflags opencv
-I/usr/local/include/opencv4 -I/usr/local/include/opencv4
p4n0@p4n0-desktop:~/Desktop/Example_Project$ pkg-config --libs opencv p4n0@p4n0-desktop:~/Desktop/Example_Project$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core -L在/ usr / local / lib目录-lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
To use IplImage , you need to include opencv2/core/types_c.h
要使用IplImage ,您需要包含opencv2/core/types_c.h
Edit:- Since you are using OpenCV4, those includes are not available for you.编辑:- 由于您使用的是 OpenCV4,因此您无法使用这些包含。 They were removed.他们被移除了。
In C++, like other languages to use a function you need to import it so the compiler can find the function during it's compilation step在 C++ 中,像其他语言一样使用函数,您需要导入它,以便编译器可以在编译步骤中找到该函数
Off topic suggestion:- This applies in case you are not forced to use IplImage etc.题外建议:- 这适用于您不被迫使用 IplImage 等的情况。
If you are allowed to use Modern OpenCV, and are not restricted by old versions, prefer the Modern solutions not the C API.如果您被允许使用 Modern OpenCV,并且不受旧版本的限制,则更喜欢 Modern 解决方案而不是 C API。
IplImage etc. were removed from OpenCV4. IplImage 等已从 OpenCV4 中删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.