卷积滤波器与边缘检测

高通滤波器

Sobel滤波器

Sobel滤波器非常常用于边缘检测和在图像中查找强度模式

高斯模糊

Import resources and display image
```import numpy as np
import matplotlib.pyplot as plt
import cv2
%matplotlib inline
# Make a copy of the image
image_copy = np.copy(image)
# Change color to RGB (from BGR)
image_copy = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)
plt.imshow(image_copy)
#### 怎么确定滤波器低通或者高通```
```# Convert to grayscale for filtering
gray = cv2.cvtColor(image_copy, cv2.COLOR_RGB2GRAY)
# Create a Gaussian blurred image
gray_blur = cv2.GaussianBlur(gray, (9, 9), 0) ### 0 表示标准差为0
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
ax1.set_title('original gray')
ax1.imshow(gray, cmap='gray')
ax2.set_title('blurred image')
ax2.imshow(gray_blur, cmap='gray')```
Test performance with a high-pass filter
```# High-pass filter

# 3x3 sobel filters for edge detection
sobel_x = np.array([[ -1, 0, 1],
[ -2, 0, 2],
[ -1, 0, 1]])

sobel_y = np.array([[ -1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])

# Filter the orginal and blurred grayscale images using filter2D
filtered = cv2.filter2D(gray, -1, sobel_x)

filtered_blurred = cv2.filter2D(gray_blur, -1, sobel_x)
filtered_blurred_y = cv2.filter2D(gray_blur, -1, sobel_y)
f, (ax1, ax2,ax3) = plt.subplots(1, 3, figsize=(20,10))

ax1.set_title('original gray')
ax1.imshow(filtered, cmap='gray')

ax2.set_title('blurred image_x')
ax2.imshow(filtered_blurred, cmap='gray')

ax3.set_title('blurred image_y')
ax3.imshow(filtered_blurred_y, cmap='gray')```
```# Create threshold that sets all the filtered pixels to white
# Above a certain threshold

retval, binary_image = cv2.threshold(filtered_blurred, 50, 255, cv2.THRESH_BINARY)

plt.imshow(binary_image, cmap='gray')```
```import numpy as np
import matplotlib.pyplot as plt
import cv2

%matplotlib inline

# Define gaussian, sobel, and laplacian (edge) filters

gaussian = (1/9)*np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])

sobel_x= np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])

sobel_y= np.array([[-1,-2,-1],
[0, 0, 0],
[1, 2, 1]])

# laplacian, edge filter
laplacian=np.array([[0, 1, 0],
[1,-4, 1],
[0, 1, 0]])

filters = [gaussian, sobel_x, sobel_y, laplacian]
filter_name = ['gaussian','sobel_x', \
'sobel_y', 'laplacian']

# perform a fast fourier transform on each filter
# and create a scaled, frequency transform image
f_filters = [np.fft.fft2(x) for x in filters]
fshift = [np.fft.fftshift(y) for y in f_filters]   # 1、在matlab中，经过fft变换后，数据的频率范围是从[0,fs]排列的。
#2、而一般，我们在画图或者讨论的时候，是从[-fs/2,fs/2]的范围进行分析。
#3、因此，需要将经过fft变换后的图像的[fs/2,fs]部分移动到[-fs/2,0]这个范围内。
frequency_tx = [np.log(np.abs(z)+1) for z in fshift]

# display 4 filters
for i in range(len(filters)):
plt.subplot(2,2,i+1),plt.imshow(frequency_tx[i],cmap = 'gray')
plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])

plt.show()```

Canny 边缘检测器

1. 首先 检测器用高斯模糊过滤掉噪声

2.然后用 Sobel 过滤器确定图像边缘的强度和方向

3.接着 借助 Sobel 过滤器的输出,Canny 会用非极大抑制,来观察每个检测边缘的强度和方向,选出局部最大像素,从而把最强的边缘绘制成连续的、一个像素宽的细线

4.最后 用滞后阀值来分离最佳边缘,滞后阀值是双阀值化操作,以某图一像素宽的横切面为例:这里的曲线代表边缘强度,峰值指的是十分强的边缘,使用滞后阀值时我们要确定一个高阀值以便允许这些强边缘通过;再设置一个低阀值,何低于该阀值的边缘即为弱边缘会被舍弃,但位于高低阀值之间的边缘,只有当其与另一个强边缘相连时 才会得到保留.

例子 canny

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

%matplotlib inline

# Change color to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.imshow(image)```
将图片转为灰度图片
```# Convert the image to grayscale for processing
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

plt.imshow(gray, cmap='gray')```

实现canny边缘检测

```# Try Canny using "wide" and "tight" thresholds

wide = cv2.Canny(gray, 30, 100)#该函数需要输入的参数有 灰度图像以及刚才定义的阀值上下限

tight = cv2.Canny(gray, 200, 240)

# Display the images
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))

ax1.set_title('wide')
ax1.imshow(wide, cmap='gray')

ax2.set_title('tight')
ax2.imshow(tight, cmap='gray')```
```# Read in the image

# Change color to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

## TODO: Define lower and upper thresholds for hysteresis
# right now the threshold is so small and low that it will pick up a lot of noise
lower = 100
upper =200

edges = cv2.Canny(gray, lower, upper)

plt.figure(figsize=(20,10))
plt.imshow(edges, cmap='gray')```

霍夫变换

