边缘检测(各类边缘算子)以及角点检测 python

边缘检测:
1.Roberts算子
2.Prewitt算子
3.Sobel算子
4.Laplacian算子
5.Canny函数
代码如下:

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('lenna.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('origin', image)

h, w = image.shape   # 获取图像的高度和宽度


# Roberts算子
kernelx = np.array([[-1, 0], [0, 1]], dtype=int)
kernely = np.array([[0, -1], [1, 0]], dtype=int)
x = cv2.filter2D(image, cv2.CV_16S, kernelx)
y = cv2.filter2D(image, cv2.CV_16S, kernely)

absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
cv2.imshow('Roberts', Roberts )

# Prewitt算子
kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]], dtype=int)
kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=int)
x = cv2.filter2D(image, cv2.CV_16S, kernelx)
y = cv2.filter2D(image, cv2.CV_16S, kernely)

absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Prewitt = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
cv2.imshow('Prewitt', Prewitt)

# Sobel 滤波器 进行边的检测
sobel_horizontal = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)  # 水平方向
sobel_vetical = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)  # 垂直方向
cv2.imshow('sobel_H', sobel_horizontal)    # 水平方向
cv2.imshow('sobel_V', sobel_vetical)    # 垂直方向

# 拉普拉斯算子 进行边的检测    64F代表每一个像素点元素占64位浮点数
laplacian = cv2.Laplacian(image, cv2.CV_64F,  ksize=5)
cv2.imshow('laplacian', laplacian)

# # Canny边检测器
canny = cv2.Canny(image, 50, 240)
cv2.imshow('Canny', canny)

cv2.waitKey(0)
cv2.destroyAllWindows()

结果图如下
Roberts
边缘检测(各类边缘算子)以及角点检测 python_第1张图片
prewitt
边缘检测(各类边缘算子)以及角点检测 python_第2张图片
Laplacian
边缘检测(各类边缘算子)以及角点检测 python_第3张图片
canny
边缘检测(各类边缘算子)以及角点检测 python_第4张图片
Sobel_v
边缘检测(各类边缘算子)以及角点检测 python_第5张图片
sobel_h
边缘检测(各类边缘算子)以及角点检测 python_第6张图片
设计检测具有特定角度边缘的算子;
检测具有45º角的边缘的3×3算子;
检测具有45º角的边缘的5×5算子;
这里我使用的是prewitt模板
3x3代码如下:

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('lenna.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('origin', image)

# Roberts算子
kernelx = np.array([[1, 1, 0], [1, 0, -1], [0, -1, -1]], dtype=int)
kernely = np.array([[1, 1, 0], [1, 0, -1], [0, -1, -1]], dtype=int)
x = cv2.filter2D(image, cv2.CV_16S, kernelx)
y = cv2.filter2D(image, cv2.CV_16S, kernely)

absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Prewitt = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
cv2.imshow('Prewitt', Prewitt)

cv2.waitKey(0)
cv2.destroyAllWindows()

5x5代码如下

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('lenna.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('origin', image)

# Roberts算子
kernelx = np.array([[1, 1, 1, 1, 0], [1, 1, 1, 0, -1], [1,1,0, -1, -1],[1,0,-1,-1,-1],[0,-1,-1,-1,-1]], dtype=int)
kernely = np.array([[1, 1, 1, 1, 0], [1, 1, 1, 0, -1], [1,1,0, -1, -1],[1,0,-1,-1,-1],[0,-1,-1,-1,-1]], dtype=int)
x = cv2.filter2D(image, cv2.CV_16S, kernelx)
y = cv2.filter2D(image, cv2.CV_16S, kernely)

absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Prewitt = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
cv2.imshow('Prewitt', Prewitt)

cv2.waitKey(0)
cv2.destroyAllWindows()

角点检测
Harris

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def cornerHarri(imgae):
    gray = cv.cvtColor(imgae, cv.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv.cornerHarris(gray, 2, 3, 0.04)
    dst = cv.dilate(dst, None)
    imgae[dst>0.01*dst.max()]=[0,0,255]
    

src = cv.imread("whiteandblack.jpg")

rows, cols, channel = src.shape

#绕图像的中心旋转
#参数:旋转中心 旋转度数 scale
M = cv.getRotationMatrix2D((cols/2, rows/2), 30, 1)
#参数:原始图像 旋转参数 元素图像宽高
rotated = cv.warpAffine(src, M, (cols, rows))

cv.imshow("contours", src)
cornerHarri(src)
cv.imshow("corn",src)
cornerHarri(rotated)
cv.imshow("corn1",rotated)

cv.waitKey(0)

cv.destroyAllWindows()

这里是对原图及它旋转变换后的图像进行角点检测
图像如下
原图:
边缘检测(各类边缘算子)以及角点检测 python_第7张图片
角点检测后:
边缘检测(各类边缘算子)以及角点检测 python_第8张图片
旋转后的角点检测:
边缘检测(各类边缘算子)以及角点检测 python_第9张图片
使用cv2.goodFeaturesToTrack()检测角点
道理一样下面给出代码(结果图就不放了):

import numpy as np
import cv2
from matplotlib import pyplot as plt

def track(imgae):
    gray = cv2.cvtColor(rotated,cv2.COLOR_BGR2GRAY)
    corners = cv2.goodFeaturesToTrack(gray,25,0.01,10) 
    corners = np.int0(corners)
    for i in corners:  
      x,y = i.ravel()
      cv2.circle(rotated,(x,y),3,255,-1)
    
    
img = cv2.imread('whiteandblack.jpg')

rows, cols, channel = img.shape

#绕图像的中心旋转
#参数:旋转中心 旋转度数 scale
M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)
#参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(img, M, (cols, rows))


cv2.imshow("contours", img)
track(img)
cv2.imshow("corn",img)
track(rotated)
cv2.imshow("corn1",rotated)

cv.waitKey(0)

cv.destroyAllWindows()

你可能感兴趣的:(图像处理)