Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Call OpenCV from a C-File

Hello Community,

I'm new here and have less programming experience. For a project work for my studies I want to implement an image processing code in an existing program. If this question is misplaced here, please let me know. During my search in the www I didn't find a solution or a similar post. If there is one existing, please let me know too. Additional I want to mention, that I didn't asked this question in another forums.

Im struggling with the task to implement the OpenCV libs. I have an existing programm. Included in this project there is a C-File (vds-client.c - see last picture in my post) which generates a picture (three channels with values for r,g and b) this picture I want to transfer to a c++-file (ImageProcessing_OpenCV.cpp - see last picture in my post) via an external function which I included to the project. In the c++-file, the image will be processed and if a specific object is detected, the flag should be set and returned to the c-file. Right now, I set the flag constantly to a value to not generate another error. Also the image processing code itself is not implemented so far. First I need a working platform.

In the C++-file I included the precompiled OpenCV Libraries which worked. E.g. I'm typing "MA" and the system offers me "MAT" as an OpenCV function.As well as the function call from the C-side to the C++-function works fine.

But the problem occurs in the .obj file from the c++-file. The error, unfortunately in German, is called:

Fehler 5 error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z)" in Funktion ""public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ)". C:\CM_Projects\TestProject\src\ImageProcessing_OpenCV.obj CarMaker

My Code (extracted from vds-client.c) to receive the image from a simulation environment and to transmit it to the c++-file (ImageProcessing_OpenCV) is:

VDS_GetData(void){
int len = 0;
int res = 0;

/* Variables for Image Processing */
char    ImgType[64]; 
int     ImgWidth, ImgHeight, Channel;
float   SimTime;
int     ImgLen;
unsigned char *img;

extern int CV_Function(img, ImgHeight, ImgWidth);



if (sscanf(VDScfg.sbuf, "*VDS %d %s %f %dx%d %d", &Channel, 
           ImgType, &SimTime, &ImgWidth, &ImgHeight, &ImgLen) == 6) {
    if (0)
        Log("SimTime: %6.3f, Channel: %d: ImgType: %8s ImgSize: %dx%d, ImgLen:  %d\n", SimTime, Channel, ImgType, ImgWidth, ImgHeight, ImgLen);
    if (ImgLen > 0) {
        if (strcmp(ImgType, "rgb") == 0) {
            *img = (unsigned char *)malloc(ImgLen);
            for (len=0; len<ImgLen; len+=res) {
                if ((res=recv(VDScfg.sock, img+len, ImgLen-len, VDScfg.RecvFlags)) < 0) {
                    Log ("VDS: Socket Reading Failure\n");
                    break;
                }
            }
            Log("len: %d, ImgLen: %d", len, ImgLen);
            if (len == ImgLen) {

                VDSIF.Flag = CV_Function(*img, ImgHeight, ImgWidth);


                if (VDScfg.Verbose) {
                /* Print general image information */
                    //Log ("> %s\n", VDScfg.sbuf);

                    //Log ("Minimal distance: %6.3f m\n", VDSIF.MinDepth);
                    //Log ("Pixel position: x = %u, y = %u\n", MinDepthPixel%ImgWidth, MinDepthPixel/ImgWidth);
                    //Log ("\n");
                }
            }
            free (img);
        }
    }
    VDSIF.nImages++;
    VDSIF.nBytes += len;
} else {
    Log("VDS: not handled: %s;\n Empfangener Bildtyp stimmt nicht mit Abzugleichendem überein \n", VDScfg.sbuf);
}

return 0;
}

The actual OpenCV Function is implemented in the c++-File:

#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

 using namespace cv;
 using namespace std;

/// Global variables

int     Flag;


 extern "C" {
int CV_Function(unsigned char *img, int HEIGHT, int WIDTH) {
    Mat A = Mat(HEIGHT, WIDTH, CV_8UC3);
    A = *img;
    namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
    imshow("Display window", A);

    return Flag = 1;
    }
 }

I linked the OpenCV libs in my project: image description

As well as I included the libs here:

image description

And here:

image description

For furhter understanding, my whole project looks like this: image description

I'm happy for any idea to solve this problem and any tip for my programming.

If any information are missing, please let me know and I will provide them immediately.

Best regards

Max