Browse Source

Add -white_noise option

post-processing for anime screencap.
nagadomi 9 years ago
parent
commit
4a4885c856
6 changed files with 62 additions and 1 deletions
  1. 8 0
      assets/index.html
  2. 8 0
      assets/index.ja.html
  3. 8 0
      assets/index.ru.html
  4. 24 0
      lib/iproc.lua
  5. 9 0
      waifu2x.lua
  6. 5 1
      web.lua

+ 8 - 0
assets/index.html

@@ -52,6 +52,14 @@
         <label><input type="radio" name="scale" value="1"> 1.6x</label>
         <label><input type="radio" name="scale" value="1"> 1.6x</label>
         <label><input type="radio" name="scale" value="2"> 2x</label>
         <label><input type="radio" name="scale" value="2"> 2x</label>
       </fieldset>
       </fieldset>
+      <fieldset>
+        <legend>Adding white noise</legend>
+        <label><input type="radio" name="white_noise" value="0" checked="checked">Off</label>
+        <label><input type="radio" name="white_noise" value="1">On</label>
+	<div class="help">
+	  To denoise without unnaturalness in an anime screencap.
+	</div>
+      </fieldset>
       <input type="submit"/>
       <input type="submit"/>
     </form>
     </form>
     <div class="help">
     <div class="help">

+ 8 - 0
assets/index.ja.html

@@ -49,6 +49,14 @@
         <label><input type="radio" name="scale" value="1"> 1.6x</label>
         <label><input type="radio" name="scale" value="1"> 1.6x</label>
         <label><input type="radio" name="scale" value="2"> 2x</label>
         <label><input type="radio" name="scale" value="2"> 2x</label>
       </fieldset>
       </fieldset>
+      <fieldset>
+        <legend>ホワイトノイズ付加</legend>
+        <label><input type="radio" name="white_noise" value="0" checked="checked">Off</label>
+        <label><input type="radio" name="white_noise" value="1">On</label>
+	<div class="help">
+	  アニメのキャプチャ画像をノイズ除去したときの不自然さを低減します。
+	</div>
+      </fieldset>
       <input type="submit" value="実行"/>
       <input type="submit" value="実行"/>
     </form>
     </form>
     <div class="help">
     <div class="help">

+ 8 - 0
assets/index.ru.html

@@ -51,6 +51,14 @@
         <label><input type="radio" name="scale" value="1"> 1.6x</label>
         <label><input type="radio" name="scale" value="1"> 1.6x</label>
         <label><input type="radio" name="scale" value="2"> 2x</label>
         <label><input type="radio" name="scale" value="2"> 2x</label>
       </fieldset>
       </fieldset>
+      <fieldset>
+        <legend>Adding white noise</legend>
+        <label><input type="radio" name="white_noise" value="0" checked="checked">Off</label>
+        <label><input type="radio" name="white_noise" value="1">On</label>
+	<div class="help">
+	  To denoise without unnaturalness in an anime screencap.
+	</div>
+      </fieldset>
       <input type="submit"/>
       <input type="submit"/>
     </form>
     </form>
     <div class="help">
     <div class="help">

+ 24 - 0
lib/iproc.lua

@@ -84,6 +84,30 @@ function iproc.padding(img, w1, w2, h1, h2)
    flow[2]:add(-w1)
    flow[2]:add(-w1)
    return image.warp(img, flow, "simple", false, "clamp")
    return image.warp(img, flow, "simple", false, "clamp")
 end
 end
+function iproc.white_noise(src, std, rgb_weights, gamma)
+   gamma = gamma or 0.454545
+   local conversion
+   src, conversion = iproc.byte2float(src)
+   std = std or 0.01
+
+   local noise = torch.Tensor():resizeAs(src):normal(0, std)
+   if rgb_weights then 
+      noise[1]:mul(rgb_weights[1])
+      noise[2]:mul(rgb_weights[2])
+      noise[3]:mul(rgb_weights[3])
+   end
+
+   local dest
+   if gamma ~= 0 then
+      dest = src:clone():pow(gamma):add(noise):pow(1.0 / gamma)
+   else
+      dest = src + noise
+   end
+   if conversion then
+      dest = iproc.float2byte(dest)
+   end
+   return dest
+end
 
 
 local function test_conversion()
 local function test_conversion()
    local a = torch.linspace(0, 255, 256):float():div(255.0)
    local a = torch.linspace(0, 255, 256):float():div(255.0)

