UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Participant bhavinlapasia
Participant
826 Views
Registered: ‎05-18-2018

Mean and standard deviation of image using HLS and OpenCV

Jump to solution
/////////////////////// top.h file
#ifndef _TOP_H_
#define _TOP_H_

#include "hls_video.h"

// maximum image size
#define MAX_WIDTH  1920
#define MAX_HEIGHT 1080

// I/O Image Settings

#define INPUT_IMAGE           "D:\\workspace\\xilinx\\2015.4\\HLS\\demo_meanSD\\test_1080p.bmp"

typedef hls::stream<ap_axiu<24,1,1,1> >               AXI_STREAM_RGB;
typedef hls::Scalar<3, unsigned char>                 RGB_PIXEL;
typedef hls::Mat<MAX_HEIGHT, MAX_WIDTH, HLS_8UC3>     RGB_IMAGE;

// top level function for HW synthesis
void image_proc(AXI_STREAM_RGB& src_axi, int rows, int cols, RGB_PIXEL mean, RGB_PIXEL sdv);

#endif

//////////////////////// top.cpp
#include "top.h"
#include "hls_video.h"


void image_proc(AXI_STREAM_RGB& video_in, int rows, int cols, RGB_PIXEL mean, RGB_PIXEL sdv) {
    //Create AXI streaming interfaces for the core
#pragma HLS INTERFACE axis port=video_in bundle=INPUT_STREAM

#pragma HLS INTERFACE s_axilite port=rows bundle=CONTROL_BUS offset=0x14
#pragma HLS INTERFACE s_axilite port=cols bundle=CONTROL_BUS offset=0x1C
#pragma HLS INTERFACE s_axilite port=mean bundle=CONTROL_BUS offset=0x24
#pragma HLS INTERFACE s_axilite port=stdv bundle=CONTROL_BUS offset=0x2C
#pragma HLS INTERFACE s_axilite port=return bundle=CONTROL_BUS

#pragma HLS INTERFACE ap_stable port=rows
#pragma HLS INTERFACE ap_stable port=cols

    RGB_IMAGE img_in(rows, cols);

#pragma HLS dataflow
	hls::AXIvideo2Mat(video_in, img_in);
	hls::AvgSdv(img_in, mean, sdv);
}

////////////////////////////// test.cpp
#include "hls_opencv.h"
#include "top.h"
using namespace cv;

int main (int argc, char** argv) {
    Mat src_rgb = imread(INPUT_IMAGE);
    if (!src_rgb.data) {
        printf("ERROR: could not open or find the input image!\n");
        return -1;
    }

    IplImage src=src_rgb;
    RGB_PIXEL hls_mean, hls_sdv;
	cv::Scalar cv_mean, cv_sdv;
    AXI_STREAM_RGB src_axi;
    IplImage2AXIvideo(&src, src_axi);
    image_proc(src_axi, 1080, 1920, hls_mean, hls_sdv);

    meanStdDev(src_rgb, cv_mean, cv_sdv);
    printf("HLS - mean [ %d %d %d ]\n",hls_mean.val[0],hls_mean.val[1],hls_mean.val[2]);
    printf("HLS - sdv [ %d %d %d ]\n",hls_sdv.val[0],hls_sdv.val[1],hls_sdv.val[2]);

    std::cout << "CV - mean " <<cv_mean << std::endl;
    std::cout << "CV - sdv " <<cv_sdv << std::endl;

}

Output:
   Compiling ../../../test.cpp in debug mode
   Compiling ../../../top.cpp in debug mode
   Generating csim.exe
HLS - mean [ 67 211 116 ]
HLS - sdv [ 206 116 28 ]
CV - mean [52.4302, 51.7068, 51.9715, 0]
CV - sdv [41.8961, 44.2676, 46.3073, 0]
@I [SIM-1] CSim done with 0 errors.

I am trying to compute mean & std. deviation of an image using HLS and verifying it using openCV. The HLS function is hls::AvgSdv and OpenCV function is meanStdDev. I am attaching 3 files top.cpp (top function), top.h (header file) and test.cpp (main function).

When I run simulation, the output generated is also displayed. The HLS output data of mean and std. dev is different if my hls::scalar typename (RGB_PIXEL) is changed from char to int or double. I was expecting same output. Please suggest possible errors in doing so.

0 Kudos
1 Solution

Accepted Solutions
Xilinx Employee
Xilinx Employee
684 Views
Registered: ‎09-05-2018

Re: Mean and standard deviation of image using HLS and OpenCV

Jump to solution

Hey @bhavinlapasia ,

hls_mean and hls_sdv are never assigned, so the numbers you're printing are essentially noise.

You meant to assign values to these inside of image_proc(). However, these two variables were passed in by value, whereas they need to be passed in by reference.

So the declaration of image_proc should be: 

void image_proc(AXI_STREAM_RGB& video_in, int rows, int cols, RGB_PIXEL *mean, RGB_PIXEL *sdv)

Then the implementation:

RGB_IMAGE img_in(rows, cols);
hls::AXIvideo2Mat(video_in, img_in);
hls::AvgSdv(img_in, *mean, *sdv);

And then call the top level funtion from main() like so:

image_proc(src_axi, rows, cols, &hls_mean, &hls_sdv);

I noticed that you were using an old version of HLS. Just note for future reference that we recommend the xfOpenCV libraries in the latest versions of Vivado HLS (2018.3+).

Nicholas Moellers

Xilinx Worldwide Technical Support
3 Replies
Xilinx Employee
Xilinx Employee
685 Views
Registered: ‎09-05-2018

Re: Mean and standard deviation of image using HLS and OpenCV

Jump to solution

Hey @bhavinlapasia ,

hls_mean and hls_sdv are never assigned, so the numbers you're printing are essentially noise.

You meant to assign values to these inside of image_proc(). However, these two variables were passed in by value, whereas they need to be passed in by reference.

So the declaration of image_proc should be: 

void image_proc(AXI_STREAM_RGB& video_in, int rows, int cols, RGB_PIXEL *mean, RGB_PIXEL *sdv)

Then the implementation:

RGB_IMAGE img_in(rows, cols);
hls::AXIvideo2Mat(video_in, img_in);
hls::AvgSdv(img_in, *mean, *sdv);

And then call the top level funtion from main() like so:

image_proc(src_axi, rows, cols, &hls_mean, &hls_sdv);

I noticed that you were using an old version of HLS. Just note for future reference that we recommend the xfOpenCV libraries in the latest versions of Vivado HLS (2018.3+).

Nicholas Moellers

Xilinx Worldwide Technical Support
Participant bhavinlapasia
Participant
663 Views
Registered: ‎05-18-2018

Re: Mean and standard deviation of image using HLS and OpenCV

Jump to solution

Thank You Nicholas. It has solved my query.

Can HLS IPs built in 2018.3 using xfOpenCV libraries be used in previous versions of Vivado? 

0 Kudos
Xilinx Employee
Xilinx Employee
645 Views
Registered: ‎09-05-2018

Re: Mean and standard deviation of image using HLS and OpenCV

Jump to solution

@bhavinlapasia,

IP from newer versions of HLS can be used in older version of Vivado, yes. The xfOpenCV libraries in particular have not been tested this way.

If your hardware team is still using an old version of Vivado, that might be because they need to stay on that version to develop for a deprecated part. You may have the same restriction on the HLS side.

Nicholas Moellers

Xilinx Worldwide Technical Support
0 Kudos