from skimage import data, io, filters import numpy as np image = io.imread("./meinv.jpg")# a gray-image is MxN, an RGB-image MxNx3 and an RGBA-image MxNx4. # image = data.coins()[34:64, 34:64] # image = image[100:105, 200:205, :]# test (h, w, c) = image.shape print(image.shape) n = h * w io.imshow(image) io.show() def get_gray(i, j, image): R, G, B = image[i, j, 0], image[i, j, 1], image[i, j, 2] Gray_fz = np.power(R, 2.2) * 0.2973 + np.power(G, 2.2) * 0.6274 + np.power(B, 2.2) * 0.0753 Gray = np.power(Gray_fz, 1.0 / 2.2) return Gray def get_vid(i, j, w): return i * w + j def get_value(gray1, gray2): if np.abs(gray1 - gray2) > 1e-8: return (0 - 1.0) * np.log2(np.abs(1.0*gray1 - 1.0*gray2) / (1.0*gray1 + 1.0*gray2)) else: # return float(‘inf‘) return 24.881685433929057 * (gray1 + 1)# beta * (gray1 + 1) # beta是小于float(‘inf‘)的其他数的最大值 Edges = [] for k in range(1, w, 1): j0, j1 = k-1, k for r in range(1, h, 1): i0, i1 = r - 1, r v_id_00, v_id_01, v_id_10, v_id_11 = get_vid(i0, j0, w), get_vid(i0, j1, w), get_vid(i1, j0, w), get_vid(i1, j1, w) Gray_00, Gray_01, Gray_10, Gray_11 = get_gray(i0, j0, image), get_gray(i0, j1, image), get_gray(i1, j0, image), get_gray(i1, j1, image) # print("[{0}][{1}]\n[{2}][{3}]\n[{4}][{5}]\n[{6}][{7}]\n".format(i0, j0, i0, j1, i1, j0, i1, j1)) if 0 <= j0 and j0 < w-2:# 中间 没有 e <1, 3> if 0 <= i0 and i0 < h-2:# 中间 没有 e <2, 3> edge01 = [v_id_00, v_id_01, get_value(Gray_00, Gray_01)] edge02 = [v_id_00, v_id_10, get_value(Gray_00, Gray_10)] edge03 = [v_id_00, v_id_11, get_value(Gray_00, Gray_11)] edge12 = [v_id_01, v_id_10, get_value(Gray_01, Gray_10)] Edges.append(edge01) Edges.append(edge02) Edges.append(edge03) Edges.append(edge12) elif i0 == h-2:# 下边界 有 e <2, 3> edge01 = [v_id_00, v_id_01, get_value(Gray_00, Gray_01)] edge02 = [v_id_00, v_id_10, get_value(Gray_00, Gray_10)] edge03 = [v_id_00, v_id_11, get_value(Gray_00, Gray_11)] edge12 = [v_id_01, v_id_10, get_value(Gray_01, Gray_10)] edge23 = [v_id_10, v_id_11, get_value(Gray_10, Gray_11)] Edges.append(edge01) Edges.append(edge02) Edges.append(edge03) Edges.append(edge12) Edges.append(edge23) elif j0 == w-2:# 右边界 有 e <1, 3> if 0 <= i0 and i0 < h - 2: # 中间 没有 e <2, 3> edge01 = [v_id_00, v_id_01, get_value(Gray_00, Gray_01)] edge02 = [v_id_00, v_id_10, get_value(Gray_00, Gray_10)] edge03 = [v_id_00, v_id_11, get_value(Gray_00, Gray_11)] edge12 = [v_id_01, v_id_10, get_value(Gray_01, Gray_10)] edge13 = [v_id_01, v_id_11, get_value(Gray_01, Gray_11)] Edges.append(edge01) Edges.append(edge02) Edges.append(edge03) Edges.append(edge12) Edges.append(edge13) elif i0 == h - 2: # 下边界 有 e <2, 3> edge01 = [v_id_00, v_id_01, get_value(Gray_00, Gray_01)] edge02 = [v_id_00, v_id_10, get_value(Gray_00, Gray_10)] edge03 = [v_id_00, v_id_11, get_value(Gray_00, Gray_11)] edge12 = [v_id_01, v_id_10, get_value(Gray_01, Gray_10)] edge13 = [v_id_01, v_id_11, get_value(Gray_01, Gray_11)] edge23 = [v_id_10, v_id_11, get_value(Gray_10, Gray_11)] Edges.append(edge01) Edges.append(edge02) Edges.append(edge03) Edges.append(edge12) Edges.append(edge13) Edges.append(edge23) # Graph = [] # values = [] # for edg in Edges: # if edg[2] < float(‘inf‘): # Graph.append(edg) # values.append(edg[2]) # max_v = max(values) # print(len(Graph)) Edges import pandas as pd import psycopg2 from io import StringIO def table_exist(table_name=None, conn=None, cur=None): try: cur.execute("select to_regclass(" + "\‘" + table_name + "\‘" + ") is not null") rows = cur.fetchall() except Exception as e: rows = [] conn.close() if rows: data = rows flag = data[0][0] return flag # connection the database conn = psycopg2.connect(database="beijing", user="jiangshan", password="jiangshan", host="192.168.1.233", port="5432") cur = conn.cursor() # table_name table_name = "img_graph" # CREATE TABLE IF table IS NOT EXIST # 查询出来的表是否存在的状态,存在则为True,不存在则为False table_flg = table_exist(table_name, conn, cur) if table_flg is False: # sql = "DROP TABLE public.{0} CASCADE".format(table_name)# -- 删除表 sql = "CREATE TABLE IF NOT EXISTS {0} (v_id BIGINT, u_id BIGINT, weight FLOAT)".format(table_name) cur.execute(sql) conn.commit() df = pd.DataFrame(columns=[‘v_id‘, ‘u_id‘, ‘weight‘], data=Edges) # dataframe类型转换为IO缓冲区中的str类型 output = StringIO() df.to_csv(output, sep=‘\t‘, index=False, header=False) output = output.getvalue() # print(output) cur.copy_from(StringIO(output), table_name) conn.commit() # print(‘CREATE INDEX OF THE v_id.....‘) # sql = "CREATE INDEX {0} ON {1} USING btree ({2})".format(table_name + "_v_id_idx", table_name, ‘v_id‘) # cur.execute(sql) # conn.commit() cur.close() conn.close() print(‘done‘)