# Edge detection: two methods

• Laplace algorithm gives edge
import cv2
import numpy
from scipy import ndimage
def strokeEdges(src,dst,blurksize,edgeKsize):
src=numpy.array(src)
yuansrc=src
dst=numpy.array(dst)
if blurksize>=3:
blurredSrc=cv2.medianBlur(src,blurksize)
graySrc=cv2.cvtColor(blurredSrc,cv2.COLOR_BGR2GRAY)
else:
graySrc=cv2.cvtColor(blurksize,cv2.COLOR_BGR2GRAY)
##以上操作是对图片模糊化然后边缘灰化 是否模糊化的标准就是blurksize是否小于3
##
cv2.Laplacian(graySrc,cv2.CV_8U,graySrc,edgeKsize)
##拉普拉斯算法 第一个参数是原图 第二个参数是深度 也就是和图片形式有关例如 RGB分别由8位
##那么深度就是2的24次方 这里的cv2.CV_8U具体看下面
##第三个参数dest是结果的图片 Ksize是核的大小 为奇数 （并不是数字越大或者越小就越好 这个好不好原因还未知）
#此时这里的边缘检测已经是完成了
normalizedInverseAlpah=(1.0/255)*(255-graySrc)
channels=cv2.split(src)
#通道拆分
for channel in channels:
channel[:]=channel*normalizedInverseAlpah
cv2.merge(channels,dst)
cv2.imshow('dst',dst)
cv2.imshow('graySrc',graySrc)
cv2.imshow('shouw',yuansrc)
cv2.waitKey()
cv2.destroyAllWindows()
dst=src
strokeEdges(src,dst,7,5)


as for the above code there are two points which you won’t find if you don’t look at it the first question is why do we use numpy.array to 2d the second question is why do we change the channels when there is no change in the for loop but then the channels change at the end
Question the answer is in the loop and strokeEdges normalizedInverseAlpah = (1.0/255) of the * (255 – graySrc) this operation numpy array for the concrete operation of operation rules will give the answer in the code snippet below
problem two answers will give the answer in the code snippet below if I go back to review or future readers see the trouble knock code of your answer to see what problem we have appeared in

import numpy
gray=[2]
channels=[[1,2,3],[1,2,3],[1,2,3]]
for channel in channels:
channel[:]=channel*2
print(channels)
print('____________________________')
channels=[[1,2,3],[1,2,3],[1,2,3]]
channels=numpy.array(channels)
for channel in channels:
channel[:]=channel*[1,2,3]
print(channels)
print('____________________________')
channels=[[1,2,3],[1,2,3],[1,2,3]]
channels=numpy.array(channels)
for channel in channels:
channel[:]=channel*2
print(channels)

• Canny algorithm obtained the edge
import cv2
cv2.imshow('canny',cv2.Canny(img,0,100))
cv2.waitKey()
cv2.destroyAllWindows()


canny function itself and not for image noise reduction operation so if you want to use canny must advance using low channel filtering denoising
cv2, canny is the first parameter to the natural images in the second and the third parameter is respectively on the threshold and the threshold is simple to understand is the edge of the small number will appear more but the same risk is likely to take noise as edges or appeared on the edge of the following is not big number will edge if too less will appear the edges as not