tso2386710
tso2386710

Reputation: 13

Convert halide image to opencv mat faster?

I am trying to convert buffer image to opencv mat by mat.at(i,j),but it runs 20 ms .It's too slow.How to improve the speed ?or another method to convert buffer image to cv::Mat?

    Halide::Func gray;

    Halide::Var x, y;

    gray(x, y) = Halide::cast<uint8_t>( 0.299f * image(x, y, 0) +0.587f * image(x, y, 1) + 0.114f * image(x, y, 2));

    Halide::Var xi,yi;
    gray.tile(x,y,xi,yi,4,4).vectorize(xi,4).parallel(y);

    Halide::Buffer<uint8_t> output = gray.realize(image.width(), image.height());

    Mat h2m = Mat::zeros(image.height(),image.width(), CV_8UC1);

    for( int i=0;i<image.height();i++){
            for(int j=0;j<image.width();j++){
                    h2m.at<uchar>(i,j)=output(j,i); 
            }
    }
    imshow("gray",h2m);

Upvotes: 1

Views: 1164

Answers (1)

Dmitry Kurtaev
Dmitry Kurtaev

Reputation: 833

Just wrap pointer to cv::Mat data:

Halide::Buffer<uint8_t> buf(img.data, width, height);

And realize your function inside it:

gray.realize(buf);

Upvotes: 3

Related Questions