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")
def laplas(): header, rgb = bmp.readBmp(orig_filename) y_cb_cr = bmp.convertYCbCr(rgb) y_cb_cr_res = BmpFilters.BmpFilters.laplas_operator(y_cb_cr, 0) y_cb_cr_res += 128 np.putmask(y_cb_cr_res, y_cb_cr_res < 0, 0) np.putmask(y_cb_cr_res, y_cb_cr_res > 255, 255) y_cb_cr_res[..., 1] = y_cb_cr_res[..., 0] y_cb_cr_res[..., 2] = y_cb_cr_res[..., 0] y_cb_cr[..., 1] = y_cb_cr[..., 0] y_cb_cr[..., 2] = y_cb_cr[..., 0] bmp.writeBmp(laplas_folder + "/laplas.bmp", header, y_cb_cr_res) bmp.writeBmp(laplas_folder + "/orig.bmp", header, y_cb_cr)
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 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 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 componentHistogramsAndEntropy(rgb): y_cb_cr = bmp.convertYCbCr(rgb) x = range(0, 255) freq_blue = defaultdict(int) freq_green = defaultdict(int) freq_red = defaultdict(int) freq_cb = defaultdict(int) freq_cr = defaultdict(int) freq_y = defaultdict(int) for b, g, r, cb, cr, y in zip(rgb[..., 0].flatten(), rgb[..., 1].flatten(), rgb[..., 2].flatten(), y_cb_cr[..., 1].flatten(), y_cb_cr[..., 2].flatten(), y_cb_cr[..., 0].flatten()): freq_blue[b] += 1 freq_green[g] += 1 freq_red[r] += 1 freq_cb[cb] += 1 freq_cr[cr] += 1 freq_y[y] += 1 b = [] g = [] r = [] cb = [] cr = [] y = [] for i in x: b.append(freq_blue[i]) g.append(freq_green[i]) r.append(freq_red[i]) cb.append(freq_cb[i]) cr.append(freq_cr[i]) y.append(freq_y[i]) size = rgb[..., 0].size plt.bar(x, y) plt.title("y") plt.savefig("images\\y hist.png") plt.clf() plt.bar(x, b) plt.title("blue") plt.savefig("images\\blue hist.png") plt.clf() plt.bar(x, g) plt.title("green") plt.savefig("images\\green hist.png") plt.clf() plt.bar(x, r) plt.title("red") plt.savefig("images\\red hist.png") plt.clf() plt.bar(x, cb) plt.title("cb") plt.savefig("images\\cb hist.png") plt.clf() plt.bar(x, cr) plt.title("cr") plt.savefig("images\\cr hist.png") plt.savefig("images\\cr hist.png") plt.clf() entropy_b = -np.sum(b * np.ma.log2(b).filled(0)) entropy_g = -np.sum(g * np.ma.log2(g).filled(0)) entropy_r = -np.sum(r * np.ma.log2(r).filled(0)) entropy_cb = -np.sum(cb * np.ma.log2(cb).filled(0)) entropy_cr = -np.sum(cr * np.ma.log2(cr).filled(0)) entropy_y = -np.sum(y * np.ma.log2(y).filled(0)) print("\n \n \n") print("entropy b ", entropy_b) print("entropy g ", entropy_g) print("entropy r ", entropy_r) print("entropy cb ", entropy_cb) print("entropy cr ", entropy_cr) print("entropy y ", entropy_y)
def DPCM(rgb, func_type): y_cb_cr = bmp.convertYCbCr(rgb) r = rgb[..., 2] g = rgb[..., 1] b = rgb[..., 0] y = y_cb_cr[..., 0] cb = y_cb_cr[..., 1] cr = y_cb_cr[..., 2] hist_d_red = r[1:, 1:].astype(np.int32) - bmp.DPCM(r, func_type) hist_d_green = g[1:, 1:].astype(np.int32) - bmp.DPCM(g, func_type) hist_d_blue = r[1:, 1:].astype(np.int32) - bmp.DPCM(b, func_type) hist_d_y = r[1:, 1:].astype(np.int32) - bmp.DPCM(y, func_type) hist_d_cb = r[1:, 1:].astype(np.int32) - bmp.DPCM(cb, func_type) hist_d_cr = r[1:, 1:].astype(np.int32) - bmp.DPCM(cr, func_type) freq_blue = defaultdict(int) freq_green = defaultdict(int) freq_red = defaultdict(int) freq_cb = defaultdict(int) size = rgb[..., 0].size freq_cr = defaultdict(int) freq_y = defaultdict(int) for b, g, r, cb, cr, y in zip(hist_d_blue.flatten(), hist_d_green.flatten(), hist_d_red.flatten(), hist_d_cb.flatten(), hist_d_cr.flatten(), hist_d_y.flatten()): freq_blue[b] += 1 freq_green[g] += 1 freq_red[r] += 1 freq_cb[cb] += 1 freq_cr[cr] += 1 freq_y[y] += 1 b = [] g = [] r = [] cb = [] cr = [] y = [] x = range(-255, 255) for i in x: b.append(freq_blue[i]) g.append(freq_green[i]) r.append(freq_red[i]) cb.append(freq_cb[i]) cr.append(freq_cr[i]) y.append(freq_y[i]) for i in range(-255, 255): b[i] = b[i] / size g[i] = g[i] / size r[i] = r[i] / size cb[i] = cb[i] / size cr[i] = cr[i] / size y[i] = y[i] / size plt.bar(x, b) plt.title(f"BLUE DPCM type {func_type}") plt.savefig(f"images\\BLUE DPCM type {func_type}.png") plt.clf() plt.bar(x, g) plt.title(f"GREEN DPCM type {func_type}") plt.savefig(f"images\\GREEN DPCM type {func_type}.png") plt.clf() plt.bar(x, r) plt.title(f"RED DPCM type {func_type}") plt.savefig(f"images\\RED DPCM type {func_type}.png") plt.clf() plt.bar(x, cb) plt.title(f"CB DPCM type {func_type}") plt.savefig(f"images\\CB DPCM type {func_type}.png") plt.clf() plt.bar(x, cr) plt.title(f"CR DPCM type {func_type}") plt.savefig(f"images\\CR DPCM type {func_type}.png") plt.clf() entropy_b = -np.sum(b * np.ma.log2(b).filled(0)) entropy_g = -np.sum(g * np.ma.log2(g).filled(0)) entropy_r = -np.sum(r * np.ma.log2(r).filled(0)) entropy_cb = -np.sum(cb * np.ma.log2(cb).filled(0)) entropy_cr = -np.sum(cr * np.ma.log2(cr).filled(0)) entropy_y = -np.sum(y * np.ma.log2(y).filled(0)) print("\n \n \n") print(f"entropy type {func_type} of b ", entropy_b) print(f"entropy type {func_type} of g ", entropy_g) print(f"entropy type {func_type} of r ", entropy_r) print(f"entropy type {func_type} of cb ", entropy_cb) print(f"entropy type {func_type} of cr ", entropy_cr) print(f"entropy type {func_type} of y ", entropy_y)
entropy_y = -np.sum(y * np.ma.log2(y).filled(0)) print("\n \n \n") print(f"entropy type {func_type} of b ", entropy_b) print(f"entropy type {func_type} of g ", entropy_g) print(f"entropy type {func_type} of r ", entropy_r) print(f"entropy type {func_type} of cb ", entropy_cb) print(f"entropy type {func_type} of cr ", entropy_cr) print(f"entropy type {func_type} of y ", entropy_y) 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]