黑色时如何将BGR图像设置为另一个图像?

时间:2017-10-14 17:09:10

标签: python opencv numpy

我有2张BGR图片。一个是黑色的。我想将所有黑色像素设置为另一个图像的像素。

func blowfishChecksizeAndPad(pt []byte) []byte {
    // calculate modulus of plaintext to blowfish's cipher block size
    // if result is not 0, then we need to pad
    modulus := len(pt) % blowfish.BlockSize
    if modulus != 0 {
        // how many bytes do we need to pad to make pt to be a multiple of 
        //blowfish's block size?
        padlen := blowfish.BlockSize - modulus
        // let's add the required padding
        for i := 0; i < padlen; i++ {
            // add the pad, one at a time
            pt = append(pt, 0)
        }
    }
    // return the whole-multiple-of-blowfish.BlockSize-sized plaintext
    // to the calling function
    return pt
}

1 个答案:

答案 0 :(得分:1)

我们可以通过在最后一个轴black上查找ALL个零来创建(axis=-1)像素的模板,方法是先跟0然后ALL reduce进行比较 -

mask = (u_v==0).all(axis=-1)

然后,使用boolean-indexing的掩码从prev_frame中选择并分配到u_v -

u_v[mask] = prev_frame[mask]

示例运行说明

1]输入:

In [148]: u_v
Out[148]: 
array([[[0, 0, 0], # first pixel set as all zeros for testing
        [2, 1, 1]],

       [[1, 2, 2],
        [0, 3, 1]]])

In [149]: prev_frame
Out[149]: 
array([[[0, 2, 1], <== this one is to be copied over to u_v
        [3, 1, 0]],

       [[2, 2, 3],
        [2, 0, 1]]])

2]黑色像素掩码:

In [150]: mask = (u_v==0).all(axis=-1)

In [151]: mask
Out[151]: 
array([[ True, False], # first element is True as first pix was black
       [False, False]], dtype=bool)

3]用于选择和分配的布尔索引:

In [152]: u_v[mask] = prev_frame[mask]

In [153]: u_v
Out[153]: 
array([[[0, 2, 1], <=== copied from prev_frame
        [2, 1, 1]],

       [[1, 2, 2],
        [0, 3, 1]]])