opencv-pycharm入门(一)

1.读取并显示图片(测试imread的三种参数)

import numpy as np
import cv2 as cv
taeyeon=cv.imread("ty0309.jpeg")
cv.imshow("taeyeon",taeyeon)
cv.waitKey(1000000)

运行结果如下:opencv-pycharm入门(一)_第1张图片
imread中的第二个参数(可缺省):1为默认值,包含彩色通道,0为灰度通道,-1则为原图(包含Alpha通道)
测试三个参数的实际应用:

import numpy as np
import cv2 as cv
taeyeon1=cv.imread("ty0309.jpeg",1)
taeyeon0=cv.imread("ty0309.jpeg",0)
taeyeonfu1=cv.imread("ty0309.jpeg",-1)
cv.imshow("taeyeon1",taeyeon1)
cv.imshow("taeyeon0",taeyeon0)
cv.imshow("taeyeonfu1",taeyeonfu1)
cv.waitKey(1000000)

运行结果如下:opencv-pycharm入门(一)_第2张图片

2.进行图像的形态学操作

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
kernel=np.ones((10,10),np.uint8)
erosion=cv.erode(taeyeon0,kernel,iterations=1)
cv.imshow("erosion",erosion)

cv.waitKey(1000000)

腐蚀和膨胀应该对应的是二值图像的操作,但在这里却可以拓展到三通道的0-255,甚至alpha通道。在cv.erode函数中,拓展到求取指定模板中的最小值(邻域最小值)
opencv-pycharm入门(一)_第3张图片
结果如下:
opencv-pycharm入门(一)_第4张图片
更换模板:
(十字型)

import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
elements=cv.getStructuringElement(cv.MORPH_CROSS,(8,8))

erosion=cv.erode(taeyeon0,elements,iterations=1)
cv.imshow("erosion",erosion)

cv.waitKey(1000000)

结果如下:

同理进行膨胀操作,让其“变亮”:

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
elements=cv.getStructuringElement(cv.MORPH_CROSS,(8,8))

erosion=cv.dilate(taeyeon0,elements,iterations=1)
cv.imshow("erosion",erosion)

cv.waitKey(1000000)

opencv-pycharm入门(一)_第5张图片
开运算和闭运算

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
elements=cv.getStructuringElement(cv.MORPH_CROSS,(8,8))

erosion=cv.morphologyEx(taeyeon0, cv.MORPH_OPEN, elements)
cv.imshow("erosion",erosion)

cv.waitKey(1000000)

opencv-pycharm入门(一)_第6张图片

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
elements=cv.getStructuringElement(cv.MORPH_CROSS,(8,8))

erosion=cv.morphologyEx(taeyeon0, cv.MORPH_CLOSE, elements)
cv.imshow("erosion",erosion)

cv.waitKey(1000000)


3.空间滤波操作
均值滤波:

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
dstimage=cv.blur(taeyeon0,(5,5))
cv.imshow("blur",dstimage)

cv.waitKey(1000000)


中值滤波:

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
dstimage=cv.medianBlur(taeyeon0,5)
cv.imshow("blur",dstimage)

cv.waitKey(1000000)


4.Canny 边缘检测

import numpy as np
import cv2 as cv
taeyeon0=cv.imread("ty0309.jpeg")
dstimage=cv.Canny(taeyeon0,100,150)
cv.imshow("blur",dstimage)

cv.waitKey(1000000)

opencv-pycharm入门(一)_第7张图片

你可能感兴趣的:(opencv,pycharm,计算机视觉)