def GaussianNoise(self, filename1, filename2):
        bmp = ReadBmp(filename1)
        # 生成均值为means,方差为sigma,噪声比为percetage的高斯噪声图像
        means = 8
        sigma = 10
        percetage = 1
        for pixel in bmp.data:
            # 图像灰度化
            gray = 0.299 * pixel[0] + 0.578 * pixel[1] + 0.114 * pixel[2]
            p = random.random()
            # 增加高斯噪声
            if p <= percetage:
                gray = gray + random.gauss(means, sigma)

            if gray < 0:
                gray = 0
            elif gray > 255:
                gray = 255

            pixel[0] = round(gray)
            pixel[1] = round(gray)
            pixel[2] = round(gray)
        # 将新图像保存到文件
        bmp.creataBmp(filename2)
        return
Example #2
0
    def MedianFilter(self, filename1, filename2):
        # 中值滤波,这里对边界填充采用不做边界处理的方式
        bmp = ReadBmp(filename1)
        new_data = []
        for j in range(bmp.biHeight):
            for i in range(bmp.biWidth):
                if i == 0 or j == 0 or i == bmp.biWidth-1 or j == bmp.biHeight-1:
                    new_data.append(bmp.data[j * bmp.biWidth + i][0])
                    continue

                list = [bmp.data[j * bmp.biWidth + i + 1][0], bmp.data[j * bmp.biWidth + i - 1][0] \
                , bmp.data[(j + 1) * bmp.biWidth + i][0], bmp.data[(j - 1) * bmp.biWidth + i][0] \
                , bmp.data[(j + 1) * bmp.biWidth + i + 1][0], bmp.data[(j + 1) * bmp.biWidth + i - 1][0] \
                , bmp.data[(j - 1) * bmp.biWidth + i + 1][0], bmp.data[(j - 1) * bmp.biWidth + i - 1][0]]
                list.sort()
                median = round((list[3] + list[4])/2)
                new_data.append(median)

        for i in range(len(new_data)):
            bmp.data[i][0] = new_data[i]
            bmp.data[i][1] = new_data[i]
            bmp.data[i][2] = new_data[i]

        # 将新图像保存到文件
        bmp.creataBmp(filename2)
        return
Example #3
0
    def Sobel(self, filename1, filename2):
        bmp = ReadBmp(filename1)
        # 阈值,根据实验结果大致调整的
        Gmax = 140
        for i in range(bmp.biHeight * bmp.biWidth):
            # 图像灰度化
            gray = round(0.299 * bmp.data[i][0] + 0.578 * bmp.data[i][1] +
                         0.114 * bmp.data[i][2])
            bmp.data[i][0] = gray
            bmp.data[i][1] = gray
            bmp.data[i][2] = gray

        new_data = []
        for j in range(bmp.biHeight):
            for i in range(bmp.biWidth):
                if i == 0 or j == 0 or i == bmp.biWidth - 1 or j == bmp.biHeight - 1:
                    new_data.append(0)
                    continue

                Gx = (-1)*bmp.data[(j + 1) * bmp.biWidth + i - 1][0] + bmp.data[(j + 1) * bmp.biWidth + i + 1][0] \
                + (-1)*bmp.data[(j - 1) * bmp.biWidth + i - 1][0] + bmp.data[(j - 1) * bmp.biWidth + i + 1][0] \
                + (-2)*bmp.data[j * bmp.biWidth + i - 1][0] + 2*bmp.data[j * bmp.biWidth + i + 1][0]

                Gy = (-1)*bmp.data[(j - 1) * bmp.biWidth + i - 1][0] + bmp.data[(j + 1) * bmp.biWidth + i - 1][0] \
                + (-1)*bmp.data[(j - 1) * bmp.biWidth + i + 1][0] + bmp.data[(j + 1) * bmp.biWidth + i + 1][0] \
                + (-2)*bmp.data[(j - 1) * bmp.biWidth + i][0] + 2*bmp.data[(j + 1) * bmp.biWidth + i][0]

                G = math.sqrt(Gx**2 + Gy**2)

                if G >= Gmax:
                    new_data.append(255)
                else:
                    new_data.append(0)

        for i in range(len(new_data)):
            bmp.data[i][0] = new_data[i]
            bmp.data[i][1] = new_data[i]
            bmp.data[i][2] = new_data[i]
        # 将新图像保存到文件
        bmp.creataBmp(filename2)

        return
    def SaltAndPepperNoise(self, filename1, filename2):
        bmp = ReadBmp(filename1)
        # 生成椒盐噪声图像
        percetage = 0.05
        for pixel in bmp.data:
            # 图像灰度化
            gray = 0.299 * pixel[0] + 0.578 * pixel[1] + 0.114 * pixel[2]
            p = random.random()
            # 增加椒盐噪声
            if p <= percetage:
                if p <= percetage * 0.5:
                    gray = 0
                else:
                    gray = 255

            pixel[0] = round(gray)
            pixel[1] = round(gray)
            pixel[2] = round(gray)

        # 将新图像保存到文件
        bmp.creataBmp(filename2)
        return
Example #5
0
    def MeanFilter(self, filename1, filename2):
        # 均值滤波, 这里对边界填充采用不做边界处理的方式
        bmp = ReadBmp(filename1)
        new_data = []
        for j in range(bmp.biHeight):
            for i in range(bmp.biWidth):
                if i == 0 or j == 0 or i == bmp.biWidth-1 or j == bmp.biHeight-1:
                    new_data.append(bmp.data[j * bmp.biWidth + i][0])
                    continue

                sum = bmp.data[j * bmp.biWidth + i + 1][0] + bmp.data[j * bmp.biWidth + i - 1][0] \
                + bmp.data[(j + 1) * bmp.biWidth + i][0] + bmp.data[(j - 1) * bmp.biWidth + i][0] \
                + bmp.data[(j + 1) * bmp.biWidth + i + 1][0] + bmp.data[(j + 1) * bmp.biWidth + i - 1][0] \
                + bmp.data[(j - 1) * bmp.biWidth + i + 1][0] + bmp.data[(j - 1) * bmp.biWidth + i - 1][0]

                new_data.append(round(sum/8))
        for i in range(len(new_data)):
            bmp.data[i][0] = new_data[i]
            bmp.data[i][1] = new_data[i]
            bmp.data[i][2] = new_data[i]
        # 将新图像保存到文件
        bmp.creataBmp(filename2)
        return
from ReadBmp import ReadBmp
import matplotlib.pyplot as plt
import numpy as np

filename1 = "1.bmp"
bmp = ReadBmp(filename1)
bmp.gray()

# 统计各像素点数
h = np.array([0 for i in range(256)])
h1 = []
for pixel in bmp.data:
    h[pixel[0]] = h[pixel[0]] + 1
    h1.append(pixel[0])
# 画出原先的直方图
plt.subplot(1, 2, 1)
plt.hist(h1, bins=256)

# 归一化
hs = h / len(bmp.data)
# 计算累计分布
hp = np.array([0.0 for i in range(256)])
for i in range(256):
    hp[i] = np.round(np.sum(hs[0:i + 1]) * 255)
T = hp.astype('uint8')

# 创建新图像,并统计新图像的各个像素点的个数
hn = np.array([0 for i in range(256)])
h2 = []
for pixel in bmp.data:
    s = T[pixel[0]]