Skip to content

Instantly share code, notes, and snippets.

@nvnnghia
Created October 1, 2021 00:48
Show Gist options
  • Save nvnnghia/b08283d1941b426349e3fe9646569e59 to your computer and use it in GitHub Desktop.
Save nvnnghia/b08283d1941b426349e3fe9646569e59 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
using namespace std::chrono;
auto start = high_resolution_clock::now();
#define MAX_VALSTR 64
typedef struct _DATABAR_INFO
{
char szOffenceCode[MAX_VALSTR];
char szGPSCoord[MAX_VALSTR];
char szDate[MAX_VALSTR];
char szCamSerial[MAX_VALSTR];
char szLocation[MAX_VALSTR];
char szDirection[MAX_VALSTR];
char szLane[MAX_VALSTR];
char szSpeed[MAX_VALSTR];
} DATABAR_INFO, *LPDATABAR_INFO;
static cv::Mat do_databar(
cv::Mat& imgSrc,
LPDATABAR_INFO pInfo)
{
int nWidth = 0;
int nHeight = 0;
nHeight = imgSrc.rows;
nWidth = imgSrc.cols;
cv::Mat imgDst(imgSrc);
cv::Mat imgDataBar(600, 4000, CV_8UC1);
if(pInfo){
const int nThickness = 4;
char lbl_code[256];
sprintf(lbl_code, "Offence Code: %s", pInfo->szOffenceCode);
char lbl_gps_coor[256];
sprintf(lbl_gps_coor, "GPS Coordinates: %s", pInfo->szGPSCoord);
char lbl_date[256];
sprintf(lbl_date, "Date: %s", pInfo->szDate);
char lbl_cam_ser[256];
sprintf(lbl_cam_ser, "Camera Serial: %s", pInfo->szCamSerial);
char lbl_loc[256];
sprintf(lbl_loc, "Location: %s", pInfo->szLocation);
char lbl_direction[256];
sprintf(lbl_direction, "Direction: %s", pInfo->szDirection);
char lbl_lane[256];
sprintf(lbl_lane, "Lane: %s", pInfo->szLane);
char lbl_speed[256];
sprintf(lbl_speed, "Speed: %s", pInfo->szSpeed);
imgDataBar.setTo(cv::Scalar(0));
cv::Size textSize_loc = getTextSize(lbl_loc, cv::FONT_HERSHEY_DUPLEX, 1.9, nThickness, 0);
cv::Size textSize_direction = getTextSize(lbl_direction, cv::FONT_HERSHEY_DUPLEX, 1.9, nThickness, 0);
cv::Size textSize_lane = getTextSize(lbl_lane, cv::FONT_HERSHEY_DUPLEX, 1.9, nThickness, 0);
cv::Size textSize_speed = getTextSize(lbl_speed, cv::FONT_HERSHEY_DUPLEX, 1.9, nThickness, 0);
int label_width = std::max({textSize_loc.width, textSize_direction.width, textSize_lane.width, textSize_speed.width});
int right_col = 3800 - label_width;
cv::putText(imgDataBar, lbl_code, cv::Point(100, 100),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_gps_coor, cv::Point(100, 200),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_date, cv::Point(100, 300),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_cam_ser, cv::Point(100, 400),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_loc, cv::Point(right_col, 100),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_direction, cv::Point(right_col, 200),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_lane, cv::Point(right_col, 300),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
cv::putText(imgDataBar, lbl_speed, cv::Point(right_col, 400),
cv::FONT_HERSHEY_DUPLEX, 1.9, (255, 255, 255), nThickness, cv::LINE_AA);
}
/***************** Merge ****************/
Mat imgResult;
cv::resize(imgDataBar, imgDataBar, cv::Size(nWidth,(int)(600*nWidth/4000)));
cv::vconcat(imgDst, imgDataBar, imgResult);
return imgResult;
}
int main(int argc, char* argv[])
{
char szInFilename[256];
char szOutFilename[256];
DATABAR_INFO info;
if (argc != 11) {
std::cout << "Usage: ourdatabar infile \"infoarray\" outfile" << std::endl;
std::cout << "Info Array Example: \"Mobile Phone\" \"100, 100\" \"2021/09/29\" \"SN:12345\" \"NewYork\" \"West - North\" \"Lane - 3\" \"20km / h\"" << std::endl;
return 0;
}
else {
strcpy(szInFilename, argv[1]);
strcpy(info.szOffenceCode, argv[2]);
strcpy(info.szGPSCoord, argv[3]);
strcpy(info.szDate, argv[4]);
strcpy(info.szCamSerial, argv[5]);
strcpy(info.szLocation, argv[6]);
strcpy(info.szDirection, argv[7]);
strcpy(info.szLane, argv[8]);
strcpy(info.szSpeed, argv[9]);
strcpy(szOutFilename, argv[10]);
}
Mat image;
image = imread(szInFilename, IMREAD_UNCHANGED); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
if (image.channels() != 1) // Check for invalid input
{
cout << "Support only 1 channel image, but current image has " << image.channels() << " channels." << std::endl;
return -1;
}
Mat imgResult = do_databar(image, &info);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " microseconds" << endl;
imwrite(szOutFilename, imgResult);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment