サイズ指定変換に対応

This commit is contained in:
lltcggie 2016-07-03 21:55:20 +09:00
parent 41fdf561d8
commit f3bb4fe182
5 changed files with 53 additions and 34 deletions

View File

@ -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;

View File

@ -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;
// 前処理

View File

@ -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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
const waifu2xCancelFunc cancel_func, const int crop_w, const int crop_h,
const boost::optional<int> 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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> 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<cNet> net, const int
return Waifu2x::eWaifu2xError_OK;
}
//double Waifu2x::CalcScaleRatio(const cv::Size_<int> &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();

View File

@ -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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> 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<cNet> 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_<int> &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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
const waifu2xCancelFunc cancel_func = nullptr, const int crop_w = 128, const int crop_h = 128,
const boost::optional<int> output_quality = boost::optional<int>(), const int output_depth = 8, const bool use_tta = false,
const int batch_size = 1);

View File

@ -706,6 +706,21 @@ void DialogEvent::ProcessWaifu2x()
ProgessFunc(maxFile, 0);
boost::optional<double> ScaleRatio;
boost::optional<int> 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);