def gauss_filter(): header, y_cb_cr = bmp.readBmp(gauss_noise_filename) h, y_cb_cr_o = bmp.readBmp(original_file_y_cb_cr) sigmaRange = np.arange(0.0001, 1.1, 0.1) psnr = defaultdict(list) rRange = range(1, 10) legends = [] out_psnr = np.zeros((len(list(rRange)) + 1, (len(list(sigmaRange)) + 1))) out_psnr[1:, 0] = list(rRange) out_psnr[0, 1:] = list(sigmaRange) print('orig ', bmp.psnr(y_cb_cr_o[..., 0], y_cb_cr[..., 0])) for r in rRange: psnr = [] for i in sigmaRange: res = BmpFilters.BmpFilters.gauss_filter(y_cb_cr, r, i) ps = bmp.psnr(y_cb_cr_o[..., 0], res[..., 0]) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] bmp.writeBmp( gauss_filter_filename + "/gauss r = {0:.2f} d = {1:.2f}.bmp".format(r, i), header, res) psnr.append(ps) p = np.array(psnr) out_psnr[r, 1:] = p l, = plt.plot(list(sigmaRange), psnr, label=f'R = {r}') legends.append(l) plt.legend(handles=legends) plt.savefig(gauss_filter_filename + f"/psnr total.png") np.savetxt(gauss_filter_filename + '/psnr.csv', out_psnr, delimiter=',', fmt='%.2f')
def median_for_impulse(): header, y_orig = bmp.readBmp(original_file_y_cb_cr) folder = 'impulse2/' folder_res = 'impulse2/res/' pa = [0.02, 0.05, 0.2, 0.3] pb = [0.03, 0.05, 0.05, 0.2] for a, b in zip(pa, pb): percent = (a + b) * 100 h, y = bmp.readBmp(folder + f'impulse % = {percent}.bmp') psnr = [] x = [] labels = [] print(f'original {percent}', bmp.psnr(y_orig, y)) x.append(0) psnr.append(bmp.psnr(y_orig, y)) for i in range(1, 10): res = BmpFilters.BmpFilters.median_filter(y, i) p = bmp.psnr(y_orig[..., 0], res[..., 0]) x.append(i) psnr.append(p) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] bmp.writeBmp(folder_res + f' % {percent} r = {i}.bmp', header, res) labels.append(plt.plot(x, psnr, label=f'r = {i}')) plt.savefig(folder + 'psnr.png', handlers=labels)
def median(): header, y_cb_cr = bmp.readBmp(gauss_noise_filename) header_o, y_cb_cr_o = bmp.readBmp(original_file_y_cb_cr) psnr = [] print('orig', bmp.psnr(y_cb_cr_o[..., 0], y_cb_cr[..., 0])) x = range(1, 10) for r in x: res = BmpFilters.BmpFilters.median_filter(y_cb_cr, r * 2 + 1) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] psnr.append(bmp.psnr(y_cb_cr_o[..., 0], res[..., 0])) bmp.writeBmp(median_folder + f"/median r = {r}.bmp", header, res) np.savetxt(median_folder + "/psnr.csv", np.array(psnr).T, delimiter=',', fmt='%.2f') plt.plot(list(x), psnr) plt.savefig(median_folder + "/psnr.png")
def avgDecimationYCbCr(rgb, n): print(f'\n \n \n avg decimation {n}') y = rgb.shape[0] x = rgb.shape[1] y_cb_cr = bmp.convertYCbCr(rgb) resG = bmp.decimationByAverageValue(y_cb_cr[..., 1], n) resR = bmp.decimationByAverageValue(y_cb_cr[..., 2], n) resG2 = bmp.recoverDecimationByDeletingEven(resG, n) resR2 = bmp.recoverDecimationByDeletingEven(resR, n) rgb_recovered = np.zeros((y, x, 3), 'uint8') rgb_recovered[..., 0] = y_cb_cr[..., 0] rgb_recovered[..., 1] = resG2 rgb_recovered[..., 2] = resR2 print("psnr cb", bmp.psnr(y_cb_cr[..., 1], resG2)) print("psnr cr", bmp.psnr(y_cb_cr[..., 2], resR2)) rgb_recovered = bmp.inverseConvertYCbCr(rgb_recovered) print("psnr blue", bmp.psnr(rgb[..., 0], rgb_recovered[..., 0])) print("psnr green", bmp.psnr(rgb[..., 1], rgb_recovered[..., 1])) print("psnr red", bmp.psnr(rgb[..., 2], rgb_recovered[..., 2])) return rgb_recovered
def impulse_noise(): # hist = defaultdict(list) # x = defaultdict(list) # hist[0.1].append(1) # hist[0.1].append(2) # x[0.1].append(3) # x[0.1].append(6) # x[0.1].append(6) # for (k, v), (k2, v2) in zip(hist.items(), x.items()): # print(k, v, k2, v2) # print('\n') # # return small = 0.00001 file_name = orig_filename header, rgb = bmp.readBmp(file_name) y_cb_cr = bmp.convertYCbCr(rgb) hist = defaultdict(list) x = defaultdict(list) rng = np.arange(0, 1.1, 0.1) for i in rng: for j in rng: if i + j >= 1: continue if i == j == 0: i = small j = small res = BmpFilters.BmpFilters.add_impulse_noise(y_cb_cr, i, j) p = bmp.psnr(y_cb_cr[..., 0], res[..., 0]) if i == j == small: i = 0 j = 0 x[i].append(j) hist[i].append(p) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] bmp.writeBmp( 'impulse/impulse pa = {0:.1f} pb = {1:.1f}.png'.format( float(i), float(j)), header, res) l = [] for (k, v), (k1, v1) in zip(x.items(), hist.items()): leg, = plt.plot(list(v), list(v1), label='pa = {0:.1}'.format(float(k))) l.append(leg) plt.legend(handles=l) plt.savefig('impulse/psnr.png')
def impulse_with_persent(): folder = 'impulse2/' header, y_cb_cr = bmp.readBmp(original_file_y_cb_cr) pa = [0.02, 0.05, 0.2, 0.3] pb = [0.03, 0.05, 0.05, 0.2] psnr = [] x = [] for a, b in zip(pa, pb): res = BmpFilters.BmpFilters.add_impulse_noise(y_cb_cr, a, b) psnr.append(bmp.psnr(y_cb_cr[..., 0], res[..., 0])) x.append((a + b) * 100) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] bmp.writeBmp(folder + f'impulse % = {(a + b) * 100}.bmp', header, res) plt.plot(x, psnr) plt.savefig(folder + 'psnr.png')
def gauss_noise(): header, rgb = bmp.readBmp(orig_filename) y_cb_cr = bmp.convertYCbCr(rgb) psnr = [] rng = range(0, 250, 10) for i in rng: if i == 0: i = 0.0000001 res = BmpFilters.BmpFilters.add_gauss_noise(y_cb_cr, d=i, m=0) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] p = bmp.psnr(y_cb_cr[..., 0], res[..., 0]) psnr.append(p) if i == 0.0000001: i = 0 bmp.writeBmp(f"gauss/gauss d ={i}.bmp", header, res) plt.plot(list(rng), psnr) plt.savefig(gauss_noise_psnr_filename)
def avg(): file_name = gauss_noise_filename header, rgb = bmp.readBmp(file_name) psnr = [] rng = range(1, 25) y_cb_cr = bmp.convertYCbCr(rgb) orinal_y_cb_cr = bmp.convertYCbCr(orig_pic) for i in rng: res = BmpFilters.BmpFilters.avg(y_cb_cr, i) psnr.append(bmp.psnr(orinal_y_cb_cr[..., 0], res[..., 0])) res[..., 1] = res[..., 0] res[..., 2] = res[..., 0] # bmp.writeBmp(moving_average_folder + f"/avg {i}.bmp", header, res) x = np.array(list(rng), dtype='uint') y = np.array(psnr) np.savetxt(moving_average_folder + "/psnr.csv", (x, y), delimiter=',', fmt='%.2f') plt.plot(list(rng), psnr) plt.savefig(moving_average_folder + "/psnr.png")
header, rgb = bmp.readBmp(in_file) a = rgb[...][...][0] - rgb[...][...][1] print(np.max(a)) sys.exit(0) y_cb_cr = bmp.convertYCbCr(rgb) cbDecimated = bmp.decimationByDeletingEven(y_cb_cr[..., 1], 4) crDecimated = bmp.decimationByDeletingEven(y_cb_cr[..., 2], 4) cbRecovered = bmp.recoverDecimationByDeletingEven(cbDecimated, 4) crRecovered = bmp.recoverDecimationByDeletingEven(crDecimated, 4) y_cb_cr[..., 1] = cbRecovered y_cb_cr[..., 2] = crRecovered rgb2 = bmp.inverseConvertYCbCr(y_cb_cr) bmp.writeBmp(res_file_path + "testRec.bmp", header, rgb2) print(bmp.psnr(rgb[..., 0], rgb2[..., 0])) print(bmp.psnr(rgb[..., 1], rgb2[..., 1])) print(bmp.psnr(rgb[..., 2], rgb2[..., 2])) sys.stdout = open('console.txt', 'w') b = rgb[..., 0] g = rgb[..., 1] r = rgb[..., 2] # [4] b getAutoCor(r, "red") getAutoCor(g, "green") getAutoCor(b, "blue") # 7 [y_cb_cr] y_cb_cr = bmp.convertYCbCr(rgb) rgbRec = bmp.inverseConvertYCbCr(y_cb_cr)