コード例 #1
0
    def AddGaussianNoise(self, srcImg):
        rows = srcImg.shape[0]
        cols = srcImg.shape[1]
        templatesSize = len(self.templates)
        meanFore, stddevFore, meanBack, stddevBack = self.templates[
            random.randint(0, templatesSize - 1)]
        foreTemplate = np.random.normal(meanFore, stddevFore,
                                        (rows, cols)).astype(np.uint8)
        backTemplate = np.random.normal(meanBack, stddevBack,
                                        (rows, cols)).astype(np.uint8)
        noiseImage = np.zeros((rows, cols), dtype=np.uint8)

        binImg = ImageUtils.BinImage(srcImg)

        for indRow in range(rows):
            for indCol in range(cols):
                if binImg[indRow, indCol] == 255:
                    noiseImage[indRow, indCol] = backTemplate[indRow, indCol]
                else:
                    noiseImage[indRow, indCol] = foreTemplate[indRow, indCol]

        noiseImage = cv2.cvtColor(noiseImage, cv2.COLOR_GRAY2RGB)
        return noiseImage