i_b = b.ravel() mea1_b = np.mean(i_b) mea = np.zeros(shape=(1,1)) while True: mea1 = mea[0] i_b1 = np.where(i_b>mea1_b) mea2_b = np.mean(i_b[i_b1]) i_b2 = np.where(i_b<mea1_b) mea3_b = np.mean(i_b[i_b2]) mea1_b = (mea3_b+mea2_b)/2 if mea1_b == mea1: break else: mea[0] = mea1_b I_B = b I_B[I_B>mea1_b] = 255 I_B[I_B<mea1_b] = 0 area_b = np.where(I_B==255) print(mea1_b) i_r = r.ravel() mea1_r = np.mean(i_r) mea = np.zeros(shape=(1,1)) while True: mea1 = mea[0] i_r1 = np.where(i_r>mea1_r) mea2_r = np.mean(i_r[i_r1]) i_r2 = np.where(i_r<mea1_r) mea3_r = np.mean(i_r[i_r2]) mea1_r = (mea3_r+mea2_r)/2 if mea1_r == mea1: break else: mea[0] = mea1_r I_R = r I_R[I_R>mea1_r] = 255 I_R[I_R<mea1_r] = 0 area_r = np.where(I_R==255) I_Rx = area_r[0] I_Ry = area_r[1] print(mea1_r)
时间: 2024-03-26 19:37:14 浏览: 85
这段代码是在进行图像处理,主要是对输入的图像进行二值化处理,将图像中的像素值分成两部分,一部分为0,一部分为255。代码中采用的方法是Otsu算法,通过计算图像中像素值的均值,进而求出一个阈值,将像素值分成两部分。具体来说,代码中首先将图像中的像素值展平为一个一维数组,然后求出该数组的均值。接下来,利用一个循环不断迭代求出新的均值,直到均值不再发生变化为止。然后,将像素值大于均值的像素值赋值为255,小于均值的像素值赋值为0。最后,打印出均值。
其中,对于图像中的红色通道和蓝色通道,分别进行了上述操作,并分别得到了二值化处理后的图像中像素值为255的坐标。
相关问题
root = tk.Tk() root.withdraw() f_path = filedialog.askopenfilename() I0 = cv2.imread(f_path ) b, g, r = cv2.split(I0) m, n = r.shape flag = False mode = 0 def abc(x): global flag a = keyboard.KeyboardEvent(event_type='down', scan_code=2, name='1') b = keyboard.KeyboardEvent(event_type='down', scan_code=3, name='2') c = keyboard.KeyboardEvent(event_type='down', scan_code=4, name='3') if x.event_type == a.event_type and x.scan_code == a.scan_code: print("迭代式阈值选择算法") mode = 1 flag = True if x.event_type == b.event_type and x.scan_code == b.scan_code: print("大律算法") flag = True mode = 2 if x.event_type == c.event_type and x.scan_code == c.scan_code: print("三角算法") flag = True mode = 3 keyboard.hook(abc) if flag == False: time.sleep(5) # 等待5秒 if mode == 1: i_b = b.ravel() mea1_b = np.mean(i_b) mea = np.zeros(shape=(1, 1)) while True: mea1 = mea[0] i_b1 = np.where(i_b > mea1_b) mea2_b = np.mean(i_b[i_b1]) i_b2 = np.where(i_b < mea1_b) mea3_b = np.mean(i_b[i_b2]) mea1_b = (mea3_b + mea2_b) / 2 if mea1_b == mea1: break else: mea[0] = mea1_b I_B = b I_B[I_B > mea1_b] = 255 I_B[I_B < mea1_b] = 0 area_b = np.where(I_B == 255) print(mea1_b) i_r = r.ravel() mea1_r = np.mean(i_r) mea = np.zeros(shape=(1, 1)) while True: mea1 = mea[0] i_r1 = np.where(i_r > mea1_r) mea2_r = np.mean(i_r[i_r1]) i_r2 = np.where(i_r < mea1_r) mea3_r = np.mean(i_r[i_r2]) mea1_r = (mea3_r + mea2_r) / 2 if mea1_r == mea1: break else: mea[0] = mea1_r I_R = r I_R[I_R > mea1_r] = 255 I_R[I_R < mea1_r] = 0 area_r = np.where(I_R == 255) I_Rx = area_r[0] I_Ry = area_r[1] print(mea1_r)哪里出现了问题
在函数 `abc(x)` 中,变量 `mode` 在函数内部被赋值,但没有使用 `global` 声明为全局变量。因此,在后面的条件语句中,`mode` 始终为 0,导致程序一直执行 `mode == 1` 的分支,而不是根据用户输入的按键执行相应的阈值选择算法。解决方法是在函数 `abc(x)` 中,将 `mode` 声明为全局变量,即在函数开头加上 `global mode`。
threshold_r = np.sort(np.abs(coeffs_r[1].ravel()))[::-1][int(compress_ratio * len(coeffs_r[1].ravel()))] 17 threshold_g = np.sort(np.abs(coeffs_g[1].ravel()))[::-1][int(compress_ratio * len(coeffs_g[1].ravel()))] 18 threshold_b = np.sort(np.abs(coeffs_b[1].ravel()))[::-1][int(compress_ratio * len(coeffs_b[1].ravel()))] AttributeError: 'tuple' object has no attribute 'ravel'
根据你提供的错误信息,提示出现了 AttributeError: 'tuple' object has no attribute 'ravel',说明 coeffs_r[1]、coeffs_g[1] 和 coeffs_b[1] 这三个元素是元组类型,而不是数组类型。因此,无法使用 ravel 函数将它们转换为一维数组。
你可以将 coeffs_r[1]、coeffs_g[1] 和 coeffs_b[1] 转换为数组类型,然后再使用 ravel 函数将它们转换为一维数组。你可以尝试在代码中添加以下语句:
```
coeffs_r_arr = np.array(coeffs_r[1])
coeffs_g_arr = np.array(coeffs_g[1])
coeffs_b_arr = np.array(coeffs_b[1])
threshold_r = np.sort(np.abs(coeffs_r_arr.ravel()))[::-1][int(compress_ratio * len(coeffs_r_arr.ravel()))]
threshold_g = np.sort(np.abs(coeffs_g_arr.ravel()))[::-1][int(compress_ratio * len(coeffs_g_arr.ravel()))]
threshold_b = np.sort(np.abs(coeffs_b_arr.ravel()))[::-1][int(compress_ratio * len(coeffs_b_arr.ravel()))]
```
这样就可以将 coeffs_r[1]、coeffs_g[1] 和 coeffs_b[1] 转换为数组类型,并使用 ravel 函数将它们转换为一维数组,然后计算阈值。
阅读全文