我有一張圖片需要作圖片的歸一化和標準化,并且把[h,w,,c]轉成[c.h,w]。現在卡在轉置這邊,這楊的結果與正確結果的數字相同,但是位置不一樣,應該是轉置這邊的問題。
這是我的代碼
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;
auto data1 = im.ptr<uchar>(0);
std::cout << static_cast<float>(*data1) << std::endl;
for (int i = 0; i < im.rows; ++i)
{
auto data = im.ptr<uchar>(i);
for (int j = 0; j < im.cols; j++)
{
*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 < 1; k++)
{
for (int c = 0; c < 3; ++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(1, 3, 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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/62241.html
標籤:機器視覺
