zoukankan      html  css  js  c++  java
  • 4-10 边缘检测2

    图片卷积和矩阵运算不是一回事。矩阵是行列式相乘。

    import cv2
    import numpy as np
    import random
    import math
    img = cv2.imread('image2.jpg',1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    cv2.imshow('src',img)
    # sobel 1 算子模板 2 图片卷积 3 阈值判决
    # [1 2 1           [ 1 0 -1 
    #  0 0 0             2 0 -2
    # -1 -2 -1 ]        1 0 -1 ]
    
    # [1 2 3 4] [a b c d] a*1+b*2+c*3+d*4 = dst
    # sqrt(a*a+b*b) = f>th(判决明显) 如果f>th,我们就认为是边缘;如果f<th,我们就认为是非边缘.
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    dst = np.zeros((height,width,1),np.uint8)
    for i in range(0,height-2):
        for j in range(0,width-2):
            gy = gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]*1-gray[i+2,j+1]*2-gray[i+2,j+2]*1
            gx = gray[i,j]+gray[i+1,j]*2+gray[i+2,j]-gray[i,j+2]-gray[i+1,j+2]*2-gray[i+2,j+2]
            grad = math.sqrt(gx*gx+gy*gy)
            if grad>50: #梯度>阈值
                dst[i,j] = 255
            else:
                dst[i,j] = 0
    cv2.imshow('dst',dst)
    cv2.waitKey(0)

    import cv2
    import numpy as np
    import random
    import math
    img = cv2.imread('image1.jpg',1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    cv2.imshow('src',img)
    # sobel 1 算子模板 2 图片卷积 3 阈值判决
    # [1 2 1           [ 1 0 -1 
    #  0 0 0             2 0 -2
    # -1 -2 -1 ]        1 0 -1 ]
    
    # [1 2 3 4] [a b c d] a*1+b*2+c*3+d*4 = dst
    # sqrt(a*a+b*b) = f>th(判决明显) 如果f>th,我们就认为是边缘;如果f<th,我们就认为是非边缘.
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    dst = np.zeros((height,width,1),np.uint8)
    for i in range(0,height-2):
        for j in range(0,width-2):
            gy = gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]*1-gray[i+2,j+1]*2-gray[i+2,j+2]*1
            gx = gray[i,j]+gray[i+1,j]*2+gray[i+2,j]-gray[i,j+2]-gray[i+1,j+2]*2-gray[i+2,j+2]
            grad = math.sqrt(gx*gx+gy*gy)
            if grad>50: #梯度>阈值
                dst[i,j] = 255
            else:
                dst[i,j] = 0
    cv2.imshow('dst',dst)
    cv2.waitKey(0)

    import cv2
    import numpy as np
    import random
    import math
    img = cv2.imread('image0.jpg',1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    cv2.imshow('src',img)
    # sobel 1 算子模板 2 图片卷积 3 阈值判决
    # [1 2 1           [ 1 0 -1 
    #  0 0 0             2 0 -2
    # -1 -2 -1 ]        1 0 -1 ]
    
    # [1 2 3 4] [a b c d] a*1+b*2+c*3+d*4 = dst
    # sqrt(a*a+b*b) = f>th(判决明显) 如果f>th,我们就认为是边缘;如果f<th,我们就认为是非边缘.
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    dst = np.zeros((height,width,1),np.uint8)
    for i in range(0,height-2):
        for j in range(0,width-2):
            gy = gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]*1-gray[i+2,j+1]*2-gray[i+2,j+2]*1
            gx = gray[i,j]+gray[i+1,j]*2+gray[i+2,j]-gray[i,j+2]-gray[i+1,j+2]*2-gray[i+2,j+2]
            grad = math.sqrt(gx*gx+gy*gy)
            if grad>50: #梯度>阈值
                dst[i,j] = 255
            else:
                dst[i,j] = 0
    cv2.imshow('dst',dst)
    cv2.waitKey(0)

    import cv2
    import numpy as np
    import random
    import math
    img = cv2.imread('image3.png',1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    cv2.imshow('src',img)
    # sobel 1 算子模板 2 图片卷积 3 阈值判决
    # [1 2 1           [ 1 0 -1 
    #  0 0 0             2 0 -2
    # -1 -2 -1 ]        1 0 -1 ]
    
    # [1 2 3 4] [a b c d] a*1+b*2+c*3+d*4 = dst
    # sqrt(a*a+b*b) = f>th(判决明显) 如果f>th,我们就认为是边缘;如果f<th,我们就认为是非边缘.
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    dst = np.zeros((height,width,1),np.uint8)
    for i in range(0,height-2):
        for j in range(0,width-2):
            gy = gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]*1-gray[i+2,j+1]*2-gray[i+2,j+2]*1
            gx = gray[i,j]+gray[i+1,j]*2+gray[i+2,j]-gray[i,j+2]-gray[i+1,j+2]*2-gray[i+2,j+2]
            grad = math.sqrt(gx*gx+gy*gy)
            if grad>50: #梯度>阈值
                dst[i,j] = 255
            else:
                dst[i,j] = 0
    cv2.imshow('dst',dst)
    cv2.waitKey(0)
  • 相关阅读:
    如何锻炼出最牛程序员的编码套路
    如果仔细观察他们,你会发现他们时时都在锻炼
    单纯地每天埋头于工作并不能算是真正意义上的锻炼
    把全世界的人们都联系在一起,提升人们的社交参与度
    HTML5十五大新特性
    html5的八大特性
    【贪心】【二维偏序】【权值分块】bzoj1691 [Usaco2007 Dec]挑剔的美食家
    【分块】【链表】bzoj2738 矩阵乘法
    【分块】bzoj3343 教主的魔法
    【线段树】bzoj3747 [POI2015]Kinoman
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/9699526.html
Copyright © 2011-2022 走看看