opencv画像効果のエッジ検出2つ

import cv2
import numpy as np
import random
import math

img = cv2.imread("E:/code/conputer_visual/data/0.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]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dst = np.zeros((height, width, 1), np.uint8)
for i in range(height-2):
    for j in range(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]*1 + gray[i+1,j]*2 + gray[i+2,j]*1 + gray[i+2,j]*(-1) + gray[i+2,j+1]*(-2) + gray[i+2,j+2]*(-1) 
        grad = math.sqrt(gx*gx + gy*gy)
        if grad>50: #梯度大于50则认为是边缘点,将其像素设置为白色
            dst[i,j] = 255
        else:   #否则认为该像素点不是边缘点,设置为黑色
            dst[i,j] = 0
cv2.imshow("dst", dst)
cv2.waitKey()

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/cyj5201314/article/details/114653903