使用遮罩更改BGR图像颜色?

时间:2019-06-30 00:37:02

标签: python-3.x opencv

enter image description here我有一个BGR图像和3个二进制掩码。我想要的是使用这些蒙版更改BGR图像上的颜色。借此,我的意思是,如果遮罩上的像素[3] [3]和[4] [4]是一个(白色),则图像上的像素[3] [3]和[4] [4]将为蓝色(例如)。

我想知道是否有一种方法可以避免在if-then-else语句中使用循环。我知道该怎么做。

您看到的图像是我想要的输出。蓝色,红色和绿色是已粘贴到原始图像中的3种不同的蒙版。它们是二进制图像,因此最初是黑白蒙版。

这是我用来完成此图像的代码:

Describe 'Set-Something' {

    Context 'already exists' {
        Mock Test-Something { return $True }
        Mock Do-Something

        It 'returns True' {
            { Set-Something } | Should -Be $True
        }

        It 'does not call Do-Something' {
            Set-Something
            Assert-MockCalled Do-Something -Times 0 -Exactly
        }
    }

    Context 'does not already exist and creation fails' {
        Mock Test-Something { return $False }
        Mock Do-Something

        It 'calls Do-Something' {
            Set-Something
            Assert-MockCalled Do-Something -Times 1 -Exactly
        }

        It 'calls Test-Something twice' {
            Set-Something
            Assert-MockCalled Test-Something -Times 2 -Exactly
        }

        It 'returns False' {
            { Set-Something } | Should -Be $False
        }
    }

    Context 'does not already exist and creation succeeds' {
        Mock Test-Something { ?? }
        Mock Do-Something

        It 'calls Do-Something' {
            Set-Something
            Assert-MockCalled Do-Something -Times 1 -Exactly
        }

        It 'calls Test-Something twice' {
            Set-Something
            Assert-MockCalled Test-Something -Times 2 -Exactly
        }

        It 'returns True' {
            { Set-Something } | Should -Be $True
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我遇到的更快的解决方案:

# True positives coordinates
coordinates = numpy.where(true_positives_mask != 0)
original_image[coordinates[0], coordinates[1]] = constants.GREEN
# False positives coordinates
coordinates = numpy.where(false_positives_mask != 0)
original_image[coordinates[0], coordinates[1]] = constants.BLUE
# False negatives coordinates
coordinates = numpy.where(false_negatives_mask != 0)
original_image[coordinates[0], coordinates[1]] = constants.RED

return original_image