From bd63f99b599d83ef2769b85c8453feff1b5aa21c Mon Sep 17 00:00:00 2001 From: nagadomi Date: Sun, 8 Nov 2015 01:21:52 +0900 Subject: [PATCH] clip --- lib/iproc.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/iproc.lua b/lib/iproc.lua index 1e39de1..abcf49b 100644 --- a/lib/iproc.lua +++ b/lib/iproc.lua @@ -40,7 +40,10 @@ function iproc.float2byte(src) local dest = src if src:type() == "torch.FloatTensor" then conversion = true - dest = (src * 255.0):byte() + dest = (src * 255.0) + dest[torch.lt(dest, 0.0)] = 0 + dest[torch.gt(dest, 255.0)] = 255.0 + dest = dest:byte() end return dest, conversion end @@ -65,5 +68,19 @@ function iproc.padding(img, w1, w2, h1, h2) return image.warp(img, flow, "simple", false, "clamp") end +local function test_conversion() + local x = torch.FloatTensor({{{0, 0.1}, {-0.1, 1.0}}, {{0.1234, 0.5}, {0.85, 1.2}}, {{0, 0.1}, {0.5, 0.8}}}) + local im = gm.Image():fromTensor(x, "RGB", "DHW") + local a, b + + a = iproc.float2byte(x):float() + b = im:toTensor("byte", "RGB", "DHW"):float() + assert((a - b):abs():sum() == 0) + + a = iproc.byte2float(iproc.float2byte(x)) + b = gm.Image():fromTensor(im:toTensor("byte", "RGB", "DHW"), "RGB", "DHW"):toTensor("float", "RGB", "DHW") + assert((a - b):abs():sum() == 0) +end +--test_conversion() return iproc