Please follow my official account [Jizhi Vision] for more notes to share

Hi, I’m Jizhi Vision. This paper introduces the opencV Mat to darknet Image data structure method, short length, purely record.

So let’s start.

// OpencV CV ::Mat can be obtained simply by reading the following figure, or can be constructed directly
cv::Mat cv_img = cv::imread(imgfile);

int img_w = cv_img.cols;
int img_h = cv_img.rows;
int img_c = cv_img.channels;
unsigned char* imgdata = cv_img.data;
int setp = cv_img.step;

// Darknet's image can be constructed directly
image dk_img = make_image(img_w, img_h, img_c);

// start converting CV ::Mat -> dk image
for (int y = 0; y < img_h; ++y) {
  for (int k = 0; k < img_c; ++k) {
    for (int x = 0; x < img_w; ++x) {
      dk_img.data[k * img_w * img_h + y * img_w + x] = imgdata[y * setp + x * 3 + k] / 255.0 f; }}}Copy the code

Well, that’s all. I hope my sharing can be of some help to your study.


[Public Account transmission]

Opencv Mat to Darknet Image data structure