+ 9 - 0
waifu2x.lua

@@ -58,6 +58,9 @@ local function convert_image(opt)
    else
    else
       error("undefined method:" .. opt.method)
       error("undefined method:" .. opt.method)
    end
    end
+   if opt.white_noise == 1 then
+      new_x = iproc.white_noise(new_x, opt.white_noise_std, {1.0, 0.8, 1.0})
+   end
    image_loader.save_png(opt.o, new_x, alpha, opt.depth)
    image_loader.save_png(opt.o, new_x, alpha, opt.depth)
    print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
    print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
 end
 end
@@ -138,6 +141,10 @@ local function convert_frames(opt)
 	 else
 	 else
 	    error("undefined method:" .. opt.method)
 	    error("undefined method:" .. opt.method)
 	 end
 	 end
+	 if opt.white_noise == 1 then
+	    new_x = iproc.white_noise(new_x, opt.white_noise_std, {1.0, 0.8, 1.0})
+	 end
+
 	 local output = nil
 	 local output = nil
 	 if opt.o == "(auto)" then
 	 if opt.o == "(auto)" then
 	    local name = path.basename(lines[i])
 	    local name = path.basename(lines[i])
@@ -175,6 +182,8 @@ local function waifu2x()
    cmd:option("-resume", 0, "skip existing files (0|1)")
    cmd:option("-resume", 0, "skip existing files (0|1)")
    cmd:option("-thread", -1, "number of CPU threads")
    cmd:option("-thread", -1, "number of CPU threads")
    cmd:option("-tta", 0, '8x slower and slightly high quality (0|1)')
    cmd:option("-tta", 0, '8x slower and slightly high quality (0|1)')
+   cmd:option("-white_noise", 0, 'adding white noise to output image (0|1)')
+   cmd:option("-white_noise_std", 0.0055, 'standard division of white noise')
    
    
    local opt = cmd:parse(arg)
    local opt = cmd:parse(arg)
    if opt.thread > 0 then
    if opt.thread > 0 then

+ 5 - 1
web.lua

@@ -142,6 +142,7 @@ function APIHandler:post()
    local x, alpha, blob = get_image(self)
    local x, alpha, blob = get_image(self)
    local scale = tonumber(self:get_argument("scale", "0"))
    local scale = tonumber(self:get_argument("scale", "0"))
    local noise = tonumber(self:get_argument("noise", "0"))
    local noise = tonumber(self:get_argument("noise", "0"))
+   local white_noise = tonumber(self:get_argument("white_noise", "0"))
    local style = self:get_argument("style", "art")
    local style = self:get_argument("style", "art")
    if style ~= "art" then
    if style ~= "art" then
       style = "photo" -- style must be art or photo
       style = "photo" -- style must be art or photo
@@ -169,6 +170,9 @@ function APIHandler:post()
 					    "Jinc")
 					    "Jinc")
 	    end
 	    end
 	 end
 	 end
+	 if white_noise == 1 then
+	    x = iproc.white_noise(x, 0.005, {1.0, 0.8, 1.0})
+	 end
       end
       end
       local name = uuid() .. ".png"
       local name = uuid() .. ".png"
       local blob, len = image_loader.encode_png(x, alpha)
       local blob, len = image_loader.encode_png(x, alpha)
@@ -179,7 +183,7 @@ function APIHandler:post()
    else
    else
       if not x then
       if not x then
 	 self:set_status(400)
 	 self:set_status(400)
-	 self:write("ERROR: unsupported image format.")
+	 self:write("ERROR: An error occurred. (unsupported image format/connection timeout/file is too large)")
       else
       else
 	 self:set_status(400)
 	 self:set_status(400)
 	 self:write("ERROR: image size exceeds maximum allowable size.")
 	 self:write("ERROR: image size exceeds maximum allowable size.")