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 08:37:14 浏览: 89
color_hist.rar_颜色直方图
这段代码是在进行图像处理,主要是对输入的图像进行二值化处理,将图像中的像素值分成两部分,一部分为0,一部分为255。代码中采用的方法是Otsu算法,通过计算图像中像素值的均值,进而求出一个阈值,将像素值分成两部分。具体来说,代码中首先将图像中的像素值展平为一个一维数组,然后求出该数组的均值。接下来,利用一个循环不断迭代求出新的均值,直到均值不再发生变化为止。然后,将像素值大于均值的像素值赋值为255,小于均值的像素值赋值为0。最后,打印出均值。
其中,对于图像中的红色通道和蓝色通道,分别进行了上述操作,并分别得到了二值化处理后的图像中像素值为255的坐标。
阅读全文