我正在开发一个将OpenCV链接到Scilab的图像处理工具箱,以便可以直接在Scilab中使用OpenCV函数。现在,我的问题是我有很多函数在所有其他代码中使用,所以我决定将它们包含在外部cpp文件中,并将其包含在我的其他代码中。我做的是,我用函数定义创建了一个头文件common.h。 common.c具有写入的实际功能。而我的所有其他文件,例如opencv_imcrop都有#include“common.h”以及其他包含的头文件。但是,当我使用我的构建器网关文件(编译我的opencv_imcrop.cpp的.sce文件)在Scilab中编译它时,它会出现以下错误: -
opencv_imcrop.cpp:14:22:致命错误:common.h:没有这样的文件或目录 编译终止。
这是我的common.h文件: -
#include "common.cpp"
string type2str(int type);
int no_of_channels(int type);
int retrieveImage(Mat &image);
int returnImage(char *checker,int &temp,Mat img);
这是我的common.cpp文件: -
string type2str(int type)
{
string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
switch ( depth )
{
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
case CV_16S: r = "16S"; break;
case CV_32S: r = "32S"; break;
case CV_32F: r = "32F"; break;
case CV_64F: r = "64F"; break;
default: r = "User"; break;
}
return r;
}
int no_of_channels(int type)
{
uchar chans = 1 + (type >> CV_CN_SHIFT);
return chans;
}
//Note : the other 2 functions are also present here, but they
// comprise of 800 lines of code, so i did not paste them here
我的opencv_imcrop.cpp(必须包含上述使用函数的头文件的文件)文件是: -
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
extern "C"
{
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "common.h"
int opencv_imcrop(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int iRows=0,iCols=0;
int *piAddr = NULL;
int *piAddrNew = NULL;
int *piAddr2 = NULL;
int *piAddr3 = NULL;
int *piAddr4 = NULL;
int *piAddr5 = NULL;
unsigned char *pstDataR = NULL;
unsigned char *pstDataG = NULL;
unsigned char *pstDataB = NULL;
int i,j,k;
double *x,*y,*width,*height;
//checking input argument
CheckInputArgument(pvApiCtx, 5, 5);
CheckOutputArgument(pvApiCtx, 1, 1) ;
Mat image;
retrieveImage(image);
//for value of top-left x-coordinate
sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr2);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &iRows, &iCols ,&x);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//for value top-left y-coordinate
sciErr = getVarAddressFromPosition(pvApiCtx,3,&piAddr3);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr3, &iRows, &iCols ,&y);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//for value of width
sciErr = getVarAddressFromPosition(pvApiCtx,4,&piAddr4);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr4, &iRows, &iCols ,&width);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//for value of height
sciErr = getVarAddressFromPosition(pvApiCtx,5,&piAddr5);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr5, &iRows, &iCols ,&height);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//defining a temporary rectangle, that denotes the area that has to be cropped into the new image
Rect myROI(x[0], y[0], width[0], height[0]);
// Crop the full image to that image contained by the rectangle myROI
// Note that this doesn't copy the data
Mat croppedRef(image, myROI);
Mat cropped;
// Copy the data into new matrix
croppedRef.copyTo(cropped);
int temp = nbInputArgument(pvApiCtx) + 1;
string tempstring = type2str(cropped.type());
char *checker;
checker = (char *)malloc(tempstring.size() + 1);
memcpy(checker, tempstring.c_str(), tempstring.size() + 1);
returnImage(checker,temp,cropped);
//Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
//Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */
}
最后,我在scilab中执行的.sce(builder_gateway_cpp.sce)文件编译上面的cpp文件如下: -
function builder_gw_cpp()
WITHOUT_AUTO_PUTLHSVAR = %t;
tbx_build_gateway("skeleton_cpp69690", ..
["imcrop","opencv_imcrop"], ..
["opencv_imcrop.cpp"], ..
get_absolute_file_path("builder_gateway_cpp.sce"),[],"g++ -ggdb
`pkg-config --cflags opencv` -o `basename opencv_imcrop.cpp .cpp`
opencv_imcrop.cpp `pkg-config --libs opencv`");
endfunction
builder_gw_cpp();
clear builder_gw_cpp; // remove builder_gw_cpp on stack