gpt4 book ai didi

c++ - 在C++和OpenCV中调用其他文件中的函数

转载 作者:行者123 更新时间:2023-11-30 17:31:52 27 4
gpt4 key购买 nike

我知道这可能是重复的,但我实际上已经浏览了几篇关于 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,但我尝试了一个简单的 int add(int x, int y)在 C 文件中,它可以直接运行。我哪里出错了?

最佳答案

(基于本的评论)

此命令应该有效:

g++ -ggdb `pkg-config --cflags opencv` test.cpp draw_shapes.cpp `pkg-config --libs opencv`

注意“pkg-config --cflags opencv”和“pkg-config --libs opencv”周围的反引号。正则引号 (') 不起作用 - 这里确实需要反引号 (`)。在大多数键盘上,反引号键位于“Esc”键的正下方。或者只是从脚本中复制粘贴它们。

关于c++ - 在C++和OpenCV中调用其他文件中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24442836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com