Including External cpp file in OpenCV cpp Code

598 views Asked by At

I am working on an Image Processing toolbox, that links OpenCV to Scilab, so that OpenCV functions can be used in Scilab directly. Now, my issue is that I have many functions that are used in all the other codes, so i decided to include those in an external cpp file, and include that in my other codes. What I did was, i made a header file common.h with the function definitions. common.c has the actual functions written. And all my other files, for e.g opencv_imcrop have a #include "common.h" along with the other included header files. However, when i compile this in Scilab using my builder gateway file(a .sce file that compiles my opencv_imcrop.cpp), it gives the following error:-

opencv_imcrop.cpp:14:22: fatal error: common.h: No such file or directory compilation terminated.

This is my common.h file:-

#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);

This is my common.cpp file:-

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

My opencv_imcrop.cpp(The file that has to include the above made header file to use the functions) file is:-

#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;

  }
/* ==================================================================== */
}

And finally, the .sce(builder_gateway_cpp.sce) file i execute in scilab to compile the above cpp file is as follows:-

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
0

There are 0 answers