阈值分割
灰度图
import cv2 as cv
import numpy as np
import os
import base_function as bf
sourceImg = cv.imread("static/26.png")
grayImg = cv.cvtColor(sourceImg,cv.COLOR_BGR2GRAY)
bf.cv_show("grayImg",grayImg)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/db4a93666b3a73cf6dfe14387b4f2a7e.png)
二值化
ret,thresh1 = cv.threshold(grayImg,100,255,cv.THRESH_BINARY)
bf.cv_show("THRESH_BINARY",thresh1)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/147217fc76cf60405b551646f944081d.png)
二值化取反
ret,thresh2 = cv.threshold(grayImg,100,255,cv.THRESH_BINARY_INV)
bf.cv_show("THRESH_BINARY_INV",thresh2)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6ac2a67f16927750f0f7d0902568c047.png)
截取
ret,thresh3 = cv.threshold(grayImg,100,0,cv.THRESH_TRUNC)
bf.cv_show("THRESH_TRUNC",thresh3)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b456df591f17a38648904d8ab63b2fda.png)
TOZERO
ret,thresh4 = cv.threshold(grayImg,100,255,cv.THRESH_TOZERO)
bf.cv_show("THRESH_TRUNC",thresh4)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ed2ec8627f1e4dc23febdbb9f83488c.png)
TOZERO取反
ret,thresh4 = cv.threshold(grayImg,100,255,cv.THRESH_TOZERO_INV)
bf.cv_show("THRESH_TRUNC",thresh4)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9beb26fd7561f271b03187992d616040.png)
滤波
均值滤波
from math import fabs
import cv2 as cv
import numpy as np
import base_function as bf
source = cv.imread("static/1.png")
cv.imshow("source",source)
cv.waitKey(1000)
cv.destroyAllWindows()
blur = cv.blur(source,(5,5))
bf.cv_show("blur",blur)
高斯滤波
gus = cv.GaussianBlur(source,(5,5),5)
bf.cv_show("gus",gus)
中值滤波
middle = cv.medianBlur(source,5)
bf.cv_show("middle",middle)
图像拼接
简单的横向和纵向拼接
all = cv.hconcat([blur,gus,middle])
bf.cv_show("all",all)
allv = cv.vconcat([blur,gus,middle])
bf.cv_show("allv",allv)
效果(三幅图片分别是均值滤波,高斯滤波,中值滤波)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5f8058a84b4093c54887ecd925c03210.png)
腐蚀与膨胀
import cv2 as cv
import base_function as bf
import numpy as np
position = (10, 50)
font = cv.FONT_HERSHEY_SIMPLEX
scale = 1
color = (255, 255, 255)
thickness = 2
source = cv.imread("static/26.png")
gray = cv.cvtColor(source,cv.COLOR_BGR2GRAY)
kernel = np.ones((7,7),np.uint8)
ret,binary = cv.threshold(gray,100,255,cv.THRESH_BINARY_INV)
erode = cv.erode(binary,kernel,iterations=1)
dilate = cv.dilate(erode,kernel,iterations=1)
open = cv.morphologyEx(binary,cv.MORPH_OPEN,kernel)
close = cv.morphologyEx(binary,cv.MORPH_CLOSE,kernel)
cv.putText(gray,"gray",position,font,scale,color,thickness)
cv.putText(binary,"binary",position,font,scale,color,thickness)
cv.putText(erode,"erode",position,font,scale,color,thickness)
cv.putText(dilate,"dilate",position,font,scale,color,thickness)
cv.putText(open,"open",position,font,scale,color,thickness)
cv.putText(close,"close",position,font,scale,color,thickness)
all =np.vstack((np.hstack((gray,binary,erode)) ,np.hstack((dilate,open,close))))
bf.cv_show("all",all)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7c469f39dec4d52d404420f209a56929.png)