3个高效技巧:快速掌握MTKClient刷机工具的核心用法
2026/5/5 8:17:26
1.格式:ret,dst = cv2.threshold(src,thresh,maxval,type)
ret, thresh1 = cv2.threshold(img1_grey, 127,255,cv2.THRESH_BINARY) ret, thresh2 = cv2.threshold(img1_grey, 127,255,cv2.THRESH_BINARY_INV) ret, thresh3 = cv2.threshold(img1_grey, 127,255,cv2.THRESH_TRUNC) ret, thresh4 = cv2.threshold(img1_grey, 127,255,cv2.THRESH_TOZERO) ret, thresh5 = cv2.threshold(img1_grey, 127,255,cv2.THRESH_TOZERO_INV) titles = ['OriginalImage','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] images = [img1, thresh1,thresh2,thresh3,thresh4,thresh5] for i in range(6): # 创建子图(拆分逗号写法,更易读) plt.subplot(2, 3, i+1) # 绘制图像:灰度图用cmap='gray',原图已转RGB无需配色 plt.imshow(images[i], cmap='gray' if i>0 else None) # 添加标题 plt.title(titles[i]) # 修正:拆分xticks和yticks调用,取消链式错误写法 plt.xticks([]) plt.yticks([]) # 调整子图间距,避免标题/图像重叠 plt.tight_layout() # 显示图像 plt.show()上面代码中“img1_grey”要换成自己图片的名称,示例结果如下:
img = cv2.imread('D:/Users/3.jpg')#路径换成自己的 cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows()——一种线性平滑滤波方法,通过计算邻域内像素的平均值替代中心像素值,降低图像噪声。
其中(3,3)是可以自己规定的选取图片窗口大小,不同的值最后呈现结果不一样。
#均值滤波 #简单的平均卷积操作 blur = cv2.blur(img, (3, 3)) cv2.imshow('blur',blur) cv2.waitKey(0) cv2.destroyAllWindows()normalize所等于的值不一样,最后结果不同,如果是True的话,是小于255,避免溢出,若等于False容易溢出,导致图像变白。
#方框滤波 #基本和均值一样,可以选择归一化 box = cv2.boxFilter(img,-1,(3,3),normalize=True) cv2.imshow('box',box) cv2.waitKey(0) cv2.destroyAllWindows()#高斯滤波 #高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的 aussian = cv2.GaussianBlur(img,(5,5),1)#5,5是所框选的范围。5*5 cv2.imshow('aussian',aussian) cv2.waitKey(0) cv2.destroyAllWindows()#中值滤波 #相当于用中值代替 median = cv2.medianBlur(img,5)#中值滤波 cv2.imshow('median',median) cv2.waitKey(0) cv2.destroyAllWindows()6.将所有的方法放在一起展示结果
1.读入图片之后,可以调整展示的窗口的大小
img = cv2.imread("D:/Users/4.jpg") new_width = 400 new_height = 300 resized_img = cv2.resize(img,(new_width,new_height)) cv2.imshow('resized_img',resized_img) cv2.waitKey(0) cv2.destroyAllWindows()2.设置盒子以及迭代次数,来决定腐蚀的速度
kernel = np.ones((5,5),np.uint8)#内盒决定腐蚀的速度 erosion = cv2.erode(resized_img,kernel,iterations = 1)#迭代次数 cv2.imshow('erosion',erosion) cv2.waitKey(0) cv2.destroyAllWindows()3.放在一起展示不同的迭代次数,所呈现的不同效果
kernel = np.ones((3,3),np.uint8) erosion_1 = cv2.erode(resized_img,kernel,iterations = 1) erosion_2 = cv2.erode(resized_img,kernel,iterations = 2) erosion_3 = cv2.erode(resized_img,kernel,iterations = 3) res = np.hstack((erosion_1,erosion_2,erosion_3)) cv2.imshow('res',res) cv2.waitKey(0) cv2.destroyAllWindows()