read images from a file in C API tool

  • Hello dear Sir,

    I'm new user with software and I wanted to make my own tool that could read images from my file system. Is there a way that can be done?
    I tried to use OpenCV imread function:
    kuvio_string src = "_DSC4969.JPG";
    cv::Mat mat = cv::imread(src, 0);
    But I'm assuming that OpenCvMat function it wraps kuvio images and matrices to OpenCV data type, but not otherway around, so in the above case even though it compiles, but I can not convert it to kuvio image data type. Maybe there is similar function to OpenCV imread() that could read directly to kuvio image data types.
    Very best regards

  • Hi,
    The easiest way to read images from files is to set up a virtual camera in the Virtual Camera ribbon of the Builder. You can select the images there. Then, pick your virtual camera from the drop-down list of the Camera Id input parameter of an Image Source tool.

    All images passed between tools must be allocated by the platform. Therefore, you cannot directly return an image that is allocated by an OpenCV function. Otherwise the platform wouldn't be able to correctly deallocate it when it is no longer needed.

    What you can do is to allocate a new output image using for example kuvio_image_alloc_copy and then copying the data from the image allocated by an OpenCV function call. If you place that image to the out struct of your custom tool implementation, you get the picture out.

  • @topi Hello Topi,
    Thank you very much for the response!
    That is a very good reason why you can not pass directly other data types to tool output.
    So how to correctly copy this cv::Mat allocated by OpenCV function data to kuvio_image_alloc_copy(const kuvio_image* source) because it expects kuvio_image data type

  • @luqt123
    Hi Lukas,

    Assuming that you have already allocated space for your kuvio_image, you can copy it across using OpenCv functions. Possible functions for this are for example cv::cvtColor, cv::mixChannels and cv::merge. Please read OpenCv documentation on how they are used.

    For example, if your opencv image is a 24-bit RGB image, the copying can be done simply as follows:

    int my_opencv_function(/*params,*/  kuvio_image* imgOut)
      if (imgOut->type == kuvio_rgb32_image_type)
          cv::Mat myOpenCvImage = ... // Get or create an RGB image. eg. cv::imread
          /// stuff with the image and copy to kuvio_image
          cv::mixChannels(myOpenCvImage, OpenCvMat(imgOut, OpenCvMat::Rgb24), {0, 0, 1, 1, 2, 2});

    Please bear in mind that the sizes of the allocated kuvio_image and the size of the image in cv::Mat myOpenCvImage must be identical.

  • Thank you Keijo it worked!

Log in to reply