mirror of
https://github.com/lltcggie/waifu2x-caffe.git
synced 2025-06-26 13:42:48 +00:00
拡大後のサイズを縦幅か横幅で指定できるようにした
This commit is contained in:
parent
0505f7948f
commit
7d1b738569
@ -1138,7 +1138,9 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(boost::shared_ptr<caffe::Net<fl
|
|||||||
return eWaifu2xError_OK;
|
return eWaifu2xError_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &Mode, const int NoiseLevel, const double ScaleRatio, const boost::filesystem::path &ModelDir, const std::string &Process,
|
Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &Mode, const int NoiseLevel,
|
||||||
|
const boost::optional<double> ScaleRatio, const boost::optional<int> ScaleWidth, const boost::optional<int> ScaleHeight,
|
||||||
|
const boost::filesystem::path &ModelDir, const std::string &Process,
|
||||||
const boost::optional<int> OutputQuality, const int OutputDepth, const bool UseTTA, const int CropSize, const int BatchSize)
|
const boost::optional<int> OutputQuality, const int OutputDepth, const bool UseTTA, const int CropSize, const int BatchSize)
|
||||||
{
|
{
|
||||||
Waifu2x::eWaifu2xError ret;
|
Waifu2x::eWaifu2xError ret;
|
||||||
@ -1146,7 +1148,22 @@ Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &M
|
|||||||
if (is_inited)
|
if (is_inited)
|
||||||
return eWaifu2xError_OK;
|
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;
|
return eWaifu2xError_InvalidParameter;
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -1154,6 +1171,8 @@ Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &M
|
|||||||
mode = Mode;
|
mode = Mode;
|
||||||
noise_level = NoiseLevel;
|
noise_level = NoiseLevel;
|
||||||
scale_ratio = ScaleRatio;
|
scale_ratio = ScaleRatio;
|
||||||
|
scale_width = ScaleWidth;
|
||||||
|
scale_height = ScaleHeight;
|
||||||
model_dir = ModelDir;
|
model_dir = ModelDir;
|
||||||
process = Process;
|
process = Process;
|
||||||
use_tta = UseTTA;
|
use_tta = UseTTA;
|
||||||
@ -1481,7 +1500,8 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructFloatMat(const bool isReconstructNois
|
|||||||
if (cancel_func && cancel_func())
|
if (cancel_func && cancel_func())
|
||||||
return eWaifu2xError_Cancel;
|
return eWaifu2xError_Cancel;
|
||||||
|
|
||||||
const int scale2 = ceil(log2(scale_ratio));
|
const double ratio = CalcScaleRatio(image_size);
|
||||||
|
const int scale2 = ceil(log2(ratio));
|
||||||
|
|
||||||
if (isReconstructScale)
|
if (isReconstructScale)
|
||||||
{
|
{
|
||||||
@ -1630,8 +1650,9 @@ Waifu2x::eWaifu2xError Waifu2x::AfterReconstructFloatMatProcess(const bool isRec
|
|||||||
cv::merge(planes, process_image);
|
cv::merge(planes, process_image);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int scale2 = ceil(log2(scale_ratio));
|
const double ratio = CalcScaleRatio(image_size);
|
||||||
const double shrinkRatio = scale_ratio / std::pow(2.0, (double)scale2);
|
const int scale2 = ceil(log2(ratio));
|
||||||
|
const double shrinkRatio = ratio / std::pow(2.0, (double)scale2);
|
||||||
|
|
||||||
cv::Mat alpha;
|
cv::Mat alpha;
|
||||||
if (floatim.channels() == 4)
|
if (floatim.channels() == 4)
|
||||||
@ -1726,6 +1747,17 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2xConvetedMat(const bool isJpeg, const cv::
|
|||||||
return eWaifu2xError_OK;
|
return 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;
|
||||||
|
}
|
||||||
|
|
||||||
Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file,
|
Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file,
|
||||||
const waifu2xCancelFunc cancel_func)
|
const waifu2xCancelFunc cancel_func)
|
||||||
{
|
{
|
||||||
@ -1810,13 +1842,20 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(double factor, const void* source, void*
|
|||||||
float_image = convert;
|
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_ratio = factor;
|
||||||
|
scale_width.reset();
|
||||||
|
scale_height.reset();
|
||||||
|
|
||||||
cv::Mat write_iamge;
|
cv::Mat write_iamge;
|
||||||
ret = waifu2xConvetedMat(false, float_image, 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)
|
if (ret != eWaifu2xError_OK)
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -88,7 +88,9 @@ private:
|
|||||||
|
|
||||||
std::string mode;
|
std::string mode;
|
||||||
int noise_level;
|
int noise_level;
|
||||||
double scale_ratio;
|
boost::optional<double> scale_ratio;
|
||||||
|
boost::optional<int> scale_width;
|
||||||
|
boost::optional<int> scale_height;
|
||||||
boost::filesystem::path model_dir;
|
boost::filesystem::path model_dir;
|
||||||
std::string process;
|
std::string process;
|
||||||
|
|
||||||
@ -136,6 +138,8 @@ private:
|
|||||||
eWaifu2xError waifu2xConvetedMat(const bool isJpeg, const cv::Mat &inMat, cv::Mat &outMat,
|
eWaifu2xError waifu2xConvetedMat(const bool isJpeg, const cv::Mat &inMat, cv::Mat &outMat,
|
||||||
const waifu2xCancelFunc cancel_func = nullptr);
|
const waifu2xCancelFunc cancel_func = nullptr);
|
||||||
|
|
||||||
|
double CalcScaleRatio(const cv::Size_<int> &size) const;
|
||||||
|
|
||||||
static int DepthBitToCVDepth(const int depth_bit);
|
static int DepthBitToCVDepth(const int depth_bit);
|
||||||
static double GetValumeMaxFromCVDepth(const int cv_depth);
|
static double GetValumeMaxFromCVDepth(const int cv_depth);
|
||||||
static double GetEPS(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
|
// mode: noise or scale or noise_scale or auto_scale
|
||||||
// process: cpu or gpu or cudnn
|
// 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,
|
eWaifu2xError init(int argc, char** argv, const std::string &mode, const int noise_level,
|
||||||
const boost::optional<int> output_quality = boost::optional<int>(), const int output_depth = 8, const bool use_tta = false, const int crop_size = 128, const int batch_size = 1);
|
const boost::optional<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
|
||||||
|
const boost::filesystem::path &model_dir, const std::string &process,
|
||||||
|
const boost::optional<int> output_quality = boost::optional<int>(), const int output_depth = 8, const bool use_tta = false,
|
||||||
|
const int crop_size = 128, const int batch_size = 1);
|
||||||
|
|
||||||
void destroy();
|
void destroy();
|
||||||
|
|
||||||
|
@ -642,8 +642,25 @@ private:
|
|||||||
|
|
||||||
Waifu2x::eWaifu2xError ret;
|
Waifu2x::eWaifu2xError ret;
|
||||||
|
|
||||||
|
boost::optional<double> ScaleRatio;
|
||||||
|
boost::optional<int> ScaleWidth;
|
||||||
|
boost::optional<int> ScaleHeight;
|
||||||
|
|
||||||
|
switch (scaleType)
|
||||||
|
{
|
||||||
|
case eScaleTypeRatio:
|
||||||
|
ScaleRatio = scale_ratio;
|
||||||
|
break;
|
||||||
|
case eScaleTypeWidth:
|
||||||
|
ScaleWidth = scale_width;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ScaleHeight = scale_height;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
Waifu2x w;
|
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)
|
if(ret != Waifu2x::eWaifu2xError_OK)
|
||||||
SendMessage(dh, WM_ON_WAIFU2X_ERROR, (WPARAM)&ret, 0);
|
SendMessage(dh, WM_ON_WAIFU2X_ERROR, (WPARAM)&ret, 0);
|
||||||
else
|
else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user