opencv-python笔记

%config IPCompleter.greedy=True   ----#Tab键代码自动生成

opencv-python

测试opencv导入是否成功

import cv2
img = cv2.imread("C:/Users/JD/Pictures/Saved Pictures/2.jpg", cv2.IMREAD_GRAYSCALE)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.imshow

读个视频

import cv2

img = cv2.VideoCapture("F:/pythonProject/tensflow/opencv/MP4/cxk.mp4")
# 检擦是否打开
while open:
    ret, frame = img.read()
    if frame is None:
        break
    if ret == True:
        #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # cv2.split(frame)
        # cur_img = frame.copy()
        # cur_img[:, :, 2] = 0
        # cur_img[:, :, 1] = 0
        # cv_show("R", cur_img)
        img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        ret, img1 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY)
        cv2.namedWindow("result", cv2.WINDOW_NORMAL)
        cv2.imshow("result", img1)
        if cv2.waitKey(10) & 0XFF == 27:
            break
img.release()
cv2.destroyWindow()
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [288], in ()
     21             break
     22 img.release()
---> 23 cv2.destroyWindow()


TypeError: destroyWindow() missing required argument 'winname' (pos 1)

图像的阈值操作

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TROQ1CFI-1673249377969)(attachment:image.png)]

图像通道的分离

# 截取部分图像数据
import cv2
img = cv2.imread("C:/Users/JD/Pictures/Saved Pictures/2.jpg")
img = img[0:500, 0:500]
def cv_show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)

cv_show("PKQ", img)
b, g, r = cv2.split(img)

img = cv2.merge((b, g, r))
# 只保留R
cur_img = img.copy()
cur_img[:, :, 0] = 0
cur_img[:, :, 1] = 0
cv_show("R", cur_img)

# 只保留G通道
cur_img = img.copy()
cur_img[:, :, 0] = 0
cur_img[:, :, 2] = 0
cv_show("G", cur_img)

# 只保留G通道
cur_img = img.copy()
cur_img[:, :, 1] = 0
cur_img[:, :, 2] = 0
cv_show("G", cur_img)

图像的平滑处理

# 滤波
# 就是卷积操作

import cv2
import numpy as np

img = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img.png")
cv2.imshow("img", img)  
cv2.waitKey(0)
cv2.destroyAllWindows()

# 均值滤波
# 简单的平均卷积操作
blur = cv2.blur(img, (3, 3))
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 方框滤波
# 基本和均值滤波一样,可以选择归一化
box = cv2.boxFilter(img, -1, (3, 3), normalize=False)    # True意思是进行均一化处理

cv2.imshow('box', box)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 高斯滤波
# 高斯函数:越接近于均值的时候可能性越大
# 高斯模糊的卷积核里的数值满足高斯分布,相当于更重视中间的

aussian = cv2.GaussianBlur(img, (5, 5), 1)

cv2.imshow('aussian', aussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 中值滤波
median = cv2.medianBlur(img, 5)

cv2.imshow('median', median)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 展示所有
res = np.hstack((blur, aussian, median))
print(res)
cv2.imshow("meidan vs average", res)
cv2.waitKey(0)
cv2.destroyWindow()
[[[233 218 219]
  [233 218 219]
  [233 218 219]
  ...
  [233 218 219]
  [233 218 219]
  [233 218 219]]

 [[233 218 219]
  [233 218 219]
  [233 218 219]
  ...
  [233 218 219]
  [233 218 219]
  [233 218 219]]

 [[233 218 219]
  [233 218 219]
  [233 218 219]
  ...
  [233 218 219]
  [233 218 219]
  [233 218 219]]

 ...

 [[236 219 220]
  [236 219 220]
  [236 219 220]
  ...
  [238 220 217]
  [238 220 217]
  [238 220 217]]

 [[236 219 220]
  [236 219 220]
  [236 219 220]
  ...
  [238 220 217]
  [238 220 217]
  [238 220 217]]

 [[236 219 220]
  [236 219 220]
  [236 219 220]
  ...
  [238 220 217]
  [238 220 217]
  [238 220 217]]]



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [1], in ()
     44 cv2.imshow("meidan vs average", res)
     45 cv2.waitKey(0)
---> 46 cv2.destroyWindow()


TypeError: destroyWindow() missing required argument 'winname' (pos 1)

礼帽和黑帽

  • 礼帽 = 原始输入-开运算结果(留下刺)
  • 黑帽 = 闭运算-原始输入

图像的梯度操作

  • 梯度=膨胀-腐蚀
  • sobel算子
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F7dP03tP-1673249377970)(attachment:image.png)]
  • Scharr和sobel差不多,但是对变化更加的明显
  • laplacian算子,对噪音点敏感
# 梯度=膨胀-腐蚀
import cv2
import numpy as np

pie = cv2.imread('F:/pythonProject/tensflow/opencv/pictures/img.png')
kernel = np.ones((7, 7), np.uint8)
dilate = cv2.dilate(pie, kernel, iterations=5)
erosion = cv2.erode(pie, kernel, iterations=5)
cv2.namedWindow("res",cv2.WINDOW_NORMAL)
res = np.hstack((dilate, erosion))

cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()


# 计算一张图像的梯度
img = cv2.imread('F:/pythonProject/tensflow/opencv/pictures/img.png')
def cv_show(img, name):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 白到黑是正数,黑到白就是负数了,所有的附属会被截断成0,所以要取绝对值

# 分别计算x和y再求和,不建议直接求和
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
cv_show(sobelx, "sobelx")

sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
cv_show(sobely, "sobely")

# 求和
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
cv_show(sobelxy, "sobelxy")

# Scharr和sobel差不多,但是对变化更加的明显
# -laplacian算子,对噪音点敏感
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
cv_show(laplacian, "laplacian")

Canny算子的边缘检测

# Canny边缘检测
import cv2
import numpy as np

img = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img.png")
v1 = cv2.Canny(img, 80, 150)
v2 = cv2.Canny(img, 50, 100)
res = np.hstack((v1, v2))
cv2.namedWindow("res", cv2.WINDOW_NORMAL)
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

金字塔

  • 向下采样(缩小)
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fkj3MWlu-1673249377970)(attachment:image.png)]
  • 向上采样(放大)
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YyMk8HT5-1673249377971)(attachment:image-2.png)]
import cv2
img = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img.png")
up = cv2.pyrUp(img)
cv2.imshow("up",up)
cv2.waitKey(0)
cv2.destroyAllWindows()
down = cv2.pyrDown(img)
def cv_show(name,img):
    cv2.imshow("name",img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
cv_show(down,down)

拉普拉斯金字塔

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j4ek4VtU-1673249377971)(attachment:image.png)]

up_down = cv2.pyrUp(down)
cv_show('up',up_down)
print(up.shape)
print(img.shape)
cv_show('img',img)
# 大小不一致能直接减
(1418, 2288, 3)
(709, 1144, 3)

轮廓的检测

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IWsAyenG-1673249377971)(attachment:image.png)]
为了更高的准确率,使用二值图像

import numpy as np
img = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img_1.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, th = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
kernel = np.ones((10, 10), np.uint8)
cv2.dilate(th,kernel)
cv_show("th",th)
contours, hierarchy = cv2.findContours(th, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
#  传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
# 注意需要 copy,要不原图会变。
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (255,0,0),2) #-1默认画所有的轮廓
cv_show("res",res)

轮廓特征

cnt = contours[1]
# 面积
cv2.contourArea(cnt)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Input In [3], in ()
----> 1 cnt = contours[1]
      2 # 面积
      3 cv2.contourArea(cnt)


NameError: name 'contours' is not defined
# 计算轮廓的边长
# True 表示是闭合的
cv2.arcLength(cnt, True)
49.899494767189026

轮廓的近似

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JLrrd0pQ-1673249377972)(attachment:image.png)]

epsilon = 0.0001*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt, epsilon,True) # 轮廓的近似

draw_img = img.copy()
res = cv2.drawContours(draw_img, approx, -1, (0,200,0),2)
cv_show('res',res)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Input In [2], in ()
----> 1 epsilon = 0.0001*cv2.arcLength(cnt,True)
      2 approx = cv2.approxPolyDP(cnt, epsilon,True) # 轮廓的近似
      4 draw_img = img.copy()


NameError: name 'cv2' is not defined

模板匹配

模板匹配和卷积的原理很相似,模板在原图像上开始滑动,计算模板与(图像被模板覆盖的地方)的差别度,这个差别程度的计算方法在opencv里面有六种,然后将每次计算的结果放入一个矩阵,作为结果输出,假如原图像是AxB大小,而模板是axb大小,则输出结果的矩阵是(A-a+1)x(B-b+1)

# 模板匹配
img = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img.png",0)
img_2 = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img_2.png",0)
img.shape
h,w = img_2.shape[:2]
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Input In [1], in ()
      1 # 模板匹配
----> 2 img = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img.png",0)
      3 img_2 = cv2.imread("F:/pythonProject/tensflow/opencv/pictures/img_2.png",0)
      4 img.shape


NameError: name 'cv2' is not defined
img_2.shape
(325, 330)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QPVpH1p5-1673249377972)(attachment:image-2.png)]

methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
          'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
res = cv2.matchTemplate(img, img_2,cv2.TM_SQDIFF)
res.shape
(385, 815)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
min_val
356981760.0
max_val
1340058368.0
min_loc
(567, 0)
max_loc
(686, 146)
import matplotlib.pyplot as plt
for meth in methods:
    img3 =img.copy()
    # 匹配方法的真值
    method = eval(meth)
    print(method)
    res = cv2.matchTemplate(img,img_2,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    
    #如果是平方差匹配或者是是归一化平方差,匹配,取最小值
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0]+w,top_left[1]+h )
    
    #画矩形
    cv2.rectangle(img3, top_left,bottom_right,255,2)
    
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.xticks([]),plt.yticks([])# 隐藏坐标轴
    plt.subplot(122),plt.imshow(img3,cmap = 'gray')
    plt.xticks([]),plt.yticks([])
    plt.suptitle(meth)
    plt.show()
4

opencv-python笔记_第1张图片

5

opencv-python笔记_第2张图片

2

opencv-python笔记_第3张图片

3

opencv-python笔记_第4张图片

0

opencv-python笔记_第5张图片

1

opencv-python笔记_第6张图片

插入一个shape的用法

img.shape[:2] 取彩色图片的长、宽。
如果img.shape[:3] 则取彩色图片的长、宽、通道。

关于img.shape[0]、[1]、[2]
img.shape[0]:图像的垂直尺寸(高度)
img.shape[1]:图像的水平尺寸(宽度)
img.shape[2]:图像的通道数

在矩阵中,[0]就表示行数,[1]则表示列数。

傅里叶变换和滤波器

opencv-python笔记_第7张图片

傅里叶变化

opencv-python笔记_第8张图片

低通滤波器

opencv-python笔记_第9张图片


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