相关文章推荐
力能扛鼎的键盘  ·  哎呦! ANR.... - 掘金·  1 年前    · 
难过的橙子  ·  logstash ...·  1 年前    · 
憨厚的单杠  ·  IF...ELSE ...·  1 年前    · 

#include
#include
#include
#include

namespace ImageUtils {
// imagenet preprocessing function
cv::Mat preprocess_image(cv::Mat image) {
cv::Mat float_image;
image.convertTo(float_image, CV_32FC3);

float mean[] = {103.939, 116.779, 123.68};

for (int y = 0; y < float_image.cols; ++y) {
for (int x = 0; x < float_image.rows; ++x) {
cv::Vec3f pix_value = float_image.at(y, x);

float red = pix_value.val[0] - mean[0];
float green = pix_value.val[1] - mean[1];
float blue = pix_value.val[2] - mean[2];

float_image.at(y, x) = cv::Vec3f(blue, green, red);
}
}

return float_image;
}

std::vector cv_mat_to_float_array(cv::Mat float_image) {
std::vector array;

array.assign((float*)float_image.datastart, (float*)float_image.dataend);

return array;
}
} // namespace ImageUtils

std::vector preprocess_image(cv::Mat image) {
cv::Mat resized_image;
cv::Mat rgb_image;
cv::Size model_size = cv::Size(224, 224); // for example, 1, 3, 224, 224
cv::resize(image, resized_image, model_size);
cv::cvtColor(resized_image, rgb_image, cv::COLOR_BGR2RGB);

cv::Mat float_image = ImageUtils::preprocess_image(rgb_image);
std::vector input_vector =
ImageUtils::cv_mat_to_float_array(float_image);

return input_vector;
}

其中图像内容cv::Mat转成一维数组vector