Skip to content

Opencv.h

Go to the source code of this file.

Shows an example of processing images using the OpenCV library and inspecting them.

This is an explanation of the ExampleClsRuntimeOpencv.cpp file, which is excluded from the build in the Examples project. This source file is the OpenCV version of ExampleClsRuntime.cpp. The Examples project provided does not include the OpenCV library. Therefore, it is excluded from the build and does not cover how to load the OpenCV library in the project settings.

Load Image

You can load images using the imread function of OpenCV. Check the height, width, and channel information of the image.

201
202
203
204
205
206
207
208
209
210
// ExampleClsRuntimeOpencv.cpp:203

cv::Mat cvImage = cv::imread(img, cv::IMREAD_UNCHANGED);
if (cvImage.empty()) {
    std::cerr << "cv::imread() failed - " << img << std::endl;
    continue;
}
h = cvImage.rows;
w = cvImage.cols;
c = cvImage.channels();

Next, set the image flag value to input to the Infer function.

  • First, the channel order of OpenCV is BGR.
  • If the number of channels is 3, add the SAIGE_IMAGE_COLOR flag.
  • If the number of channels is 4, add the SAIGE_IMAGE_COLOR and SAIGE_IMAGE_ALPHA flags.
  • If it is a 16-bit image, add the SAIGE_IMAGE_16BIT flag.
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// ExampleClsRuntimeOpencv.cpp:213

unsigned int imageFlag = SAIGE_IMAGE_BGR; // cv 기본 BGR
if (c == 3) {
    imageFlag |= SAIGE_IMAGE_COLOR;
} else if (c == 4) {
    imageFlag |= SAIGE_IMAGE_COLOR | SAIGE_IMAGE_ALPHA;
}  else {
    std::cerr << "Unsupported image channel: " << c << std::endl;
    continue;
}
type = cvImage.type();
if (type == CV_16UC1 || type == CV_16UC3 || type == CV_16UC4) {
    // 16bit depth image
    imageFlag |= SAIGE_IMAGE_16BIT;
}

Call Infer Function

Use the data member of cv::Mat to input the image data. After that, everything is the same, and you can visualize the result using OpenCV functions instead of the visualization function of the utility function.

236
237
238
239
240
241
242
243
// ExampleClsRuntimeOpencv.cpp:238

retval = SaigeInferFromByteArray(handle, cvImage.data, w, h, imageFlag, result);
if (retval < 0) {
    std::cerr << "SaigeInferFromByteArray() failed - " << SaigeStrError(retval)
              << std::endl;
    continue;
}