In my following code, a colorful RGB image, which is a 3D Tensor, is converted into a gray image, which should be a 2D Tensor. In short
GRAY_2D = 0.299*RED_2D + 0.587*GREEN_2D + 0.114*BLUE_2D
However the result imgGray has a rank of 3. The question must root from imgIn[_, _, 0].map(i => float64(i)*0.299) which produces a tensor of 3-rank. So what is the way to fix the problem? Thanks
import sugar
import ./src/arraymancer
var imgIn = read_image("1.png").permute(2, 1, 0) # chw2whc
echo "imgIn.rank = ", imgIn.rank
echo "imgIn.shape = ", imgIn.shape
let
M = imgIn.shape[0]
N = imgIn.shape[1]
C = imgIn.shape[2]
var imgGray = zeros[uint8]([M, N])
echo "zero imgGray.rank = ", imgGray.rank # 2, is expected
echo "zero imgGray.shape = ", imgGray.shape
if imgIn.rank == 3:
imgGray = (
imgIn[_, _, 0].map(i => float64(i)*0.299) +
imgIn[_, _, 1].map(i => float64(i)*0.587) +
imgIn[_, _, 2].map(i => float64(i)*0.114)
).map(i => uint8(i))[_, _]
else:
imgGray = imgIn
echo "new imgGray.rank = ", imgGray.rank # 3, is unexpected
echo "new imgGray.shape = ", imgGray.shape
You can get rid of a remaining dimension which is just 1 by reshaping it.
import sugar
import arraymancer
var imgIn = read_image("narg.png").permute(2, 1, 0) # chw2whc
echo "imgIn.rank = ", imgIn.rank
echo "imgIn.shape = ", imgIn.shape
let
M = imgIn.shape[0]
N = imgIn.shape[1]
C = imgIn.shape[2]
var imgGray = zeros[uint8]([M, N])
echo "zero imgGray.rank = ", imgGray.rank # 2, is expected
echo "zero imgGray.shape = ", imgGray.shape
if imgIn.rank == 3:
imgGray = (
imgIn[_, _, 0].map(i => float64(i)*0.299) +
imgIn[_, _, 1].map(i => float64(i)*0.587) +
imgIn[_, _, 2].map(i => float64(i)*0.114)
).map(i => uint8(i))
let reshapedImage = imgGray.reshape(imgIn.shape[0..1])
# just a sample to show that they're identical:
for i in 0..<100:
assert reshapedImage[i,i] == imgGray[i,i,0]
imgGray = reshapedImage
else:
imgGray = imgIn
echo "new imgGray.rank = ", imgGray.rank # 3, is unexpected
echo "new imgGray.shape = ", imgGray.shape