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!

Showing results for 
Search instead for 
Did you mean: 
Registered: ‎09-02-2013

hls::Filter2D and cv::Filter2D gives different results

Been trying to get a simple edge detection to work (as a first step at least), but Filter2D from hls and opencv keeps giving different results.


I used the code from xapp1167 to set up a diffX function


void diffX(RGB_IMAGE& src, RGB_IMAGE& dst)
	const int k_val[3][3] = { 	{-1, 0, 1},
					{-2, 0, 2},
					{-1, 0, 1}, };
    hls::Window<3, 3, int> 	kernel;
    hls::Point_<int> 		anchor;
    for (int i = 0; i < 3; i++)
    	for (int j = 0; j < 3; j++)
            kernel.val[i][j] = k_val[i][j];

    anchor.x = -1;
    anchor.y = -1;

    hls::Filter2D(src, dst, kernel, anchor);

 And the openCV equivalent is:

	Mat kernXdiff = (Mat_<int>(3,3)  <<	-1, 0, 1,
						-2, 0, 2,
						-1, 0, 1);
	filter2D(src_opencv, diffx, OPENCV_TYPE, kernXdiff);


I use normalize in the test bench for both HLS and openCV images to get them back to 0-255 before saving the image 

normalize(dst_hls, dst_hls, 0, 255, NORM_MINMAX);


Been trying with different data types for both the HLS and CV part, both with and without color, but it's always different results.


For example, the latest attempt used data types:




HLS result: http://i.imgur.com/ycxVauF.png

OpenCV result: http://i.imgur.com/k2WPylF.png


Anyone got similar problems?

0 Kudos
4 Replies
Registered: ‎04-17-2011

Re: hls::Filter2D and cv::Filter2D gives different results

Which one is correct? :)
Kindly note- Please mark the Answer as "Accept as solution" if information provided is helpful.

Give Kudos to a post which you think is helpful and reply oriented.
0 Kudos
Registered: ‎09-02-2013

Re: hls::Filter2D and cv::Filter2D gives different results

I was writing up a long response about cvtColor converting weird when I realised that the cv::Mat in the test bench that gets the hls results back from the hls function was also defined with CV_8UC3, making the output only fill one of the channels making the output blue.


So now I got black and white to work:



but I don't want to work with only unsigned 8-bit. I need at least 16-bit and signed type to detect both rising and falling edges. How do I change type in HLS


in OpenCV I could do 

filter2D(src_opencv, diffx, CV_32F, kernXdiff);

even when the input (src_opencv) is CV_8U


Now I also tried with




But there are slight differences in the images:

HLS result http://i.imgur.com/fWLUwLQ.png

CV result http://i.imgur.com/33dH4CL.png


BTW original image is: http://i.imgur.com/XPtmkKb.png


0 Kudos
Registered: ‎09-02-2013

Re: hls::Filter2D and cv::Filter2D gives different results

OK, So now I'm back at where I originally was, with the exception that RGB to Grey conversion seems to work now.


The HLS Mat's are defined as HLS_16SC3 (The AXI-video stream is set to 48-bit to allow this)




The HLS part is only:

#pragma HLS dataflow
	hls::AXIvideo2Mat(input, img_0);
	hls::CvtColor<HLS_RGB2GRAY>(img_0, img_1);
	diffX(img_1, img_dx);
	hls::Mat2AXIvideo(img_dx, output);

diffX is the same as in original post.


The hls filter is called upon in the test bench as so:

	cvMat2AXIvideo(src_hls, axi_src);
	hls_filter(axi_src, axi_dst, MAX_HEIGHT, MAX_WIDTH);
	dst_hls = Mat::zeros(MAX_HEIGHT, MAX_WIDTH, CV_16S);
	AXIvideo2cvMat(axi_dst, dst_hls);
	normalize(dst_hls, dst_hls, 0, 255, NORM_MINMAX, CV_8U);
	imwrite(IMG_OUT_HLS, dst_hls);

Giving this result: http://i.imgur.com/zyeyrDR.png

which I guess looks correct.


But then compare to when running the OpenCV part of test bench (kernXdiff is the same as in Original post):

	cvtColor(src_opencv, src_opencv, CV_RGB2GRAY);
	filter2D(src_opencv, diffx, CV_16S, kernXdiff);
	normalize(diffx, dst_opencv, 0, 255, NORM_MINMAX, CV_8U);
	imwrite(IMG_OUT_OPENCV, dst_opencv);

I get: http://i.imgur.com/blSvRpG.png

While it also looks correct it, for starters, flipped the kernel so that high intensity -> low intensity is black, instead of white as in the hls filter.

But even when I tried to flip the kernel and run again, i got this: http://i.imgur.com/TKOVFil.png which is still different from the HLS filter result. I don't know what the difference is though.

I've been battling with datatypes and channels for days now and it's all just feels lika a mess

0 Kudos
Registered: ‎09-02-2013

Re: hls::Filter2D and cv::Filter2D gives different results

The problem was probably in 

hls::AXIvideo2Mat(input, img_0);

Since the input is actually 3-channel 8-bit image, it got messed up by this function when img_0 was 3-channel 16-bit

I added a line in the test bench before the hls filter call:

src_hls.convertTo(src_hls, CV_16SC3);

So src_hls is converted from CV_8UC3 -> CV_16SC3 in a proper way.


still don' t know how that would be done in HLS though

0 Kudos