26cv::Mat
GetImage(
const std::string &path,
bool to_rgb) {
27 cv::Mat img = cv::imread(path);
30 throw std::runtime_error(
"Failed to read image: " + path);
33 if (to_rgb && img.channels() == 3) {
34 cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
40void Resize(
const cv::Mat &input_img, cv::Mat *out_img,
int width,
int height,
41 double fx,
double fy,
int interpolation) {
42 ACHECK(
nullptr != out_img);
43 cv::resize(input_img, *out_img, cv::Size(width, height), fx, fy,
47void Normalize(
const std::vector<float> &mean,
const std::vector<float> &
std,
48 float scale, cv::Mat *img) {
52 for (
const auto std_value :
std) {
53 ACHECK(fabs(std_value) > eps);
56 (*img).convertTo(*img, CV_32FC3, scale);
57 for (
int h = 0; h < img->rows; h++) {
58 for (
int w = 0; w < img->cols; w++) {
59 img->at<cv::Vec3f>(h, w)[0] =
60 (img->at<cv::Vec3f>(h, w)[0] - mean[0]) /
std[0];
61 img->at<cv::Vec3f>(h, w)[1] =
62 (img->at<cv::Vec3f>(h, w)[1] - mean[1]) /
std[1];
63 img->at<cv::Vec3f>(h, w)[2] =
64 (img->at<cv::Vec3f>(h, w)[2] - mean[2]) /
std[2];
69std::vector<float>
HWC2CHW(
const cv::Mat &input_img) {
70 int channel = input_img.channels();
71 int width = input_img.cols;
72 int height = input_img.rows;
74 std::vector<cv::Mat> input_channels(channel);
75 cv::split(input_img, input_channels);
77 std::vector<float> result(channel * width * height);
78 auto data = result.data();
79 int channel_length = width * height;
81 for (
int i = 0; i < channel; ++i) {
82 memcpy(data, input_channels[i].data, channel_length *
sizeof(
float));
83 data += channel_length;
void Resize(cv::Mat *img, cv::Mat *img_n, int width, int height)
Image resize function
void Normalize(const std::vector< float > &mean, const std::vector< float > &std, float scale, cv::Mat *img)
Image normalize function
cv::Mat GetImage(const std::string &path, bool to_rgb)
std::vector< float > HWC2CHW(const cv::Mat &input_img)