Last active
August 29, 2015 14:11
-
-
Save mmmikael/992a9a098ae3d930bfdc to your computer and use it in GitHub Desktop.
lua torch rgb <=> yuv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function rgbToYuv(rgbTensor) | |
local r,g,b = rgbTensor[1],rgbTensor[2],rgbTensor[3] | |
local yuvTensor = rgbTensor:clone() | |
yuvTensor[1] = torch.mul(r, 0.299) + torch.mul(g, 0.587) + torch.mul(b, 0.114) | |
yuvTensor[2] = torch.mul(b - yuvTensor[1], 0.492) | |
yuvTensor[3] = torch.mul(r - yuvTensor[1], 0.877) | |
return yuvTensor | |
end | |
local function yuvToRgb(yuvTensor) | |
local y,u,v = yuvTensor[1],yuvTensor[2],yuvTensor[3] | |
local rgbTensor = yuvTensor:clone() | |
rgbTensor[1] = y + torch.mul(v, 1.13983) | |
rgbTensor[2] = y + torch.mul(u, -0.39465) + torch.mul(v, -0.58060) | |
rgbTensor[3] = y + torch.mul(u, 2.03211) | |
return rgbTensor | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment