def method_name(gray): a, b, c = modify.point_classification_new(gray, 1, 1, 1) for x in range(len(a)): print(gray[a[x][0], a[x][1]], end=" ") print(a[x], end=" ") print() for x in range(len(b)): print(gray[b[x][0], b[x][1]], end=" ") print(b[x], end=" ") print() for x in range(len(c)): print(gray[c[x][0], c[x][1]], end=" ") print(c[x], end=" ") print()
def fix_noise(src1, tag, noise_num): src = src1.copy() for i in range(tag.shape[0]): for j in range(tag.shape[1]): if tag[i, j] > 1: a_sum = 0 a_num = 0 b_sum = 0 b_num = 0 minn, a, b = modify.point_classification_new(src1, i, j, 1) for aa in a: # if tag[aa[0], aa[1]] == 0: if tag[aa[0], aa[1]] <= noise_num: a_sum += src[aa[0], aa[1]] a_num += 1 for bb in b: # if tag[bb[0], bb[1]] == 0: if tag[bb[0], bb[1]] <= noise_num: b_sum += src[bb[0], bb[1]] b_num += 1 if b_num > 0 and a_num > 0: if abs(src[i, j] - a_sum / a_num) > abs(src[i, j] - b_sum / b_num): src[i, j] = b_sum / b_num else: src[i, j] = a_sum / a_num # 输出测试结果 # if (i == 7 and j == 72 and b_num != 0): # print(b_sum / b_num) # if (i == 7 and j == 72 and a_num != 0): # print(a_sum / a_num) # if (i == 7 and j == 72): # print(b_num) # print(a_num) # print(b_sum) # print(a_sum) return src
def cut(gray, th, th2, rgb): re = [] # 存储符合条件的八邻域分割点 re_weak = [] noise_num = 0 noise_pos = np.zeros((gray.shape[0], gray.shape[1])) for i in range(1, gray.shape[0] - 1): for j in range(1, gray.shape[1] - 1): mul_list = {} # 存储每个边的能量值与坐标信息 poit = {} # 存储每个点的能量值与坐标信息 for k in range(len(xx)): poit[(i + xx[k], j + yy[k])] = gray[i + xx[k], j + yy[k]] # if i == 1 and j == 1: # # print(i + xx[k], j + yy[k]) # # print(i + xx[k - 1], j + yy[k - 1]) # print(type(gray[i + xx[k], j + yy[k]])) # print(type(gray[i + xx[k - 1], j + yy[k - 1]])) # # print((gray[i + xx[k], j + yy[k]] - gray[i + xx[k - 1], j + yy[k - 1]])) # print(int(gray[i + xx[k], j + yy[k]]) - int(gray[i + xx[k - 1], j + yy[k - 1]])) mul_list[((i + xx[k], j + yy[k]), (i + xx[k - 1], j + yy[k - 1]))] = pow( int(rgb[i + xx[k], j + yy[k], 0]) - int(rgb[i + xx[k - 1], j + yy[k - 1], 0]), 2) + pow( int(rgb[i + xx[k], j + yy[k], 1]) - int(rgb[i + xx[k - 1], j + yy[k - 1], 1]), 2) + pow( int(rgb[i + xx[k], j + yy[k], 2]) - int(rgb[i + xx[k - 1], j + yy[k - 1], 2]), 2) noise, a, b = modify.point_classification_new(gray, i, j, 1) # 找到八邻域中的一个噪声点,后续判断噪声点是否影响分割点 noise_pos[noise[0][0], noise[0][1]] = 1 sum_a = 0 num_a = 0 sum_b = 0 num_b = 0 raws = gray point_a = [] point_b = [] for k in range(len(a)): sum_a += raws[a[k][0], a[k][1]] num_a += 1 point_a.append(raws[a[k][0], a[k][1]]) for k in range(len(b)): sum_b += raws[b[k][0], b[k][1]] num_b += 1 point_b.append(raws[b[k][0], b[k][1]]) maxa = max(point_a) # 两个区域的的四个极值 maxb = max(point_b) mina = min(point_a) minb = min(point_b) # if (maxa >= maxb and mina <= maxb): # re[i, j] = 0 # elif (maxb >= maxa and minb <= maxa): # re[i, j] = 0 # print(mul_list) # 将每条边的能量进行排序,得到能量最大的两条边为索引1和0, 但是得到的数据类型是列表 mul_list = sorted(mul_list.items(), key=lambda item: item[1], reverse=True) # print(mul_list) # print(type(mul_list)) # mina = min(mul_list[0][0][0][0], mul_list[1][0][0][0]) # minb = min(mul_list[0][0][0][1], mul_list[1][0][0][1]) # x1 = (mul_list[0][0][0][0], mul_list[0][0][0][1]) # x2 = (mul_list[1][0][0][0], mul_list[1][0][0][1]) n1 = mul_list[0][0] # 两个含有噪声点的边的坐标信息(不一定含有噪声,只是能量边最大) n2 = mul_list[1][0] # if i == 1 and j == 1: # print(mul_list) # print(a) # print(b) # print(n1) # print(n2) # print(gray[a, b]) # del (poit[(a, b)]) # 删除是噪声点那个点 # print(poit) noise_po, po1, po2 = find_(n1, n2) # print(noise_po) # print(po1) # print(po2) # if(a1[0][0]==a and a1[0][1]== b): # if noise[0] == (mina, minb): # 判断噪声点影响了八邻域分隔 if po1 != 0 and noise_po == noise[0]: noise_num += 1 # while po1 != 0 and noise_po == noise[0]: # fix_noise(gray, i, j, noise[0][0], noise[0][1]) # noise, a, b = modify.point_classification_new(gray, i, j, 1) # 找到八邻域中的一个噪声点,后续判断噪声点是否影响分割点 # mul_list = {} # for k in range(len(xx)): # mul_list[((i + xx[k], j + yy[k]), (i + xx[k - 1], j + yy[k - 1]))] = ( # pow((gray[i + xx[k], j + yy[k]] - gray[i + xx[k - 1], j + yy[k - 1]]), 2)) # mul_list = sorted(mul_list.items(), key=lambda item: item[1], reverse=True) # n1 = mul_list[0][0] # 两个含有噪声点的边的坐标信息(不一定含有噪声,只是能量边最大) # n2 = mul_list[1][0] # noise_po, po1, po2 = find_(n1, n2) # print("while") # print("fix") if mina > maxb or minb > maxa: if abs((sum_a / num_a) - (sum_b / num_b)) > th: # continue re.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标 re.append(n2) elif th >= abs((sum_a / num_a) - (sum_b / num_b)) >= th2: re_weak.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标 re_weak.append(n2) # r1 = mul_list[0][0] # del mul_list[0] # 删除最大能量的两条边 # r2 = mul_list[0][0] # del mul_list[0] # # # m1, m2, m3 = find_(r1, r2) # # mul_list = listtomap(mul_list) # 把相应的列表重新转为map类型 # # mul_list[((m2), (m3))] = pow((gray[m2[0]][m2[1]] - gray[m3[0]][m3[1]]), 2) # 去掉一个噪声点之后的边的能量加入进去 # re.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标 # re.append(n2) else: if mina > maxb or minb > maxa: if abs((sum_a / num_a) - (sum_b / num_b)) > th: # continue re.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标 re.append(n2) elif th >= abs((sum_a / num_a) - (sum_b / num_b)) >= th2: re_weak.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标 re_weak.append(n2) # print(noise_num) return re, re_weak, noise_pos
def show_cut(gray, th, th2, rgb, i, j): re = [] # 存储符合条件的八邻域分割点 re_weak = [] noise_num = 0 noise_pos = np.zeros((gray.shape[0], gray.shape[1])) # for i in range(1, gray.shape[0] - 1): # for j in range(1, gray.shape[1] - 1): mul_list = {} # 存储每个边的能量值与坐标信息 poit = {} # 存储每个点的能量值与坐标信息 for k in range(len(xx)): poit[(i + xx[k], j + yy[k])] = gray[i + xx[k], j + yy[k]] # if i == 1 and j == 1: # # print(i + xx[k], j + yy[k]) # # print(i + xx[k - 1], j + yy[k - 1]) # print(type(gray[i + xx[k], j + yy[k]])) # print(type(gray[i + xx[k - 1], j + yy[k - 1]])) # # print((gray[i + xx[k], j + yy[k]] - gray[i + xx[k - 1], j + yy[k - 1]])) # print(int(gray[i + xx[k], j + yy[k]]) - int(gray[i + xx[k - 1], j + yy[k - 1]])) mul_list[((i + xx[k], j + yy[k]), (i + xx[k - 1], j + yy[k - 1]))] = pow( int(rgb[i + xx[k], j + yy[k], 0]) - int(rgb[i + xx[k - 1], j + yy[k - 1], 0]), 2) + pow( int(rgb[i + xx[k], j + yy[k], 1]) - int(rgb[i + xx[k - 1], j + yy[k - 1], 1]), 2) + pow( int(rgb[i + xx[k], j + yy[k], 2]) - int(rgb[i + xx[k - 1], j + yy[k - 1], 2]), 2) noise, a, b = modify.point_classification_new(gray, i, j, 1) # 找到八邻域中的一个噪声点,后续判断噪声点是否影响分割点 noise_pos[noise[0][0], noise[0][1]] = 1 sum_a = 0 num_a = 0 sum_b = 0 num_b = 0 raws = gray point_a = [] point_b = [] for k in range(len(a)): sum_a += raws[a[k][0], a[k][1]] num_a += 1 point_a.append(raws[a[k][0], a[k][1]]) for k in range(len(b)): sum_b += raws[b[k][0], b[k][1]] num_b += 1 point_b.append(raws[b[k][0], b[k][1]]) maxa = max(point_a) # 两个区域的的四个极值 maxb = max(point_b) mina = min(point_a) minb = min(point_b) # if (maxa >= maxb and mina <= maxb): # re[i, j] = 0 # elif (maxb >= maxa and minb <= maxa): # re[i, j] = 0 # print(mul_list) # 将每条边的能量进行排序,得到能量最大的两条边为索引1和0, 但是得到的数据类型是列表 mul_list = sorted(mul_list.items(), key=lambda item: item[1], reverse=True) # print(mul_list) # print(type(mul_list)) # mina = min(mul_list[0][0][0][0], mul_list[1][0][0][0]) # minb = min(mul_list[0][0][0][1], mul_list[1][0][0][1]) # x1 = (mul_list[0][0][0][0], mul_list[0][0][0][1]) # x2 = (mul_list[1][0][0][0], mul_list[1][0][0][1]) n1 = mul_list[0][0] # 两个含有噪声点的边的坐标信息(不一定含有噪声,只是能量边最大) n2 = mul_list[1][0] print(n1,end="") print(n2)
def gradient_average_new(raws, x, y, noise_num): re = np.zeros((x, y)) for i in range(1, x - 1): for j in range(1, y - 1): noise, a, b = modify.point_classification_new( raws, i, j, noise_num) # 在八邻域中找到一个矛盾点剩余部分划分为两个部分 # print(b) point_a = [] point_b = [] sum_a = 0 num_a = 0 sum_b = 0 num_b = 0 min_a = 255 # 获取中心点离两个区间的的最近差值 min_b = 255 for k in range(len(a)): # point_a.append(raws[i + a[k][0] - 1, j + a[k][1] - 1]) // 这里使用的相对地址,但是前面的函数传入的是绝对地址 point_a.append(raws[a[k][0], a[k][1]]) # sum_a += raws[a[k][0] - 1, a[k][1] - 1] sum_a += raws[a[k][0], a[k][1]] num_a += 1 for k in range(len(b)): # point_b.append(raws[i + b[k][0] - 1, j + b[k][1] - 1]) point_b.append(raws[b[k][0], b[k][1]]) # sum_b += raws[b[k][0] - 1, b[k][1] - 1] sum_b += raws[b[k][0], b[k][1]] num_b += 1 maxa = max(point_a) # 两个区域的的四个极值 maxb = max(point_b) mina = min(point_a) minb = min(point_b) if maxa >= maxb >= mina: avg_a = sum_a / num_a avg_b = sum_b / num_b if (abs(raws[i, j] - avg_a) > abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_b - avg_a # else: # re[i,j] = avg_b - avg_a if (abs(raws[i, j] - avg_a) < abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_a - avg_b elif maxb >= maxa >= minb: avg_a = sum_a / num_a avg_b = sum_b / num_b if (abs(raws[i, j] - avg_a) > abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_b - avg_a # else: # re[i,j] = avg_b - avg_a if (abs(raws[i, j] - avg_a) < abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_a - avg_b else: avg_a = sum_a / num_a avg_b = sum_b / num_b if (abs(raws[i, j] - avg_a) > abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_b - avg_a # else: # re[i,j] = avg_b - avg_a if (abs(raws[i, j] - avg_a) < abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_a - avg_b return re
def gradient_average(raws, x, y, noise_num): re = np.zeros((x, y)) for i in range(1, x - 1): for j in range(1, y - 1): noise, a, b = modify.point_classification_new( raws, i, j, noise_num) # 在八邻域中找到一个矛盾点剩余部分划分为两个部分 # print(b) point_a = [] point_b = [] sum_a = 0 num_a = 0 sum_b = 0 num_b = 0 min_a = 255 # 获取中心点离两个区间的的最近差值 min_b = 255 for k in range(len(a)): # point_a.append(raws[i + a[k][0] - 1, j + a[k][1] - 1]) // 这里使用的相对地址,但是前面的函数传入的是绝对地址 point_a.append(raws[a[k][0], a[k][1]]) # sum_a += raws[a[k][0] - 1, a[k][1] - 1] sum_a += raws[a[k][0], a[k][1]] num_a += 1 for k in range(len(b)): # point_b.append(raws[i + b[k][0] - 1, j + b[k][1] - 1]) point_b.append(raws[b[k][0], b[k][1]]) # sum_b += raws[b[k][0] - 1, b[k][1] - 1] sum_b += raws[b[k][0], b[k][1]] num_b += 1 maxa = max(point_a) # 两个区域的的四个极值 maxb = max(point_b) mina = min(point_a) minb = min(point_b) if (maxa >= maxb and mina <= maxb): re[i, j] = 0 elif (maxb >= maxa and minb <= maxa): re[i, j] = 0 else: avg_a = sum_a / num_a avg_b = sum_b / num_b if (abs(raws[i, j] - avg_a) > abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_b - avg_a if (i == 87 and j == 205): print(a) print(avg_a) print(b) print(avg_b) # else: # re[i,j] = avg_b - avg_a if (abs(raws[i, j] - avg_a) < abs(raws[i, j] - avg_b)): # if (avg_a > avg_b): re[i, j] = avg_a - avg_b if (i == 87 and j == 205): print(a) print(avg_a) print(b) print(avg_b) # else: # re[i,j] = avg_a - avg_b # for k in range(len(a)): # temp = abs(raws[i, j] - raws[a[k][0] - 1, a[k][1] - 1]) # if (min_a > temp): # min_a = temp # for k in range(len(b)): # temp = abs(raws[i, j] - raws[b[k][0] - 1, b[k][1] - 1]) # if (min_b > temp): # min_b = temp # # re[i, j] = abs(sum_a / num_a - sum_b / num_b) # # if (min_a > min_b): # if (sum_a / num_a > sum_b / num_b): # re[i, j] = -re[i, j] # else: # re[i, j] = re[i, j] # if (min_a < min_b): # if (sum_a / num_a > sum_b / num_b): # re[i, j] = re[i, j] # else: # re[i, j] = -re[i, j] # if( abs(sum_a / num_a - sum_b / num_b) >5): # re[i, j] = abs(sum_a - sum_b) # re[i,j] = abs(avg_a-avg_b) return re
inpath = "D:\\in\\" outpath = "D:\\out\\" src = "a" # raw = cv2.imread(inpath + src + ".jpg") # raw_Filter = cv2.bilateralFilter(raw, 7, 50, 50) # cv2.imwrite(outpath + src + "__srcBil" + ".jpg", raw_Filter) # raw = raw_Filter # raw2 = cv2.cvtColor(raw_Filter, cv2.COLOR_BGR2GRAY) # re = cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY) # re = np.loadtxt(path1, dtype=np.int, delimiter=",") # re = np.array([[120, 120, 120], [120, 100, 170], [140, 130, 160]]) re = np.array([[150, 150, 150], [120, 120, 120], [120, 140, 130]]) # no, a, b = modify.point_classification_new(re, 7, 72, 1) # no, a, b = modify.point_classification_new(re, 1, 1, 2) no, a, b = modify.point_classification_new(re, 1, 1, 0) for x in no: print(re[x[0], x[1]], end=" ") print() # print(a) for x in a: print(re[x[0], x[1]], end=" ") print() for x in b: print(re[x[0], x[1]], end=" ") # print(b) print() # changeTest.gradient_average(re, re.shape[0], re.shape[1])