From 7d1b7385698ab70049ebee0fb7dc0727b6667107 Mon Sep 17 00:00:00 2001 From: lltcggie Date: Sat, 19 Mar 2016 18:15:09 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8B=A1=E5=A4=A7=E5=BE=8C=E3=81=AE=E3=82=B5?= =?UTF-8?q?=E3=82=A4=E3=82=BA=E3=82=92=E7=B8=A6=E5=B9=85=E3=81=8B=E6=A8=AA?= =?UTF-8?q?=E5=B9=85=E3=81=A7=E6=8C=87=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/waifu2x.cpp | 53 +++++++++++++++++++++++++++++++----- common/waifu2x.h | 13 +++++++-- waifu2x-caffe-gui/Source.cpp | 19 ++++++++++++- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index baad8c7..e32706e 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -1138,7 +1138,9 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(boost::shared_ptr ScaleRatio, const boost::optional ScaleWidth, const boost::optional ScaleHeight, + const boost::filesystem::path &ModelDir, const std::string &Process, const boost::optional OutputQuality, const int OutputDepth, const bool UseTTA, const int CropSize, const int BatchSize) { Waifu2x::eWaifu2xError ret; @@ -1146,7 +1148,22 @@ Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &M if (is_inited) return eWaifu2xError_OK; - if (ScaleRatio <= 0.0) + int valid_num = 0; + if (ScaleRatio) + valid_num++; + if (ScaleWidth) + valid_num++; + if (ScaleHeight) + valid_num++; + + if (valid_num != 1) + return eWaifu2xError_InvalidParameter; + + if (ScaleRatio && *ScaleRatio <= 0.0) + return eWaifu2xError_InvalidParameter; + if (ScaleWidth && *ScaleWidth <= 0) + return eWaifu2xError_InvalidParameter; + if (ScaleHeight && *ScaleHeight <= 0.) return eWaifu2xError_InvalidParameter; try @@ -1154,6 +1171,8 @@ Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &M mode = Mode; noise_level = NoiseLevel; scale_ratio = ScaleRatio; + scale_width = ScaleWidth; + scale_height = ScaleHeight; model_dir = ModelDir; process = Process; use_tta = UseTTA; @@ -1481,7 +1500,8 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructFloatMat(const bool isReconstructNois if (cancel_func && cancel_func()) return eWaifu2xError_Cancel; - const int scale2 = ceil(log2(scale_ratio)); + const double ratio = CalcScaleRatio(image_size); + const int scale2 = ceil(log2(ratio)); if (isReconstructScale) { @@ -1630,8 +1650,9 @@ Waifu2x::eWaifu2xError Waifu2x::AfterReconstructFloatMatProcess(const bool isRec cv::merge(planes, process_image); } - const int scale2 = ceil(log2(scale_ratio)); - const double shrinkRatio = scale_ratio / std::pow(2.0, (double)scale2); + const double ratio = CalcScaleRatio(image_size); + const int scale2 = ceil(log2(ratio)); + const double shrinkRatio = ratio / std::pow(2.0, (double)scale2); cv::Mat alpha; if (floatim.channels() == 4) @@ -1726,6 +1747,17 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2xConvetedMat(const bool isJpeg, const cv:: return eWaifu2xError_OK; } +double Waifu2x::CalcScaleRatio(const cv::Size_ &size) const +{ + if (scale_ratio) + return *scale_ratio; + + if (scale_width) + return (double)*scale_width / (double)size.width; + + return (double)*scale_height / (double)size.height; +} + Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file, const waifu2xCancelFunc cancel_func) { @@ -1810,13 +1842,20 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(double factor, const void* source, void* float_image = convert; } - const auto oldScale = scale_ratio; + const auto oldScaleRatio = scale_ratio; + const auto oldScaleWidth = scale_width; + const auto oldScaleHeight = scale_height; + scale_ratio = factor; + scale_width.reset(); + scale_height.reset(); cv::Mat write_iamge; ret = waifu2xConvetedMat(false, float_image, write_iamge); - scale_ratio = oldScale; + scale_ratio = oldScaleRatio; + scale_width = oldScaleWidth; + scale_height = oldScaleHeight; if (ret != eWaifu2xError_OK) return ret; diff --git a/common/waifu2x.h b/common/waifu2x.h index a6a165c..7a4a73d 100644 --- a/common/waifu2x.h +++ b/common/waifu2x.h @@ -88,7 +88,9 @@ private: std::string mode; int noise_level; - double scale_ratio; + boost::optional scale_ratio; + boost::optional scale_width; + boost::optional scale_height; boost::filesystem::path model_dir; std::string process; @@ -136,6 +138,8 @@ private: eWaifu2xError waifu2xConvetedMat(const bool isJpeg, const cv::Mat &inMat, cv::Mat &outMat, const waifu2xCancelFunc cancel_func = nullptr); + double CalcScaleRatio(const cv::Size_ &size) const; + static int DepthBitToCVDepth(const int depth_bit); static double GetValumeMaxFromCVDepth(const int cv_depth); static double GetEPS(const int cv_depth); @@ -152,8 +156,11 @@ public: // mode: noise or scale or noise_scale or auto_scale // process: cpu or gpu or cudnn - eWaifu2xError init(int argc, char** argv, const std::string &mode, const int noise_level, const double scale_ratio, const boost::filesystem::path &model_dir, const std::string &process, - const boost::optional output_quality = boost::optional(), const int output_depth = 8, const bool use_tta = false, const int crop_size = 128, const int batch_size = 1); + eWaifu2xError init(int argc, char** argv, const std::string &mode, const int noise_level, + const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + const boost::filesystem::path &model_dir, const std::string &process, + const boost::optional output_quality = boost::optional(), const int output_depth = 8, const bool use_tta = false, + const int crop_size = 128, const int batch_size = 1); void destroy(); diff --git a/waifu2x-caffe-gui/Source.cpp b/waifu2x-caffe-gui/Source.cpp index c01e679..e0b2246 100644 --- a/waifu2x-caffe-gui/Source.cpp +++ b/waifu2x-caffe-gui/Source.cpp @@ -642,8 +642,25 @@ private: Waifu2x::eWaifu2xError ret; + boost::optional ScaleRatio; + boost::optional ScaleWidth; + boost::optional ScaleHeight; + + switch (scaleType) + { + case eScaleTypeRatio: + ScaleRatio = scale_ratio; + break; + case eScaleTypeWidth: + ScaleWidth = scale_width; + break; + default: + ScaleHeight = scale_height; + break; + } + Waifu2x w; - ret = w.init(__argc, __argv, mode, noise_level, scale_ratio, model_dir, process, output_quality, output_depth, use_tta, crop_size, batch_size); + ret = w.init(__argc, __argv, mode, noise_level, ScaleRatio, ScaleWidth, ScaleHeight, model_dir, process, output_quality, output_depth, use_tta, crop_size, batch_size); if(ret != Waifu2x::eWaifu2xError_OK) SendMessage(dh, WM_ON_WAIFU2X_ERROR, (WPARAM)&ret, 0); else