24,854
社区成员
发帖
与我相关
我的任务
分享
//图片标准化
cv::Mat im;
int64 start = cv::getTickCount();
int channels = 3;
int bach_size = ori_image_vec.size();
cv::resize(ori_image_vec[0], im, cv::Size(new_width, new_height));
int size = im.rows * im.cols * channels;
std::vector<std::vector<float>> files(bach_size);
files[0].resize(size);
float *image_data = files[0].data();
float *ptr_image_r = image_data;
float *ptr_image_g = image_data + size / 3;
float *ptr_image_b = image_data + size / 3 * 2;
float mean_b = 0.485;
float mean_g = 0.456;
float mean_r = 0.406;
float std_b = 0.229;
float std_g = 0.224;
float std_r = 0.225;
for (int i = 0; i < im.rows; ++i)
{
auto data = im.ptr<uchar>(i);
for (int j = 0; j < im.cols; j++)
{
if (channels > 1)
{
*ptr_image_b++ = (static_cast<float>(*data++) / 255.0 - mean_b) / std_b;
*ptr_image_g++ = (static_cast<float>(*data++) / 255.0 - mean_g) / std_g;
*ptr_image_r++ = (static_cast<float>(*data++) / 255.0 - mean_r) / std_r;
}
}
}
//transpose shape from (h,w,c) to (c,h,w)
for (int k = 0; k < bach_size; k++)
{
for (int c = 0; c < channels; ++c)
{
for (int i = 0; i < new_height; ++i)
{
for (int j = 0; j < new_width; ++j)
{
array.push_back(static_cast<float>(image_data[(i * new_width + j) * 3 + c]));
}
}
}
}
//expend shape from (c,h,w) to (1,c,h,w)
Shape image_shape = Shape(bach_size, channels, new_height, new_width);
NDArray transformed_image = NDArray(image_shape, (*model_p).global_ctx, false);
transformed_image.SyncCopyFromCPU(array.data(), image_shape.Size());
NDArray::WaitAll();
(*model_p).mutex.unlock(); //线程解锁
return transformed_image;