• OpenCV从入门到入坟


    小脑一抽去学了狗都不学的CV,几天热度到现在全是凉水

    学习视频链接b站openCV

    1. 图像基本操作

    1.1 图片处理

    • cv2.IMREAD_COLOR: 彩色图像
    • cv2.IMREAD_GRAYSCALE:灰度图像
    import cv2  #opencv读取的格式的RGB
    import matplotlib.pyplot as plt  # matplotlib的格式是RGB
    import numpy as np
    %matplotlib inline
    
    • 1
    • 2
    • 3
    • 4
    图片读取
    img = cv2.imread('img/cat.jpg')
    print(img.shape)  # (414, 500, 3)
    img
    ""
    array([[[142, 151, 160],
            [146, 155, 164],
            [151, 160, 170],
            ...,
            [183, 198, 200],
            [128, 143, 145],
            [127, 142, 144]]], dtype=uint8)
    ""
    
    def cv_show(name, img):
        cv2.imshow(name, img)
        cv2.waitKey(0)  # 等待时间,毫秒级,0表示任意键终止
        cv2.destroyAllWindows()
    
    # 灰度图
    img = cv2.imread('img/cat.jpg', cv2.IMREAD_GRAYSCALE)
    print(img.shape, type(img), img.size, img.dtype)  # (414, 500)  207000 uint8
    cv_show('image', img)
    cv2.imwrite('img/mycat.jpg', img)
    
    # 截取部分图像
    cat = img[0:50, 0:200] 
    cv_show('cat', cat)
    
    # 颜色通道提取
    img = cv2.imread('img/cat.jpg')
    b, g, r = cv2.split(img)
    print(r.shape) # (414, 500)
    img = cv2.merge((b, g, r))
    print(img.shape)  # (414, 500, 3)
    
    # 只保留R
    cur_img = img.copy()
    cur_img[:,:,0] = 0
    cur_img[:,:,1] = 0
    cv_show('G', cur_img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    边界填充
    • BORDER_REPLICATE:复制最边缘像素
    • BORDER_REFLECT:反射法,对感兴趣的图像中的像素在两边进行复制例如:fedcba|abcdefgh|hgfedcb
    • BORDER_REFLECT_101:反射法,也就是以最边缘像素为轴,对称,gfedcb|abcdefgh|gfedcba
    • BORDER_WRAP:外包装法cdefgh|abcdefgh|abcdefg
    • BORDER_CONSTANT:常量法,常数值填充
    top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
    replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
    reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT)
    reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT_101)
    wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_WRAP)
    constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_CONSTANT, value=0)
    plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
    plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
    plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
    plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
    plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
    plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    数值计算
    img_cat = cv2.imread('img/cat.jpg')
    img_cat2 = img_cat + 10 
    print(img_cat[:5,:,0])
    print(img_cat2[:5,:,0])
    ""
    [[142 146 151 ... 156 155 154]
     [108 112 118 ... 155 154 153]
     [108 110 118 ... 156 155 154]
     [139 141 148 ... 156 155 154]
     [153 156 163 ... 160 159 158]]
    [[152 156 161 ... 166 165 164]
     [118 122 128 ... 165 164 163]
     [118 120 128 ... 166 165 164]
     [149 151 158 ... 166 165 164]
     [163 166 173 ... 170 169 168]]
    ""
    
    print((img_cat + img_cat2)[:5,:,0]) # 相当于% 256
    print(cv2.add(img_cat, img_cat2)[:5, :, 0]) # 超过的赋值为最大值
    ""
    [[ 38  46  56 ...  66  64  62]
     [226 234 246 ...  64  62  60]
     [226 230 246 ...  66  64  62]
     [ 32  36  50 ...  66  64  62]
     [ 60  66  80 ...  74  72  70]]
    [[255 255 255 ... 255 255 255]
     [226 234 246 ... 255 255 255]
     [226 230 246 ... 255 255 255]
     [255 255 255 ... 255 255 255]
     [255 255 255 ... 255 255 255]]
    ""
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    图像融合
    img_cat = cv2.imread('img/cat.jpg')
    img_dog = cv2.imread('img/dog.jpg')
    img_cat + img_dog
    ""
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-30-e16b258924d3> in <module>
          1 img_cat = cv2.imread('img/cat.jpg')
          2 img_dog = cv2.imread('img/dog.jpg')
    ----> 3 img_cat + img_dog
    
    ValueError: operands could not be broadcast together with shapes (414,500,3) (429,499,3) 
    ""
    
    img_dog = cv2.resize(img_dog, (500, 414))
    print(img_dog.shape)  # (414, 500, 3)
    res = cv2.addWeighted(img_cat, 0.4, img_dog, 0.6, 0)
    plt.imshow(res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    res = cv2.resize(img_cat, (0, 0), fx=4, fy=4)
    plt.imshow(res)
    
    • 1
    • 2

    在这里插入图片描述

    res = cv2.resize(img, (0, 0), fx=1, fy=3)
    plt.imshow(res)
    
    • 1
    • 2

    在这里插入图片描述

    HSV
    • H - 色调(主波长)
    • S - 饱和度(纯度/颜色的阴影)
    • V值(强度)
    # gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 灰度图
    hsv = cv2.cvtColor(img_cat, cv2.COLOR_BGR2HSV)
    plt.imshow(hsv)
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    图像阈值

    ret, dst = cv2.threshold(src, thresh, maxval, type)

    • src: 输入图,只能输入单通道图像,通常来说为灰度图
    • dst: 输出图
    • thresh: 阈值
    • maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
    • type:二值化操作的类型,包含以下5种类型:
      • cv2.THRESH_BINARY: 超过阈值部分取maxval(最大值),否则取0
      • cv2.THRESH_BINARY_INV: THRESH_BINARY的反转
      • cv2.THRESH_TRUNC: 大于阈值部分设为阈值,否则不变
      • cv2.THRESH_TOZERO: 大于阈值部分不改变,否则设为0
      • cv2.THRESH_TOZERO_INV: THRESH_TOZERO的反转
    img_gray = cv2.cvtColor(img_cat, cv2.COLOR_BGR2GRAY)
    ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
    ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
    ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
    ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV)
    
    titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
    images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
    
    for i in range(6):
        plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
        plt.title(titles[i])
    #     plt.xticks([]), plt.yticks([])
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    图像平滑
    img = cv2.imread('img/lenaNoise.png')
    cv_show('img', img)
    
    # 均值滤波:简单的卷积操作
    blur =  cv2.blur(img, (3, 3))
    cv_show('blur', blur)
    
    # 方框滤波: 基本和均值一样,可以选择归一化
    box = cv2.boxFilter(img, -1, (3, 3), normalize=True)
    cv_show('box', box)
    
    # 高斯滤波:高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的
    aussian = cv2.GaussianBlur(img, (5, 5), 1)
    cv_show('aussian', aussian)
    
    # 中值滤波:相当于用中值代替
    median = cv2.medianBlur(img, 5)
    cv_show('median', median)
    
    # 展示所有的
    res = np.hstack((blur, aussian, median))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    形态学-腐蚀操作
    img = cv2.imread('img/dige.png')
    cv_show('img', img)
    kernal = np.ones((3, 3), np.uint8)
    erosion = cv2.erode(img, kernal, iterations=1)
    cv_show('erosion', erosion)
    
    pie = cv2.imread('img/pie.png')
    cv_show('pie', pie)
    kernel = np.ones((30, 30),np.uint8) 
    erosion_1 = cv2.erode(pie, kernel, iterations = 1)
    erosion_2 = cv2.erode(pie, kernel, iterations = 2)
    erosion_3 = cv2.erode(pie, kernel, iterations = 3)
    res = np.hstack((erosion_1 erosion_2, erosion_3))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    形态学-膨胀操作
    img = cv2.imread('img/dige.png')
    cv_show('img', img)
    kernel = np.ones((3, 3), np.uint8) 
    dige_erosion = cv2.erode(img, kernel, iterations = 1)
    dige_dilate = cv2.dilate(dige_erosion, kernel, iterations = 1) # 腐蚀膨胀后不变
    cv_show('dige_dilate', dige_dilate)
    
    pie = cv2.imread('img/pie.png')
    cv_show('pie', pie)
    kernel = np.ones((30,30),np.uint8) 
    dilate_1  = cv2.dilate(pie, kernel, iterations = 1)
    dilate_2  = cv2.dilate(pie, kernel, iterations = 2)
    dilate_3  = cv2.dilate(pie, kernel, iterations = 3)
    res = np.hstack((dilate_1, dilate_2, dilate_3))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    开运算与闭运算
    img = cv2.imread('img/dige.png')
    kernel = np.ones((5, 5), np.uint8) 
    # 开:先腐蚀,再膨胀
    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    # 闭:先膨胀,再腐蚀
    closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    res = np.hstack((img, opening, closing))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    梯度运算
    # 梯度 = 膨胀 - 腐蚀
    pie = cv2.imread('img/pie.png')
    kernel = np.ones((7, 7), np.uint8) 
    dilate = cv2.dilate(pie, kernel, iterations = 5)
    erosion = cv2.erode(pie, kernel, iterations = 5)
    gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
    res = np.hstack((pie, dilate, erosion, gradient))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    礼帽与黑帽
    • 礼帽 = 原始输入- 开运算结果
    • 黑帽 = 闭运算 - 原始输入
    img = cv2.imread('img/dige.png')
    tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
    blackhat  = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
    res = np.hstack((img, tophat, blackhat))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    图像梯度-Sobel算子

    dst = cv2.Sobel(src, ddepth, dx, dy, ksize)

    • ddepth:图像的深度
    • dx和dy分别表示水平和竖直方向
    • ksize是Sobel算子的大小
    img = cv2.imread('img/pie.png',cv2.IMREAD_GRAYSCALE)
    sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
    res = np.hstack((img, sobelx))
    cv_show('res', res)
    # 白到黑是正数,黑到白就是负数了,所有的负数会被截断成0,所以要取绝对值
    
    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)
    
    # 分别计算x和y,再求和
    sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
    cv_show('sobelxy',sobelxy)
    
    # 不建议直接计算
    sobelxy=cv2.Sobel(img, cv2.CV_64F, 1, 1, ksize=3)
    sobelxy = cv2.convertScaleAbs(sobelxy) 
    cv_show('sobelxy',sobelxy)
    
    img = cv2.imread('img/lena.jpg', cv2.IMREAD_GRAYSCALE)
    sobelx = cv2.Sobel(img, cv2.CV_64F ,1 ,0, ksize=3)
    sobelx = cv2.convertScaleAbs(sobelx)
    sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
    sobely = cv2.convertScaleAbs(sobely)
    sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
    sobelxy1 = cv2.Sobel(img, cv2.CV_64F, 1, 1, ksize=3) 
    sobelxy1 = cv2.convertScaleAbs(sobelxy1)
    res = np.hstack((sobelx, sobely, sobelxy, sobelxy1))
    cv_show('res', res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    图像梯度-Scharr算子/laplacian算子
    #不同算子的差异
    img = cv2.imread('img/lena.jpg', cv2.IMREAD_GRAYSCALE)
    sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
    sobely = cv2.Sobel(img, cv2.CV_64F ,0, 1, ksize=3)
    sobelx = cv2.convertScaleAbs(sobelx)   
    sobely = cv2.convertScaleAbs(sobely)  
    sobelxy =  cv2.addWeighted(sobelx,0.5,sobely,0.5,0)  
    
    scharrx = cv2.Scharr(img,cv2.CV_64F, 1, 0)
    scharry = cv2.Scharr(img,cv2.CV_64F, 0, 1)
    scharrx = cv2.convertScaleAbs(scharrx)   
    scharry = cv2.convertScaleAbs(scharry)  
    scharrxy =  cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0) 
    
    laplacian = cv2.Laplacian(img, cv2.CV_64F)
    laplacian = cv2.convertScaleAbs(laplacian)   
    
    res = np.hstack((sobelxy, scharrxy, laplacian))
    cv_show('res',res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    Canny边缘检测
    1. 使用高斯滤波器,以平滑图像,滤除噪声。
    2. 计算图像中每个像素点的梯度强度和方向。
    3. 应用非极大值(Non-Maximum Suppression)抑制,以消除边缘检测带来的杂散响应。
    4. 应用双阈值(Double-Threshold)检测来确定真实的和潜在的边缘。
    5. 通过抑制孤立的弱边缘最终完成边缘检测。
    img = cv2.imread("img/lena.jpg", cv2.IMREAD_GRAYSCALE)
    v1 = cv2.Canny(img, 80, 150)
    v2 = cv2.Canny(img, 50, 100)
    res = np.hstack((v1, v2))
    cv_show('res',res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    图像金字塔
    • 高斯金字塔: 下采样
    • 拉普拉斯金字塔:上采样
    img= cv2.imread("img/AM.png")
    up = cv2.pyrUp(img)
    down = cv2.pyrDown(img)
    print(img.shape, up.shape, down.shape)  # (442, 340, 3) (884, 680, 3) (221, 170, 3)
    cv_show('img', img)
    cv_show('up', up)
    cv_show('down', down)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    图像轮廓

    cv2.findContours(img, mode, method)

    mode:轮廓检索模式

    • RETR_EXTERNAL :只检索最外面的轮廓;
    • RETR_LIST:检索所有的轮廓,并将其保存到一条链表当中;
    • RETR_CCOMP:检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;
    • RETR_TREE:检索所有的轮廓,并重构嵌套轮廓的整个层次;

    method:轮廓逼近方法

    • CHAIN_APPROX_NONE:以Freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。
    • CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。

    为了更高的准确率,使用二值图像。

    img = cv2.imread('img/contours.png')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    cv_show('thresh',thresh)
    
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    
    
    #传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
    draw_img = img.copy()
    v1 = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)
    v2 = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)
    res = np.hstack((v1, v2))
    cv_show('res', res)
    
    # 轮廓特征
    cnt = contours[0]
    # 面积
    print(cv2.contourArea(cnt))  # 8500.5
    #周长,True表示闭合的
    print(cv2.arcLength(cnt, True))  # 437.9482651948929
    
    # 轮廓近似
    res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 2)
    cv_show('res', res)
    
    epsilon = 0.15 * cv2.arcLength(cnt, True) 
    approx = cv2.approxPolyDP(cnt, epsilon ,True)
    res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
    cv_show('res', res)
    
    # 边界矩形
    x, y, w, h = cv2.boundingRect(cnt)
    img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv_show('img', img)
    
    area = cv2.contourArea(cnt)
    x, y, w, h = cv2.boundingRect(cnt)
    rect_area = w * h
    extent = float(area) / rect_area
    print ('轮廓面积与边界矩形比', extent)  # 轮廓面积与边界矩形比 0.5154317244724715
    
    # 外接圆
    (x, y), radius = cv2.minEnclosingCircle(cnt) 
    center = (int(x), int(y)) 
    radius = int(radius) 
    img = cv2.circle(img, center, radius, (0, 255, 0), 2)
    cv_show('img', img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    傅里叶变换
    • 高频:变化剧烈的灰度分量,例如边界
    • 低频:变化缓慢的灰度分量,例如一片大海

    滤波

    • 低通滤波器:只保留低频,会使得图像模糊
    • 高通滤波器:只保留高频,会使得图像细节增强

    注意:

    • opencv中主要就是cv2.dft()和cv2.idft(),输入图像需要先转换成np.float32格式。
    • 得到的结果中频率为0的部分会在左上角,通常要转换到中心位置,可以通过shift变换来实现。
    • cv2.dft()返回的结果是双通道的(实部,虚部),通常还需要转换成图像格式才能展示(0, 255)。
    img = cv2.imread('img/lena.jpg', 0)
    img_float32 = np.float32(img)
    print(img_float32.shape)  # (263, 263)
    
    dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
    # print(dft)
    dft_shift = np.fft.fftshift(dft)
    # print(dft_shift)
    
    rows, cols = img.shape
    crow, ccol = int(rows/2) , int(cols/2)     # 中心位置
    
    # 低通滤波
    mask = np.zeros((rows, cols, 2), np.uint8)
    print(mask.shape)  # (263, 263, 2)
    mask[crow-30:crow+30, ccol-30:ccol+30] = 1
    # print(mask)
    
    # IDFT
    fshift = dft_shift * mask
    f_ishift = np.fft.ifftshift(fshift)
    img_back = cv2.idft(f_ishift)
    img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])
    
    plt.subplot(121),plt.imshow(img, cmap = 'gray')
    plt.title('Input Image'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
    plt.title('Result'), plt.xticks([]), plt.yticks([])
    plt.show()                
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    
    rows, cols = img.shape
    crow, ccol = int(rows/2) , int(cols/2)     # 中心位置
    
    # 高通滤波
    mask = np.ones((rows, cols, 2), np.uint8)
    mask[crow-30:crow+30, ccol-30:ccol+30] = 0
    
    # IDFT
    fshift = dft_shift * mask
    f_ishift = np.fft.ifftshift(fshift)
    img_back = cv2.idft(f_ishift)
    img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
    
    plt.subplot(121),plt.imshow(img, cmap = 'gray')
    plt.title('Input Image'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
    plt.title('Result'), plt.xticks([]), plt.yticks([])
    plt.show()    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    1.2 视频处理

    vc = cv2.VideoCapture('img/test.mp4')
    if vc.isOpened():  # 检查是否正确打开
        open, frame = vc.read()
    else:
        open = False
    while open:
        ret, frame = vc.read()
        if frame is None:
            break
        if ret == True:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            cv2.imshow('res', gray)
            if cv2.waitKey(10) & 0xFF == 27:
                break
    vc.release()
    cv2.destroyAllWindows()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2. 直方图与模板匹配

    import cv2  # opencv读取的格式是BGR
    import numpy as np
    import matplotlib.pyplot as plt  # Matplotlib是RGB
    %matplotlib inline 
    
    def cv_show(img, name):
        cv2.imshow(name,img)
        cv2.waitKey()
        cv2.destroyAllWindows()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    直方图

    cv2.calcHist(images, channels, mask, histSize, ranges)

    • images: 原图像图像格式为 uint8 或 float32。当传入函数时应 用中括号 [] 括来例如[img]
    • channels: 同样用中括号括来它会告函数我们统幅图像的直方图。如果入图像是灰度图它的值就是 [0]如果是彩色图像的传入的参数可以是 [0][1][2] 它们分别对应着 BGR。
    • mask: 掩模图像。统整幅图像的直方图就把它为None。但是如果你想统图像某一分的直方图的你就制作一个掩模图像并使用它。
    • histSize:BIN 的数目。也应用中括号括来
    • ranges: 像素值范围常为 [0-256]
    img = cv2.imread('img/cat.jpg', 0) # 0表示灰度图
    hist = cv2.calcHist([img], [0], None, [256], [0, 256])
    print(hist.shape)  # (256, 1)
    plt.hist(img.ravel(), 256); 
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    img = cv2.imread('img/cat.jpg')
    color = ('b','g','r')
    for i, col in enumerate(color): 
        histr = cv2.calcHist([img], [i], None, [256], [0, 256]) 
        plt.plot(histr, color = col) 
        plt.xlim([0, 256]) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    # mask操作
    mask = np.zeros(img.shape[:2], np.uint8)
    print (mask.shape)  # (414, 500)
    mask[100:300, 100:400] = 255
    cv_show(mask, 'mask')
    
    img = cv2.imread('img/cat.jpg', 0)
    masked_img = cv2.bitwise_and(img, img, mask=mask) #与操作
    cv_show(masked_img, 'masked_img')
    
    hist_full = cv2.calcHist([img], [0], None, [256], [0, 256])
    hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 256])
    plt.subplot(221), plt.imshow(img, 'gray')
    plt.subplot(222), plt.imshow(mask, 'gray')
    plt.subplot(223), plt.imshow(masked_img, 'gray')
    plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
    plt.xlim([0, 256])
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    直方图均衡图
    img = cv2.imread('img/clahe.jpg', 0)
    plt.hist(img.ravel(), 256)
    plt.show()
    equ = cv2.equalizeHist(img) 
    plt.hist(equ.ravel(), 256)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    自适应直方图均衡化
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) 
    res_clahe = clahe.apply(img)
    res = np.hstack((img, equ, res_clahe))
    cv_show(res,'res')
    
    • 1
    • 2
    • 3
    • 4

    3. 信用卡数字识别

    import numpy as np
    import cv2
    import argparse
    
    # 设置参数
    ap = argparse.ArgumentParser()
    ap.add_argument('-i', '--image', required=True, default='img/credit_card_01.png', help='path to input image')
    ap.add_argument('-t', '--template', required=True, default='img/ocr_a_reference.png', help='path to template OCR-A image')
    args = vars(ap.parse_args())
    
    # 绘图展示
    def cv_show(name, img):
        cv2.imshow(name, img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    img = cv2.imread('img/credit_card_01.png')
    cv_show('img', img)
    # 灰度图
    ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv_show('ref', ref)
    # 二值图像
    ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
    cv_show('ref', ref)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    计算轮廓
    • cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图)
    • cv2.RETR_EXTERNAL只检测外轮廓
    • cv2.CHAIN_APPROX_SIMPLE只保留终点坐标

    返回的list中每个元素都是图像中的一个轮廓

    refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(img, refCnts, -1, (0, 0, 255), 3)
    cv_show('img', img)
    print(np.array(refCnts).shape)
    
    refCnts
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    2022大厂高频面试题之Vue篇
    在线购物系统(JSP+java+springmvc+mysql+MyBatis)
    大前端CPU优化技术--NEON自动向量化
    竞赛选题 身份证识别系统 - 图像识别 深度学习
    Kotlin基础——DSL
    借助 ChatGPT 编写的 libbpf eBPF 工具开发实践教程: 通过例子学习 eBPF
    在VScode中使用Jupyter Notebook的一些技巧
    前端使用 Konva 实现可视化设计器(13)- 折线 - 最优路径应用【思路篇】
    CP Autosar-ETH Driver配置
    【监督学习】套索回归与岭回归(含代码)
  • 原文地址:https://blog.csdn.net/keiven_/article/details/126608272