ermig1979 / Simd

C++ image processing and machine learning library with using of SIMD: SSE, AVX, AVX-512, AMX for x86/x64, NEON for ARM.
http://ermig1979.github.io/Simd
MIT License
2.06k stars 412 forks source link

build error View = Mat #147

Closed wangzhankun closed 3 years ago

wangzhankun commented 3 years ago

I have included the Simd/SimdLib.hpp, and I defined #define SIMD_OPENCV_ENABLE, but when I build, I run into error.

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video/tracking.hpp>
#define SIMD_OPENCV_ENABLE
#include "Simd/SimdLib.hpp"

#include <thread>
#include <atomic>
#include "ShareSpace.h"
#include "Controller.h"
        img.resource = camera->getFrame();
        if(!img.resource.empty())
        {
            // cv::resize(img.resource, img.resource, cv::Size(640,512));
            Simd::View viewsrc = img.resource;
            cv::Mat tmp= cv::Mat::zeros(cv::Size(640,512), img.resource.type());
            Simd::View viewdest = tmp;
            Simd::ResizeBilinear(viewsrc, viewdest);
            img.resource = tmp;
        }

error

error: Share/ThreadManager/ThreadManager.cpp:91:38: error: class template argument deduction failed:
   91 |             Simd::View viewsrc = img.resource;
      |                                      ^~~~~~~~
Share/ThreadManager/ThreadManager.cpp:91:38: error: no matching function for call to ‘View(cv::Mat&)’
In file included from /usr/include/Simd/SimdLib.hpp:27,
                 from Share/ThreadManager/ThreadManager.h:19,
                 from Share/ThreadManager/ThreadManager.cpp:10:
/usr/include/Simd/SimdView.hpp:819:52: note: candidate: ‘template<template<class> class A> View(const Simd::Point<long int>&, Simd::View<A>::Format)-> Simd::View<A>’
  819 |     template <template<class> class A> SIMD_INLINE View<A>::View(const Point<ptrdiff_t> & size, Format f)
      |                                                    ^~~~~~~
/usr/include/Simd/SimdView.hpp:819:52: note:   template argument deduction/substitution failed:
Share/ThreadManager/ThreadManager.cpp:91:38: note:   candidate expects 2 arguments, 1 provided
   91 |             Simd::View viewsrc = img.resource;
      |                                      ^~~~~~~~
In file included from /usr/include/Simd/SimdLib.hpp:27,
                 from Share/ThreadManager/ThreadManager.h:19,
                 from Share/ThreadManager/ThreadManager.cpp:10
wangzhankun commented 3 years ago

I even defined #define SIMD_OPENCV_ENABLE in the SimdView.hpp, but it dose nothing.

ermig1979 commented 3 years ago

Hi! What is type of img.resource ? Try to do something like this:

cv::Mat src = img.resource;
Simd::View viewSrc = src;
wangzhankun commented 3 years ago

img.resource is cv::Mat type. And now I find the reason. I have to typedef View firstly, typedef Simd::View<Simd::Allocator> View;. But yesterday I don't know that.

wangzhankun commented 3 years ago

I have another question, if simd have implemented cv::inRange function? I have a need that I want convert RGB into HSV and then execute cv::inRange function. I find that I cannot use cv_mat_hsv = simd_view_hsv to convert View into cv:Mat type and use the cv::inRange function.

ermig1979 commented 3 years ago

Unfortunately, there is no function in Simd with described functionality.

wangzhankun commented 3 years ago

But why cannot convert View in HSV color space into cv::Mat in HSV color space? I think there is no special data structure in Simd::View or cv::Mat since that you have implemented the function View to Mat in RGB color space.

ermig1979 commented 3 years ago

You can use another type of Simd::View constructor:

    /*!
        Creates a new View structure with specified width, height, row size, pixel format and pointer to pixel data.

        \param [in] w - a width of created image view.
        \param [in] h - a height of created image view.
        \param [in] s - a stride (row size) of created image view.
        \param [in] f - a pixel format of created image view.
        \param [in] d - a pointer to the external buffer with pixel data. 
                                If this pointer is NULL then will be created own buffer.
    */
    View(size_t w, size_t h, ptrdiff_t s, Format f, void * d);

It is more universal method to create View:

    View view(mat.cols, mat.rows, mat.step[0], View::Hsv24, mat.data);
wangzhankun commented 3 years ago

Sorry for my poor English. I confused you.

    typedef Simd::View<Simd::Allocator> View;
    View view_tmp = this->src_img;
    View dst(this->src_img.cols, this->src_img.rows, Simd::View<Simd::Allocator>::Format::Hsv24);
    cout << dst.Size().x << " " << dst.Size().y << endl;
    Simd::BgrToHsv(view_tmp, dst);
    cout << "hello" << endl;
    Mat tmp = dst;
    // Mat tmp;
    cout << "world" << endl;

At the 7th line there is an issue when running:

/usr/local/include/Simd/SimdView.hpp:1142: static int Simd::View<A>::ToOcv(Simd::View<A>::Format) [with A = Simd::Allocator]: Assertion `0' failed.
error: execv(/home/wang/Documents/ComputerVision/build/linux/x86_64/debug/CV) failed(-1)

I want to know how to convert View in HSV color space into cv::Mat?

ermig1979 commented 3 years ago

You can use universal constructor for output View too.

typedef Simd::View<Simd::Allocator> View;
View src = this->src_img;
Mat tmp(this->src_img.cols, this->src_img.rows, CV_8UC3);
View dst(tmp.cols, tmp.rows, tmp.step[0], View::Hsv24, tmp.data);
Simd::BgrToHsv(src, dst);

or use С API function:

Mat tmp(this->src_img.cols, this->src_img.rows, CV_8UC3);
SimdBgaToHsv(this->src_img.data, this->src_img.cols, this->src_img.rows, this->src_img.step[0], tmp.data, tmp.step[0]);
wangzhankun commented 3 years ago

It seems that Simd::BgrToHsv is much slower than cv::cvtColor, although Simd::Resize is a little faster than cv::resize. But also for you patience on answering my question and your work on simd. Thanks.

ermig1979 commented 3 years ago

As I can see there is no optimizations for Sims::BgrToHsv. I did not use this function widely before.