k=∞是不方便表示的，而且q怎么取值呢，这样不是办法。因此考虑将笛卡尔坐标系换为：极坐标表示。

ρ表示原点到直线的距离 θ 表示的是直线与横轴所成的角 在极坐标系下，其实是一样的：极坐标的点→霍夫空间的直线，只不过霍夫空间不再是[k,q]的参数，而是[ρ,θ ]的参数，给出对比图：

霍夫线检测

```# Import resources and display the image
import numpy as np
import matplotlib.pyplot as plt
import cv2

%matplotlib inline

# Change color to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.imshow(image)```
```# Perform edge detection
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Define our parameters for Canny
low_threshold = 50
high_threshold = 100
edges = cv2.Canny(gray, low_threshold, high_threshold)

plt.imshow(edges, cmap='gray')```
```#Find lines using a Hough transform
# Define the Hough transform parameters
# Make a blank the same size as our image to draw on
rho = 1
theta = np.pi/180 #其中 ρ 和 θ 就是我们的霍夫变量了，两者定义了检测的分辨率，我把它们分别设为 1 个像素和 1 度
threshold = 60 #然后是直线检测的最低阀值，这个阀值就是霍夫空间要找到一根直线所需的最少相交数
min_line_length = 100 #最小线条长度
max_line_gap = 5 # 线条分段之间的距离

line_image = np.copy(image) #creating an image copy to draw lines on

# Run Hough on the edge-detected image
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
#这个函数会返回其检测到的所有霍夫直线，每根直线实际上是一个包含着四个点的数组，四个点即 x1、y1 和 x2、y2，也就是每根直线两端的终点。

# Iterate over the output "lines" and draw lines on the image copy
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)

plt.imshow(line_image)```

霍夫圆检测

```import numpy as np
import matplotlib.pyplot as plt
import cv2
%matplotlib inline

# Change color to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)```
```# Gray and blur
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray_blur = cv2.GaussianBlur(gray, (3, 3), 0)
plt.imshow(gray_blur, cmap='gray')```

`HoughCircles`函数

• an input image, detection method (Hough gradient), resolution factor between the detection and image (1),
• minDist - the minimum distance between circles
• param1 - the higher value for performing Canny edge detection
• param2 - threshold for circle detection, a smaller value --> more circles will be detected
• min/max radius for detected circles
```# for drawing circles on
circles_im = np.copy(image)

## TODO: use HoughCircles to detect circles
# right now there are too many, large circles being detected
minDist=45,
param1=70,
param2=11,

# convert circles into expected type
circles = np.uint16(np.around(circles))
# draw each one
for i in circles[0,:]:
# draw the outer circle
cv2.circle(circles_im,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(circles_im,(i[0],i[1]),2,(0,0,255),3)

plt.imshow(circles_im)

print('Circles shape: ', circles.shape)```

Face detection using OpenCV

One older (from around 2001), but still popular scheme for face detection is a Haar cascade classifier; these classifiers in the OpenCV library and use feature-based classification cascades that learn to isolate and detect faces in an image. You can read the original paper proposing this approach here.

```# import required libraries for this section
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import cv2
# load in color image for face detection

# convert to RBG
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20,10))
plt.imshow(image)```
```# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

plt.figure(figsize=(20,10))
plt.imshow(gray, cmap='gray')```

Next we load in the fully trained architecture of the face detector, found in the file detector_architectures/ haarcascade_frontalface_default.xml,and use it on our image to find faces!

```# load in cascade classifier

# run the detector on the grayscale image

How many faces are detected is determined by the function, detectMultiScale which aims to detect faces of varying sizes. The inputs to this function are: (image, scaleFactor, minNeighbors); you will often detect more faces with a smaller scaleFactor, and lower value for minNeighbors, but raising these values often produces better matches. Modify these values depending on your input image.

```# print out the detections found
print ('We found ' + str(len(faces)) + ' faces in this image')
print ("Their coordinates and lengths/widths are as follows")
print ('=============================')
print (faces)```

The output of the classifier is an array of detections; coordinates that define the dimensions of a bounding box around each face. Note that this always outputs a bounding box that is square in dimension.

```img_with_detections = np.copy(image)   # make a copy of the original image to plot rectangle detections ontop of
##### Let's plot the corresponding detection boxes on our original image to see how well we've done.
# loop over our detections and draw their corresponding boxes on top of our original image
for (x,y,w,h) in faces:
# draw next detection as a red rectangle on top of the original image.
# Note: the fourth element (255,0,0) determines the color of the rectangle,
# and the final argument (here set to 5) determines the width of the drawn rectangle
cv2.rectangle(img_with_detections,(x,y),(x+w,y+h),(255,0,0),5)

# display the result
plt.figure(figsize=(20,10))
plt.imshow(img_with_detections)```

148 篇文章22 人订阅

0 条评论

相关文章

5627

1323

【专知荟萃25】文字识别OCR知识资料全集（入门/进阶/论文/综述/代码/专家，附查看）

OCR文字，车牌，验证码识别 专知荟萃 入门学习 论文及代码 文字识别 文字检测 验证码破解 手写体识别 车牌识别 实战项目 视频 入门学习 端到端的OCR...

2K8

2.2K5

9924

FPGA图像处理之rgbtogray算法的实现

FPGA图像处理之rgbtogray算法的实现 作者：lee神 1.背景知识 在正是入题之前先给大家讲解一下gray图像，YUV图像以及Ycbcr图像。 Gra...

4284

3336

48610

4668