说明一下,所有代码需要:基础代码(通用)+ 关键代码才能运行。请自行结合
基础代码
import cv2 as cv
# 读路径下的图片
img = cv.imread("./cat.jpg")
# 在窗口显示
cv.imshow("color", img)
cv.waitKey(0)
关键代码
展示变灰图片
# 变灰
grey = cv.cvtColor(src=img, code=cv.COLOR_BGR2GRAY)
cv.imshow('gray', grey)
效果
模糊(高斯模糊)
blur = cv.GaussianBlur(img,(5,5),cv.BORDER_DEFAULT)
cv.imshow("blur", blur)
效果
边缘检测
canny = cv.Canny(img, threshold1=100, threshold2=200)
cv.imshow("canny", canny)
效果
膨胀
dilated = cv.dilate(src=canny, kernel=(10,10), iterations=1)
cv.imshow("dilated", dilated)
效果
缩放
sized = cv.resize(img, dsize=(100,100), interpolation=cv.INTER_CUBIC)
cv.imshow("size", sized)
效果
裁剪
cropped = img[50:250, 50:250]
cv.imshow("croppred", cropped)
效果
完整代码
import cv2 as cv
# 读路径下的图片
img = cv.imread("./cat.jpg")
# 变灰
# grey = cv.cvtColor(src=img, code=cv.COLOR_BGR2GRAY)
# cv.imshow('gray', grey)
# 模糊
# blur = cv.GaussianBlur(img,(5,5),cv.BORDER_DEFAULT)
# cv.imshow("blur", blur)
# 边缘检测
canny = cv.Canny(img, threshold1=100, threshold2=200)
# cv.imshow("canny", canny)
# 膨胀
# dilated = cv.dilate(src=canny, kernel=(10,10), iterations=1)
# cv.imshow("dilated", dilated)
# 缩放
# sized = cv.resize(img, dsize=(100,100), interpolation=cv.INTER_CUBIC)
# cv.imshow("size", sized)
# 剪裁
cropped = img[50:250, 50:250]
cv.imshow("croppred", cropped)
cv.imshow("color", img)
cv.waitKey(0)