From f3bb4fe1825b6fe2111d9bac379621bfb1b54c52 Mon Sep 17 00:00:00 2001 From: lltcggie Date: Sun, 3 Jul 2016 21:55:20 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B5=E3=82=A4=E3=82=BA=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E5=A4=89=E6=8F=9B=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/stImage.cpp | 10 ++++++++++ common/stImage.h | 3 +++ common/waifu2x.cpp | 32 ++++++++++++++++++------------ common/waifu2x.h | 8 +++++--- waifu2x-caffe-gui/MainDialog.cpp | 34 +++++++++++++++----------------- 5 files changed, 53 insertions(+), 34 deletions(-) diff --git a/common/stImage.cpp b/common/stImage.cpp index 66f5665..a6b4702 100644 --- a/common/stImage.cpp +++ b/common/stImage.cpp @@ -382,6 +382,16 @@ Waifu2x::eWaifu2xError stImage::Load(const void* source, const int width, const return Waifu2x::eWaifu2xError_OK; } +double stImage::GetScaleFromWidth(const int width) const +{ + return (double)width / (double)mOrgSize.width; +} + +double stImage::GetScaleFromHeight(const int height) const +{ + return (double)height / (double)mOrgSize.height; +} + bool stImage::RequestDenoise() const { return mIsRequestDenoise; diff --git a/common/stImage.h b/common/stImage.h index e92caab..010f616 100644 --- a/common/stImage.h +++ b/common/stImage.h @@ -89,6 +89,9 @@ public: // sourceはPostprocess()が終わるまで存在している必要がある Waifu2x::eWaifu2xError Load(const void* source, const int width, const int height, const int channel, const int stride); + double GetScaleFromWidth(const int width) const; + double GetScaleFromHeight(const int width) const; + bool RequestDenoise() const; // 前処理 diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index 4195a9b..eb347e5 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -401,7 +401,8 @@ boost::filesystem::path Waifu2x::GetInfoPath(const boost::filesystem::path &mode } Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file, - const double factor, const waifu2xCancelFunc cancel_func, const int crop_w, const int crop_h, + const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + const waifu2xCancelFunc cancel_func, const int crop_w, const int crop_h, const boost::optional output_quality, const int output_depth, const bool use_tta, const int batch_size) { @@ -420,7 +421,8 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_fil const bool isReconstructNoise = mMode == eWaifu2xModelTypeNoise || mMode == eWaifu2xModelTypeNoiseScale || (mMode == eWaifu2xModelTypeAutoScale && image.RequestDenoise()); const bool isReconstructScale = mMode == eWaifu2xModelTypeScale || mMode == eWaifu2xModelTypeNoiseScale || mMode == eWaifu2xModelTypeAutoScale; - double Factor = factor; + double Factor = CalcScaleRatio(scale_ratio, scale_width, scale_height, image); + if (!isReconstructScale) Factor = 1.0; @@ -482,6 +484,21 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(const double factor, const void* source, return Waifu2x::eWaifu2xError_OK; } +double Waifu2x::CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + const stImage &image) +{ + if (scale_ratio) + return *scale_ratio; + + if (scale_width) + return image.GetScaleFromWidth(*scale_width); + + if(scale_height) + return image.GetScaleFromWidth(*scale_height); + + return 1.0; +} + Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(const double factor, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, const bool isReconstructNoise, const bool isReconstructScale, const Waifu2x::waifu2xCancelFunc cancel_func, stImage &image) { @@ -691,17 +708,6 @@ Waifu2x::eWaifu2xError Waifu2x::ProcessNet(std::shared_ptr net, const int return Waifu2x::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; -//} - void Waifu2x::Destroy() { mNoiseNet.reset(); diff --git a/common/waifu2x.h b/common/waifu2x.h index 181a177..49f6297 100644 --- a/common/waifu2x.h +++ b/common/waifu2x.h @@ -96,6 +96,9 @@ private: static boost::filesystem::path GetModeDirPath(const boost::filesystem::path &model_dir); static boost::filesystem::path GetInfoPath(const boost::filesystem::path &model_dir); + static double CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + const stImage &image); + Waifu2x::eWaifu2xError ReconstructImage(const double factor, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, const bool isReconstructNoise, const bool isReconstructScale, const Waifu2x::waifu2xCancelFunc cancel_func, stImage &image); Waifu2x::eWaifu2xError ReconstructScale(const int crop_w, const int crop_h, const bool use_tta, const int batch_size, @@ -106,8 +109,6 @@ private: const Waifu2x::waifu2xCancelFunc cancel_func, cv::Mat &im); Waifu2x::eWaifu2xError ProcessNet(std::shared_ptr net, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, cv::Mat &im); - // double CalcScaleRatio(const cv::Size_ &size) const; - public: Waifu2x(); ~Waifu2x(); @@ -124,7 +125,8 @@ public: const boost::filesystem::path &model_dir, const std::string &process); eWaifu2xError waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file, - const double factor, const waifu2xCancelFunc cancel_func = nullptr, const int crop_w = 128, const int crop_h = 128, + const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + const waifu2xCancelFunc cancel_func = nullptr, const int crop_w = 128, const int crop_h = 128, const boost::optional output_quality = boost::optional(), const int output_depth = 8, const bool use_tta = false, const int batch_size = 1); diff --git a/waifu2x-caffe-gui/MainDialog.cpp b/waifu2x-caffe-gui/MainDialog.cpp index d494df8..553d890 100644 --- a/waifu2x-caffe-gui/MainDialog.cpp +++ b/waifu2x-caffe-gui/MainDialog.cpp @@ -706,6 +706,21 @@ void DialogEvent::ProcessWaifu2x() ProgessFunc(maxFile, 0); + boost::optional ScaleRatio; + boost::optional ScaleWidth, ScaleHeight; + switch (scaleType) + { + case eScaleTypeRatio: + ScaleRatio = scale_ratio; + break; + case eScaleTypeWidth: + ScaleWidth = scale_width; + break; + default: + ScaleHeight = scale_height; + break; + } + DWORD startTime = 0; int64_t processeNum = 0; @@ -725,24 +740,7 @@ void DialogEvent::ProcessWaifu2x() continue; } - double factor; - - //switch (scaleType) - //{ - //case eScaleTypeRatio: - // ScaleRatio = scale_ratio; - // break; - //case eScaleTypeWidth: - // ScaleWidth = scale_width; - // break; - //default: - // ScaleHeight = scale_height; - // break; - //} - - factor = scale_ratio; - - ret = w.waifu2x(p.first, p.second, factor, [this]() + ret = w.waifu2x(p.first, p.second, ScaleRatio, ScaleWidth, ScaleHeight, [this]() { return cancelFlag; }, crop_size, crop_size, output_quality, output_depth, use_tta, batch_size);