没有从main.cpp C ++调用C函数

时间:2017-08-17 13:26:33

标签: c++ c

extern "C" {
#include "CLAHE.h"
}
#include "opencv2/opencv.hpp"       // opencv general include file
#include "opencv2/highgui/highgui.hpp"  // opencv GUI include file
using namespace cv;

#include <vector>

#include "stdlib.h"

#include <QDebug>

#define WIDTH 704
#define HEIGHT 576

int main(int argc, int * argv[])
{
    int width = 0, height = 0;
    unsigned char * cbuf, *obuf;

    Mat gray;
    Mat img = imread("path/to/image", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
    cvtColor(img, gray, CV_BGR2GRAY);

    width = gray.cols; height = gray.rows;

    qDebug() << "Width = " << width << ", Height = " << height;

    CLAHE_OBJ *obj = new CLAHE_OBJ;

    init_clahe(obj, width, height, 64, 64, 256, 0, 255);

    return 0;
}

CLAHE.h的结构为CLAHE_OBJ:

typedef struct CLAHE_OBJ
{
    int *m_blocks_histogram;
    int *m_blocks_he_map;
    int m_image_width;
    int m_image_height;
    int m_image_pixels;
    int m_window_size_wd;
    int m_window_size_ht;
    int m_window_pixels;
    int m_width_blocks_num;
    int m_height_blocks_num;
    int m_blocks_num;

    //optional
    int m_grayscale_levels; //Default: 256, Max: 256
    int *m_map_to_histogram_bins;
    int m_input_min_pixel_value;
    int m_input_max_pixel_value;

}CLAHE_OBJ;

int init_clahe(CLAHE_OBJ *clahe_obj, int frame_width, int frame_height,
               int window_size_width, int window_size_height,
               int grayscale_levels, int min_pixel_value, int max_pixel_value)
{
    printf("Break point 1\n");

    if(frame_width <= 0 || frame_height <= 0)
    {
        printf("Frame width and height must be greater that 0.");
        return -1;
    }

    printf("Break point 2\n");

    if(grayscale_levels == 0)
    {
        printf("Number of grayscale values must be greater than 0.");
        return -1;
    }

    if(frame_width % window_size_width > 0 ||
            frame_height % window_size_height > 0)
    {
        printf("Set window size (width, height) such that the frame can be divided"
               " into equal sized blocks,i.e., "
               "frame_width % window_size_width == 0;frame_height % window_size_height > 0;");
        return -1;
    }

    printf("Break point 3\n");

    clahe_obj->m_image_width = frame_width;
    clahe_obj->m_image_height = frame_height;
    clahe_obj->m_image_pixels = frame_width * frame_height;
    clahe_obj->m_window_size_wd = window_size_width;
    clahe_obj->m_window_size_ht = window_size_height;
    clahe_obj->m_window_pixels = window_size_height * window_size_width;

    if((clahe_obj->m_width_blocks_num = frame_width / window_size_width) < 2 ||
    (clahe_obj->m_height_blocks_num = frame_height / window_size_height) < 2)
    {
        printf("Number of blocks that the frame can be divide into must be greater than 4, i.e.,"
               "frame_width / window_size_width >= 2;frame_height / window_size_height >= 2;");
        return -1;
    }

    printf("Break point 4\n");

    clahe_obj->m_blocks_num = clahe_obj->m_width_blocks_num * clahe_obj->m_height_blocks_num;

    if(clahe_obj->m_blocks_num > 256)
    {
        printf("Number of blocks that the frame can be divide into must be lesser than 256.");
        return -1;
    }

    clahe_obj->m_grayscale_levels = (grayscale_levels >= 256) ? 256 : grayscale_levels;
    map_to_histogram_bins(clahe_obj, min_pixel_value, max_pixel_value);

    clahe_obj->m_blocks_histogram = (int*)malloc(clahe_obj->m_blocks_num *
                                                 clahe_obj->m_grayscale_levels *
                                                 sizeof(int));
    if(!clahe_obj->m_blocks_histogram)
    {
        printf("Unable to allocate memory for histograms for each block");
        return -1;
    }
    memset(clahe_obj->m_blocks_histogram, 0, clahe_obj->m_blocks_num *
           clahe_obj->m_grayscale_levels * sizeof(int));

    printf("Break point 5\n");

    clahe_obj->m_blocks_he_map = (int*)malloc(clahe_obj->m_blocks_num *
                                              clahe_obj->m_grayscale_levels *
                                              sizeof(int));
    if(!clahe_obj->m_blocks_he_map)
    {
        printf("Unable to allocate memory for equalized histograms for each block");
        return -1;
    }

    printf("Break point 6\n");

    return 0;
}

使用'imread'读取输入图像后,程序立即崩溃。永远不会调用'init_clahe',即从不执行printf(“Break Point ..”)。我无法弄清楚出了什么问题。我甚至尝试过使用malloc而不是new,但问题仍然存在。

请帮忙。

编辑#1: 执行qDebug()语句。我确实得到了宽度和高度的正确值。

1 个答案:

答案 0 :(得分:0)

您的应用程序似乎与第三方库相关联。只需确保您使用相同的运行时库,而不是混合发布和调试运行时库。