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
    
    
    Run

Reply via email to