Demo entry 6635876

cc

   

Submitted by anonymous on Aug 22, 2017 at 03:38
Language: C++. Code size: 1.8 kB.

// helper function: convert cv::Mat to a raw pointer
// N.B. ptr should be allocated before calling this function
void ConvertMat8UC3ToPtr(const cv::Mat &mat, uchar *ptr) {
    assert(nullptr != ptr);

    int height = mat.rows;
    int width = mat.cols;
    const int channel = 3;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            for (int k = 0; k < channel; k++) {
                ptr[i * width * channel + j * channel + k] = mat.at<cv::Vec3b>(i, j)[k];
            }
        }
    }
}

// demo function: show how to call fishlib API
void CALLFishCounting(const fs::path &im_dir,
                        const fs::path &out_dir) {
    /// load images
    auto r = ReadImage(im_dir, ColorMode::red);
    auto g = ReadImage(im_dir, ColorMode::green);
    auto b = ReadImage(im_dir, ColorMode::blue);

    /// convert input cv::Mat to raw pointer
    const int height = r.rows;
    const int width = r.cols;
    const int channel = 3;

    uchar *g_img_ptr = new uchar[height * width * channel];
    uchar *r_img_ptr = new uchar[height * width * channel];
    uchar *b_img_ptr = new uchar[height * width * channel];

    ConvertMat8UC3ToPtr(r, r_img_ptr);
    ConvertMat8UC3ToPtr(g, g_img_ptr);
    ConvertMat8UC3ToPtr(b, b_img_ptr);

    /// allocate space for fish counting result
    int *g_map = new int[height * width];
    int *r_map = new int[height * width];
    int *b_map = new int[height * width];

    /// run algorithm
    FishLib_AutoCounting(r_img_ptr, g_img_ptr, b_img_ptr,
                           height, width, r_map, g_map, b_map);

    /// release memory
    delete[] g_img_ptr;
    delete[] r_img_ptr;
    delete[] b_img_ptr;

    delete[] g_map;
    delete[] r_map;
    delete[] b_map;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).