diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index 58bbf38..adeecd5 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -66,7 +66,7 @@ static std::once_flag waifu2x_cuda_once_flag; } \ } while (0) -Waifu2x::Waifu2x() : is_inited(false), isCuda(false), block(nullptr), dummy_data(nullptr), out_block(nullptr) +Waifu2x::Waifu2x() : is_inited(false), isCuda(false), input_block(nullptr), dummy_data(nullptr), output_block(nullptr) { } @@ -419,8 +419,8 @@ Waifu2x::eWaifu2xError Waifu2x::ConstractNet(boost::shared_ptr { if (layer_param->mutable_memory_data_param()->width() == original_width_height && layer_param->mutable_memory_data_param()->height() == original_width_height) { - layer_param->mutable_memory_data_param()->set_width(block_size); - layer_param->mutable_memory_data_param()->set_height(block_size); + layer_param->mutable_memory_data_param()->set_width(input_block_size); + layer_param->mutable_memory_data_param()->set_height(input_block_size); } } } @@ -442,7 +442,10 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(boost::shared_ptr Width) + { + right += (x + width) - Width; + width = Width - x; + } + + if (y < 0) + { + top += -y; + height -= -y; + y = 0; + } + + if (y + height > Height) + { + bottom += (y + height) - Height; + height = Height - y; + } + + cv::Mat someimg = im(cv::Rect(x, y, width, height)); + + cv::Mat someimg_tmp; + someimg.copyTo(someimg_tmp); + someimg.release(); + + cv::Mat someborderimg; + // 画像を中央にパディング。余白はcv::BORDER_REPLICATEで埋める + cv::copyMakeBorder(someimg_tmp, someborderimg, top, bottom, left, right, cv::BORDER_REPLICATE); + someimg_tmp.release(); + + // 画像を直列に変換 + { + float *fptr = input_block + (input_block_plane_size * n); + const float *uptr = (const float *)someborderimg.data; + + const auto Line = someborderimg.step1(); + + if (input_block_size == Line) + memcpy(fptr, uptr, input_block_size * input_block_size * sizeof(float)); + else { - float *fptr = block + (input_block_plane_size * n); - const float *uptr = (const float *)someborderimg.data; - - const auto Line = someborderimg.step1(); - - if (block_size == Line) - memcpy(fptr, uptr, block_size * block_size * sizeof(float)); - else - { - for (int i = 0; i < block_size; i++) - memcpy(fptr + i * block_size, uptr + i * Line, block_size * sizeof(float)); - } + for (int i = 0; i < input_block_size; i++) + memcpy(fptr + i * input_block_size, uptr + i * Line, input_block_size * sizeof(float)); } } } } // ネットワークに画像を入力 - input_layer->Reset(block, dummy_data, input_block_plane_size * processNum); + input_layer->Reset(input_block, dummy_data, input_block_plane_size * processNum); // 計算 auto out = net->ForwardPrefilled(nullptr); @@ -527,7 +577,7 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(boost::shared_ptrgpu_data(); - caffe::caffe_copy(output_block_plane_size * processNum, ptr, out_block); + caffe::caffe_copy(output_block_plane_size * processNum, ptr, output_block); for (int n = 0; n < processNum; n++) { @@ -537,11 +587,11 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(boost::shared_ptr= 1); @@ -658,20 +715,20 @@ Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &M return ret; } - const int input_block_plane_size = block_size * block_size; - const int output_block_plane_size = crop_size * crop_size; + const int input_block_plane_size = input_block_size * input_block_size; + const int output_block_plane_size = output_block_size * output_block_size; if (isCuda) { - CUDA_CHECK_WAIFU2X(cudaHostAlloc(&block, sizeof(float) * input_block_plane_size * batch_size, cudaHostAllocWriteCombined)); + CUDA_CHECK_WAIFU2X(cudaHostAlloc(&input_block, sizeof(float) * input_block_plane_size * batch_size, cudaHostAllocWriteCombined)); CUDA_CHECK_WAIFU2X(cudaHostAlloc(&dummy_data, sizeof(float) * input_block_plane_size * batch_size, cudaHostAllocWriteCombined)); - CUDA_CHECK_WAIFU2X(cudaHostAlloc(&out_block, sizeof(float) * output_block_plane_size * batch_size, cudaHostAllocDefault)); + CUDA_CHECK_WAIFU2X(cudaHostAlloc(&output_block, sizeof(float) * output_block_plane_size * batch_size, cudaHostAllocDefault)); } else { - block = new float[input_block_plane_size * batch_size]; + input_block = new float[input_block_plane_size * batch_size]; dummy_data = new float[input_block_plane_size * batch_size]; - out_block = new float[output_block_plane_size * batch_size]; + output_block = new float[output_block_plane_size * batch_size]; } for (size_t i = 0; i < input_block_plane_size * batch_size; i++) @@ -694,15 +751,15 @@ void Waifu2x::destroy() if (isCuda) { - CUDA_HOST_SAFE_FREE(block); + CUDA_HOST_SAFE_FREE(input_block); CUDA_HOST_SAFE_FREE(dummy_data); - CUDA_HOST_SAFE_FREE(out_block); + CUDA_HOST_SAFE_FREE(output_block); } else { - SAFE_DELETE_WAIFU2X(block); + SAFE_DELETE_WAIFU2X(input_block); SAFE_DELETE_WAIFU2X(dummy_data); - SAFE_DELETE_WAIFU2X(out_block); + SAFE_DELETE_WAIFU2X(output_block); } is_inited = false; diff --git a/common/waifu2x.h b/common/waifu2x.h index 76bb298..aca7eeb 100644 --- a/common/waifu2x.h +++ b/common/waifu2x.h @@ -59,7 +59,7 @@ private: int batch_size; // ネットに入力する画像のサイズ - int block_size; + int input_block_size; // ブロック変換後の出力サイズ int output_size; // ネットワークに入力する画像のサイズ(出力画像の幅はlayer_num * 2だけ小さくなる) @@ -73,14 +73,19 @@ private: std::string model_dir; std::string process; + int inner_padding; + int outer_padding; + + int output_block_size; + bool isCuda; boost::shared_ptr> net_noise; boost::shared_ptr> net_scale; - float *block; + float *input_block; float *dummy_data; - float *out_block; + float *output_block; private: eWaifu2xError LoadImage(cv::Mat &float_image, const std::string &input_file);