Skip to content

Instantly share code, notes, and snippets.

@basilevh
Last active April 27, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basilevh/e2db102f27590c8258764061bd3e82e7 to your computer and use it in GitHub Desktop.
Save basilevh/e2db102f27590c8258764061bd3e82e7 to your computer and use it in GitHub Desktop.
Vivado HLS test bench for a 2D filter, showing how to convert an image to a stream and vice versa.
// Basile Van Hoorick, April 2020
#include "hls_opencv.h"
// (NOTE: the defined constants and included imports in this gist might be incomplete, but shouldn't be too hard to infer)
int test_bf()
{
cv::Mat src = cv::imread(INPUT_PATH, CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat dst = cv::Mat(DHEIGHT, DWIDTH, CV_8U);
hls::stream<depth_t> stream_in("stream_in");
hls::stream<depth_t> stream_out("stream_out");
// Tested data values should be in a range of 0.0 to 1.0
// Input image pixels are in a range of 0 to 255
// Read & convert image
for (int y = 0; y < DHEIGHT; y++)
{
for (int x = 0; x < DWIDTH; x++)
{
depth_t value = depth_t(double(src.at<uchar>(y, x) + 0.5) / double(255.0));
stream_in.write(value);
}
}
// Apply bilateral filter
cout << "Starting bilateral_filter..." << endl;
bilateral_filter_3x3(stream_out, stream_in);
cout << "bilateral_filter finished!" << endl;
// Convert & write image
for (int y = 0; y < DHEIGHT; y++)
{
for (int x = 0; x < DWIDTH; x++)
{
depth_t value = stream_out.read();
dst.at<uchar>(y, x) = uchar(double(value) * double(255.0) + 0.5);
}
}
cv::imwrite(OUTPUT_PATH, dst);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment