17 #include "../include/common.hpp"
34 const string inpFileName,
int simCnt) {
36 ifstream inpFileStream;
37 string datFile =
"../../../../test/" + inpFileName;
41 inpFileStream.open(datFile.c_str());
42 if ( !inpFileStream ) {
43 cout <<
"### ERROR : Could not open the input data file " << datFile << endl;
48 while (inpFileStream) {
50 if (!inpFileStream.eof()) {
52 getline(inpFileStream, strLine);
53 if (strLine.empty())
continue;
54 sscanf(strLine.c_str(),
"%llx %x %d", &udpWord.
tdata, &udpWord.
tkeep, &udpWord.
tlast);
57 if (sDataStream.full()) {
58 printf(
"### ERROR : Stream is full. Cannot write stream with data from file \"%s\".\n", inpFileName.c_str());
61 sDataStream.write(udpWord);
63 printf(
"[%4.4d] TB is filling input stream [%s] - Data write = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
64 simCnt, dataStreamName.c_str(),
65 udpWord.
tdata.to_long(), udpWord.
tkeep.to_int(), udpWord.
tlast.to_int());
71 inpFileStream.close();
87 ifstream inpFileStream;
88 string datFile =
"../../../../test/" + inpFileName;
90 unsigned int index = 0;
91 ap_uint<OUTPUT_PTR_WIDTH> current_tdata;
94 inpFileStream.open(datFile.c_str());
95 if ( !inpFileStream ) {
96 cout <<
"### ERROR : Could not open the input data file " << datFile << endl;
101 while (inpFileStream) {
103 if (!inpFileStream.eof()) {
105 getline(inpFileStream, strLine);
106 if (strLine.empty())
continue;
107 sscanf(strLine.c_str(),
"%llx %x %d", &udpWord.
tdata, &udpWord.
tkeep, &udpWord.
tlast);
109 current_tdata = (ap_uint<OUTPUT_PTR_WIDTH>)(udpWord.
tdata >> i*8);
111 imgArray[index++] = current_tdata;
113 printf(
"[%4.4d] TB is filling input array from [%s] - Data write = {D=0x%16.16llX, K=0x%2.2X, L=%d, current_tdata=0x%16.16llX} \n",
114 simCnt, inpFileName.c_str(),
115 udpWord.
tdata.to_long(), udpWord.
tkeep.to_int(), udpWord.
tlast.to_int(),
116 current_tdata.to_long());
122 inpFileStream.close();
139 sDataStream.read(*udpWord);
155 value = (value << 8 ) + buffer[6] ;
156 value = (value << 8 ) + buffer[5] ;
157 value = (value << 8 ) + buffer[4] ;
158 value = (value << 8 ) + buffer[3] ;
159 value = (value << 8 ) + buffer[2] ;
160 value = (value << 8 ) + buffer[1] ;
161 value = (value << 8 ) + buffer[0] ;
175 if (!outFileStream.is_open()) {
176 printf(
"### ERROR : Output file stream is not open. \n");
179 outFileStream << hex << noshowbase << setfill(
'0') << setw(16) << udpWord->
tdata.to_uint64();
180 outFileStream <<
" ";
181 outFileStream << hex << noshowbase << setfill(
'0') << setw(2) << udpWord->
tkeep.to_int();
182 outFileStream <<
" ";
183 outFileStream << setw(1) << udpWord->
tlast.to_int() <<
"\n";
197 const string dataStreamName,
const string outFileName,
int simCnt)
200 ofstream outFileStream;
201 string datFile =
"../../../../test/" + outFileName;
206 outFileStream.open(datFile.c_str());
207 if ( !outFileStream ) {
208 cout <<
"### ERROR : Could not open the output data file " << datFile << endl;
213 while (!sDataStream.empty()) {
216 printf(
"[%4.4d] TB is draining output stream [%s] - Data read = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
217 simCnt, dataStreamName.c_str(),
218 udpWord.
tdata.to_long(), udpWord.
tkeep.to_int(), udpWord.
tlast.to_int());
227 outFileStream.close();
241 const string outFileName,
int simCnt)
244 ofstream outFileStream;
245 string datFile =
"../../../../test/" + outFileName;
248 unsigned int bytes_per_line = 8;
251 outFileStream.open(datFile.c_str());
252 if ( !outFileStream ) {
253 cout <<
"### ERROR : Could not open the output data file " << datFile << endl;
256 printf(
"came to dumpImgToFile: _img.rows=%u, img.cols=%u\n", _img.rows, _img.cols);
258 ap_uint<8> value[bytes_per_line];
261 for (
unsigned int total_bytes = 0, chan =0 ; chan < _img.channels(); chan++) {
262 for (
unsigned int j = 0; j < _img.rows; j++) {
264 for (
unsigned int i = 0; i < (_img.cols >> XF_BITSHIFT(
NPIX)); i+=bytes_per_line, total_bytes+=bytes_per_line) {
266 for (
unsigned int k = 0; k < bytes_per_line; k++) {
267 value[k] = _img.read(j * (_img.cols >> XF_BITSHIFT(
NPIX)) + i + k);
272 if ((total_bytes >= (_img.rows * _img.cols * _img.channels() - bytes_per_line)) ||
273 ((total_bytes + bytes_per_line) %
PACK_SIZE == 0)) {
279 printf(
"[%4.4d] IMG TB is dumping image to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
280 simCnt, datFile.c_str(), total_bytes, value,
281 udpWord.
tdata.to_long(), udpWord.
tkeep.to_int(), udpWord.
tlast.to_int());
291 outFileStream.close();
305 const string outFileName,
int simCnt,
float * transform_matrix)
308 ofstream outFileStream;
309 string datFile =
"../../../../test/" + outFileName;
312 unsigned int bytes_per_line = 8;
315 outFileStream.open(datFile.c_str());
316 if ( !outFileStream ) {
317 cout <<
"### ERROR : Could not open the output data file " << datFile << endl;
320 printf(
"came to dumpImgToFile: _img.rows=%u, img.cols=%u\n", _img.rows, _img.cols);
322 ap_uint<8> value[bytes_per_line];
323 ap_uint<8> tx_cmd[bytes_per_line];
324 ap_uint<8> img_cmd[bytes_per_line];
326 for (
unsigned int k = 0; k < bytes_per_line; k++) {
330 img_cmd[k] = (char)0;
334 img_cmd[k] = (char)2;
344 outFileStream.close();
348 for (
int i = 0; i < 8; i++)
350 memcpy(value+off, (
float*)transform_matrix+i, 4);
352 off = off % bytes_per_line;
353 std::cout <<
"[DEBUG] off=" << off <<
" tx mat=" << transform_matrix[i] <<
" idx=" << i <<
" flt val=" << value[off] <<
" " << value[off+1] <<
" " << value[off+2] <<
" " << value[off+3]<< std::endl;
356 std::cout <<
"[DEBUG] packing the valua :D" << std::endl;
362 outFileStream.close();
370 unsigned int zero_constant = 0;
371 memcpy(value, (
char*)transform_matrix+8, 4);
372 memcpy(value, (
char*)&zero_constant, 4);
378 outFileStream.close();
383 memcpy(img_cmd+6, (
char*)&_img.rows, 2);
384 memcpy(img_cmd+4, (
char*)&_img.cols, 2);
385 img_cmd[1]=_img.channels();
392 outFileStream.close();
396 for (
unsigned int total_bytes = 0, chan =0 ; chan < _img.channels(); chan++) {
397 for (
unsigned int j = 0; j < _img.rows; j++) {
399 for (
unsigned int i = 0; i < (_img.cols >> XF_BITSHIFT(
NPIX)); i+=bytes_per_line, total_bytes+=bytes_per_line) {
401 for (
unsigned int k = 0; k < bytes_per_line; k++) {
402 value[k] = _img.read(j * (_img.cols >> XF_BITSHIFT(
NPIX)) + i + k);
407 if ((total_bytes >= (_img.rows * _img.cols * _img.channels() - bytes_per_line)) ||
408 ((total_bytes + bytes_per_line) %
PACK_SIZE == 0)) {
414 printf(
"[%4.4d] IMG TB is dumping image to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
415 simCnt, datFile.c_str(), total_bytes, value,
416 udpWord.
tdata.to_long(), udpWord.
tkeep.to_int(), udpWord.
tlast.to_int());
426 outFileStream.close();
438 std::vector<cv::Point>& hls_points,
439 std::vector<cv::Point>& ocv_points,
440 std::vector<cv::Point>& common_pts) {
442 cv::Mat ocvpnts, hlspnts;
444 ocvpnts = in_img.clone();
446 int nhls = hls_points.size();
449 for (
int j = 1; j < ocv_out_img.rows - 1; j++) {
450 for (
int i = 1; i < ocv_out_img.cols - 1; i++) {
451 if ((
int)ocv_out_img.at<
unsigned char>(j, i)) {
452 cv::circle(ocvpnts, cv::Point(i, j), 5, cv::Scalar(0, 0, 255), 2, 8, 0);
453 ocv_points.push_back(cv::Point(i, j));
458 printf(
"ocv corner count = %d, Hls corner count = %d\n", ocv_points.size(), hls_points.size());
459 int nocv = ocv_points.size();
465 int ocv_x, ocv_y, hls_x, hls_y;
466 for (
int j = 0; j < nocv; j++) {
467 for (
int k = 0; k < nhls; k++) {
468 ocv_x = ocv_points[j].x;
469 ocv_y = ocv_points[j].y;
470 hls_x = hls_points[k].x;
471 hls_y = hls_points[k].y;
473 if ((ocv_x == hls_x) && (ocv_y == hls_y)) {
474 common_pts.push_back(ocv_points[j]);
480 imwrite(
"output_hls.png", out_img);
481 imwrite(
"output_ocv.png", ocvpnts);
483 float persuccess, perloss, pergain;
485 int totalocv = ocv_points.size();
486 int ncommon = common_pts.size();
487 int totalhls = hls_points.size();
488 persuccess = (((
float)ncommon / totalhls) * 100);
489 perloss = (((
float)(totalocv - ncommon) / totalocv) * 100);
490 pergain = (((
float)(totalhls - ncommon) / totalhls) * 100);
492 printf(
"Commmon = %d\t Success = %f\t Loss = %f\t Gain = %f\n", ncommon, persuccess, perloss, pergain);
494 if (persuccess < 60 || totalhls == 0)
507 cv::Mat& out_img, std::vector<cv::Point>& hls_points) {
509 for (
int j = 0; j < imgOutput.rows; j++) {
511 for (
int i = 0; i < (imgOutput.cols >> XF_BITSHIFT(
NPIX)); i++) {
512 if (
NPIX == XF_NPPC8) {
514 imgOutput.read(j * (imgOutput.cols >> XF_BITSHIFT(
NPIX)) + i);
515 for (
int k = 0; k < 64; k += 8, l++) {
516 uchar pix = value.range(k + 7, k);
521 if ((
tmp.x < in_img.cols) && (
tmp.y < in_img.rows) && (j > 0)) {
522 hls_points.push_back(
tmp);
527 if (j > 0) cv::circle(out_img, cv::Point(
x,
y), 5, cv::Scalar(0, 0, 255, 255), 2, 8, 0);
531 if (
NPIX == XF_NPPC1) {
532 unsigned char pix = imgOutput.read(j * (imgOutput.cols >> XF_BITSHIFT(
NPIX)) + i);
537 if ((
tmp.x < in_img.cols) && (
tmp.y < in_img.rows) && (j > 0)) {
538 hls_points.push_back(
tmp);
543 if (j > 0) cv::circle(out_img, cv::Point(
x,
y), 5, cv::Scalar(0, 0, 255, 255), 2, 8, 0);
void markPointsOnImage(Mat &imgOutput, Mat &in_img, Mat &out_img, vector< Point > &hw_points)
Mark the points found by Gammacorrection into the image.
ap_uint< 64 > pack_ap_uint_64_(ap_uint< 8 > *buffer)
Pack an array of 8 x ap_uint<8> into a ap_uint<64> word.
bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream)
Dump a data word to a file.
bool setInputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string inpFileName, int simCnt)
Initialize an input data stream from a file.
bool getOutputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string outFileName, int simCnt)
Fill an output file with data from an output stream.
#define BITS_PER_10GBITETHRNET_AXI_PACKET
bool readDataStream(stream< UdpAppData > &sDataStream, UdpAppData *udpWord)
Read data from a stream.
bool dumpImgToFile(xf::cv::Mat< OUT_TYPE, HEIGHT, WIDTH, NPIX > &_img, const string outFileName, int simCnt)
Fill an output file with data from an image.
bool dumpImgToFileWarpTransform(xf::cv::Mat< OUT_TYPE, HEIGHT, WIDTH, NPIX > &_img, const string outFileName, int simCnt, float *transform_matrix)
Fill an output file with data from an image.
unsigned int writeCornersIntoFile(cv::Mat &in_img, cv::Mat &ocv_out_img, cv::Mat &out_img, std::vector< cv::Point > &hls_points, std::vector< cv::Point > &ocv_points, std::vector< cv::Point > &common_pts)
Write the corners found by Harris into a file.
bool setInputFileToArray(const string inpFileName, ap_uint< OUTPUT_PTR_WIDTH > *imgArray, int simCnt)
Initialize an input array from a file with format "tdata tkeep tlast".