OpenCV图像归一化和标准化问题

attor 2020-04-26 06:22:07
代码部分:
//图片标准化
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;

能帮我看看这部分代码的问题吗,结果与NDArray运算出来的不一样
...全文
274 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
attor 2020-04-28
  • 打赏
  • 举报
回复
已解决,是前面归一化改变了原来Mat的结构

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