【发布时间】:2014-06-27 01:53:12
【问题描述】:
我知道这可能是重复的,但实际上我已经阅读了几篇关于 SO 的帖子来尝试解决这个问题
我在 OpenCV 和 C++ 中有以下代码
test.cpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include "draw_shapes.h"
using namespace cv;
using namespace std;
int main(){
Mat normal = imread("/INPUT 2.jpg");
Mat gray;
cvtColor(normal, gray,CV_RGB2GRAY);
Mat binary = gray > 128;
Point start = cv::Point(5.0,5.0);
Point end = cv::Point(200.0,200.0);
draw_line(binary, start, end, 0, 255, 0);
imshow("Draw_Line", binary);
while(waitKey(0)!=27){
;
}
return 0;
}
draw_shapes.cpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include "draw_shapes.h"
void draw_line(cv::Mat img, cv::Point start, cv::Point end, int B, int G, int R){
int thickness = 2;
int lineType = 8;
line(img,start,end,Scalar(B,G,R),thickness,lineType);
}
void draw_rectangle(cv::Mat img, cv::Point p1, cv::Point p2, int B, int G, int R){
int thickness = 2;
int lineType = 8;
rectangle(img, p1, p2, Scalar(B,G,R),thickness, lineType);
}
draw_shapes.h
#ifndef DRAWSHAPES_H
#define DRAWSHAPES_H
void draw_line(cv::Mat, cv::Point, cv::Point, int, int, int);
void draw_rectangle(cv::Mat, cv::Point, cv::Point, int, int, int);
#endif
compile_opencv.sh
#!/bin/bash
echo "compiling $1"
if [[ $1 == *.c ]]
then
gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"
./${1%.*}
我几乎浏览了 StackOverflow 上与头文件相关的所有帖子,包括来自外部 CPP 文件的函数,这就是我得到的。
现在,当我编译时,我得到undefined reference to 'draw_line(cv::Mat, cv::Point_<int>, cv::Point_<int>, int, int, int)'
我在胡乱猜测并说这可能是因为我没有单独编译 draw_shapes.cpp,但我在 C 文件中尝试了一个简单的int add(int x, int y),它直接工作。我哪里错了?
【问题讨论】:
-
你是如何编译这个的?你在编译这两个 cpp 文件吗?
-
那么在
g++命令中,您在什么时候构建draw_shapes.cpp文件? -
@Ben 这就是我的意思。如上所述,当我包含 C 文件时,我不需要单独编译。包含 C++ 文件时是否需要编译?如果是,您能否建议修改 bash 脚本?
-
在命令行上试试这个:
g++ -ggdb pkg-config --cflags opencv test.cpp draw_shapes.cpp -o output(修复我在 g++ 命令中犯的任何错误)这对你有用吗? -
也许这是我个人的喜好,但#include
看起来比长长的标题列表更好。为我工作。
标签: c++ c opencv header-files