霍夫变换

基本概念

霍夫变换是一种特征提取技术,主要应用于检测图像中的直线或者圆。
OpenCV 中分为霍夫线变换和霍夫圆变换。

霍夫线变换

分类
  • 标准霍夫变换(SHT) cv2.HoughLines
  • 多尺度霍夫变换 (MSHT) cv2.HoughLines
  • 累计概率霍夫变换 (PPHT) cv2.HoughLinesP

注意:在使用霍夫线变换之前,首先要对图像进行边缘检测的处理,即霍夫线变换的直接输入只能是边缘二值图像

标准霍夫变换

使用极坐标来表示直线,对于在笛卡尔坐标上直线上所有给定的点,在极坐标上都能转换成正弦曲线,直线上所有点绘制出来的正弦曲线交与一点,若交于交点的曲线数量超过一定阈值,说明在笛卡尔坐标上表示一条直线。更多资料

HoughLines(image, rho, theta, threshold[, lines[, srn[, stn[, min_theta[, max_theta]]]]]) -> lines

  • image: 输入图像,需为 8 位的单通道二进制图像
  • rho: 以像素为单位的距离精度
  • theta: 以弧度为单位的角度精度
  • threshold: 阈值参数
  • srn: 默认值为 0 对于多尺度霍夫变换,srn 表示进步尺寸 rho 的除数距离,粗略的累加器进步尺寸直接是 rho ,而精确的累加器进步尺寸为 rho/srn。
  • stn: 默认值为 0 对于多尺度霍夫变换,stn 表示进步尺寸的单位角度 theta 的除数距离。若 srn stn 同时为 0,表示使用经典的霍夫变换,否则这两个参数都应该是正数。
  1. import cv2
  2. import numpy as np
  3. img = cv2.imread("./sample_img/HoughLines.jpg")
  4. edges = cv2.Canny(img, 50, 200, apertureSize=3)
  5. gray = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
  6. # 经典的霍夫变换
  7. lines = cv2.HoughLines(edges, 1, np.pi/180, 180, 0, 0) # 在图像中找到的所有直线都存储在这里
  8. for i in range(lines.shape[0]-1):
  9. rho, theta = lines[i][0][0], lines[i][0][1] # rho 为距离, theta 为角度,把每条直线的参数分离出来
  10. # 坐标转换
  11. a = np.cos(theta)
  12. b = np.sin(theta)
  13. x0 = a*rho
  14. y0 = b*rho
  15. x1 = int(x0 + 1000*(-b))
  16. y1 = int(y0 + 1000*(a))
  17. x2 = int(x0 - 1000*(-b))
  18. y2 = int(y0 - 1000*(a))
  19. # 绘制直线
  20. cv2.line(gray, (x1, y1), (x2, y2), (0, 0, 255), 2)
  21. cv2.imshow("img", img)
  22. cv2.imshow("gray", gray)
  23. cv2.imshow("edges", edges)
  24. cv2.waitKey(0)
  25. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图1

图像卷积与滤波的一些知识点)

注意,下面的坐标变换不是很清楚

  1. img = np.zeros((200, 200, 3), dtype=np.uint8)
  2. cv2.line(img, (0, 100), (100, 0), (0, 255, 0), 2)
  3. cv2.line(img, (0, 150), (150, 0), (0, 255, 0), 2)
  4. edges = cv2.Canny(img, 50, 200, apertureSize=3)
  5. gray = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
  6. # 经典的霍夫变换
  7. lines = cv2.HoughLines(edges, 1, np.pi/180, 100, 0, 0) # 在图像中找到的所有直线都存储在这里
  8. for i in range(lines.shape[0]-1):
  9. rho, theta = lines[i][0][0], lines[i][0][1] # rho 为距离, theta 为角度,把每条直线的参数分离出来
  10. # 坐标转换
  11. a = np.cos(theta)
  12. b = np.sin(theta)
  13. x0 = a*rho
  14. y0 = b*rho
  15. x1 = int(x0 + 1000*(-b))
  16. y1 = int(y0 + 1000*(a))
  17. x2 = int(x0 - 1000*(-b))
  18. y2 = int(y0 - 1000*(a))
  19. # 绘制直线
  20. cv2.line(gray, (x1, y1), (x2, y2), (0, 0, 255), 2)
  21. cv2.imshow("img", img)
  22. cv2.imshow("gray", gray)
  23. cv2.imshow("edges", edges)
  24. cv2.waitKey(0)
  25. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图2

累计概率霍夫变换

累计概率霍夫变换可以找出图像中直线大概的起始和终止坐标,返回 4 个元素,分别代表起始坐标(x1, y1),终止坐标(x2, y2)

HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]]) -> lines

  • image: 输入图像,需为 8 位单通道二进制图像
  • rho: 以像素为单位的距离精度,另一种表述方式是:直线搜索时的进步尺寸的单位半径
  • theta: 以弧度为单位的角度精度,另一种表述方式是:直线搜索时的进步尺寸的单位角度
  • threshold: 阈值参数
  • lines: 存储检测到的线条的输出矢量,返回 4 个元素,分别代表起始坐标(x1, y1),终止坐标(x2, y2)
  • minLineLength: 默认值 0 表示最低线段的长度,比这个设定参数短的线段不能显示出来
  • maxLineGap: 默认值 0 允许将同一行点与点之间连接起来的最大的距离。
  1. import cv2
  2. import numpy as np
  3. img = cv2.imread("./sample_img/HoughLines.jpg")
  4. edges = cv2.Canny(img, 50, 200, apertureSize=3) # 边缘检测出来就是二值图像
  5. gray = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
  6. threshold = 80
  7. minLineLength = 40
  8. maxLineGap = 10
  9. linesP = cv2.HoughLinesP(edges, 1, np.pi/180, threshold, minLineLength, maxLineGap)
  10. for i in range(linesP.shape[0]):
  11. x1 = linesP[i][0][0]
  12. y1 = linesP[i][0][1]
  13. x2 = linesP[i][0][2]
  14. y2 = linesP[i][0][3]
  15. cv2.line(gray, (x1, y1), (x2, y2), (0, 0, 255), 2)
  16. cv2.imshow("img", img)
  17. cv2.imshow("gray", gray)
  18. cv2.imshow("edges", edges)
  19. cv2.waitKey(0)
  20. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图3

  1. import cv2
  2. import numpy as np
  3. def nothing(x):
  4. pass
  5. img = cv2.imread("./sample_img/HoughLines.jpg")
  6. edges = cv2.Canny(img, 50, 200, apertureSize=3) # 边缘检测出来就是二值图像
  7. cv2.namedWindow("gray")
  8. cv2.createTrackbar("minLineLength", "gray", 0, 200, nothing)
  9. cv2.createTrackbar("maxLineGap", "gray", 0, 200, nothing)
  10. while(1):
  11. gray = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
  12. minLineLength = cv2.getTrackbarPos("minLineLength", "gray")
  13. maxLineGap = cv2.getTrackbarPos("maxLineGap", "gray")
  14. linesP = cv2.HoughLinesP(edges, 1, np.pi/180, 70, minLineLength, maxLineGap)
  15. for i in range(linesP.shape[0]):
  16. x1 = linesP[i][0][0]
  17. y1 = linesP[i][0][1]
  18. x2 = linesP[i][0][2]
  19. y2 = linesP[i][0][3]
  20. cv2.line(gray, (x1, y1), (x2, y2), (0, 0, 255), 2)
  21. k = cv2.waitKey(1) & 0xff
  22. if k == 27:
  23. break
  24. #cv2.imshow("img", img)
  25. cv2.imshow("gray", gray)
  26. #cv2.imshow("edges", edges)
  27. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图4

霍夫圆变换

原理

圆的表达式为 $(x-a)^{2} + (y-b)^{2} = r^{2}$ ,将圆上的任意点 (x, y) 变换成 (a, b, r) 坐标结果是一个圆锥,同一个圆上的点形成的圆锥会交于一点,从该交点可以得出圆的信息。

HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles

  • image:输入图像,需为 8 位单通道二进制图像
  • method: 使用的检测方法,目前只有 cv2.HOUGH_GRADIENT 霍夫梯度法一种
  • dp: 用来检测圆心的累加器图像的分辨率于输入图像之比的倒数,且此参数允许创建一个比输入图像分辨率低的累加器。例如,如果dp= 1时,累加器和输入图像具有相同的分辨率。如果dp=2,累加器便有输入图像一半那么大的宽度和高度。
  • minDist: 为霍夫变换检测到的圆的圆心之间的最小距离,即让我们的算法能明显区分的两个不同圆之间的最小距离。这个参数如果太小的话,多个相邻的圆可能被错误地检测成了一个重合的圆。反之,这个参数设置太大的话,某些圆就不能被检测出来了。
  • param1: 有默认值100。它是参数method设置的检测方法的对应的参数。对当前唯一的方法霍夫梯度法CV_HOUGH_GRADIENT,它表示传递给canny边缘检测算子的高阈值,而低阈值为高阈值的一半。
  • param2: 有默认值100。它是参数method设置的检测方法的对应的参数。对当前唯一的方法霍夫梯度法CV_HOUGH_GRADIENT,它表示在检测阶段圆心的累加器阈值。它越小的话,就可以检测到更多根本不存在的圆,而它越大的话,能通过检测的圆就更加接近完美的圆形了。
  • minRadius: int类型的minRadius,有默认值0,表示圆半径的最小值。
  • maxRadius: int类型的maxRadius,也有默认值0,表示圆半径的最大值。
  • circles: 返回的结果,包含三个元素 (x, y, radius),圆心和半径

图片例程
  1. import cv2
  2. import numpy as np
  3. """
  4. img = np.zeros((200, 200, 3), dtype=np.uint8)
  5. cv2.circle(img, (100, 100), 50, (0, 255, 255))
  6. cv2.circle(img, (95, 100), 50, (0, 255, 255))
  7. cv2.circle(img, (80, 100), 50, (0, 255, 255))
  8. """
  9. img = cv2.imread("./sample_img/opencv-logo.png")
  10. img1 = cv2.imread("./sample_img/opencv-logo.png", 0)
  11. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. gaussian = cv2.GaussianBlur(gray, (9, 9), 2, 2)
  13. circle = cv2.HoughCircles(gaussian, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=50, minRadius=0, maxRadius=0)
  14. for i in range(circle.shape[1]):
  15. center = (circle[0][i][0], circle[0][i][1])
  16. radius = circle[0][i][2]
  17. cv2.circle(img, center, radius, (100, 100, 100), 4)
  18. cv2.circle(img, center, 3, (100, 100, 100))
  19. cv2.imshow("img", img)
  20. cv2.imshow("img1", cv2.Canny(img1, 50, 200, apertureSize=3))
  21. cv2.imshow("gray", gray)
  22. cv2.waitKey(0)
  23. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图5

视频例程
  1. import cv2
  2. cap = cv2.VideoCapture(0)
  3. # 设置摄像头分辨率
  4. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 400)
  5. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)
  6. while(1):
  7. # 提取每一帧, frame 源视频
  8. _, frame = cap.read()
  9. frame = cv2.flip(frame, 1)
  10. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  11. #gaussian = cv2.GaussianBlur(gray, (9, 9), 2, 2)
  12. #canny = cv2.Canny(gaussian, 20, 100)
  13. circle = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1,
  14. minDist=20, param1=50, param2=50, minRadius=0, maxRadius=0)
  15. if circle is not None:
  16. center = (circle[0][0][0], circle[0][0][1])
  17. radius = circle[0][0][2]
  18. cv2.circle(frame, center, radius, (0, 0, 255), 4)
  19. cv2.imshow("frame", frame) # 源视频
  20. #cv2.imshow("gray", gray) # 源视频
  21. k = cv2.waitKey(1) & 0xFF
  22. if k == 27:
  23. break
  24. cap.release() # 记得释放掉捕获的视频
  25. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图6

效果并不是很好,是由于霍夫变换存在一定缺陷

更多资料

霍夫圆变换原理)

重映射

基本概念

重映射:把一副图像中某位置的像素放置到另一个图片指定位置的过程,为了完成映射过程,需要获得一些插值为非整数像素的坐标,因为原图像和目标图像的像素坐标是不一一对应的。

remap(src, map1, map2, interpolation[, dst[, borderMode[, borderValue]]]) -> dst

  • src: 源图像
  • map1: 1.表示点 (x, y) 的第一个映射 2.表示 CV_16SC2、CV_32FC1、或 CV_32FC2 类型的 X 值
  • map2: 根据 map1 参数来确定表示的对象,1. 若 map1 表示点 (x, y) 时,该参数不代表任何值 2. 表示 CV_16UC1、CV_32FC1 类型的 Y 值
  • interpolation: 插值的参数,在原图找不到的坐标使用插值补全
  • borderMode: 边界模式
  • borderValue:

例程

  1. import cv2
  2. def remap_mode(mode, map_x, map_y):
  3. if mode == 0: # 倒置
  4. for i in range(map_x.shape[0]):
  5. map_x[i,:] = [x for x in range(map_x.shape[1])]
  6. for j in range(map_y.shape[1]):
  7. #map_y[:,j] = [map_y.shape[0]-y for y in range(map_y.shape[0])]
  8. map_y[:,j] = [map_y.shape[0]-y-1 for y in range(map_y.shape[0])]
  9. elif mode == 1: # 竖直对称
  10. for i in range(map_x.shape[0]):
  11. map_x[i,:] = [map_x.shape[1]-x for x in range(map_x.shape[1])]
  12. for j in range(map_y.shape[1]):
  13. map_y[:,j] = [y for y in range(map_y.shape[0])]
  14. elif mode == 2: # mode=0 和 mode=1 的组合
  15. for i in range(map_x.shape[0]):
  16. map_x[i,:] = [map_x.shape[1]-x for x in range(map_x.shape[1])]
  17. for j in range(map_y.shape[1]):
  18. map_y[:,j] = [map_y.shape[0]-y for y in range(map_y.shape[0])]
  19. elif mode == 3: # 缩小后在中间显示
  20. for i in range(map_x.shape[0]):
  21. for j in range(map_x.shape[1]):
  22. if j > map_x.shape[1]*0.25 and j < map_x.shape[1]*0.75 and i > map_x.shape[0]*0.25 and i < map_x.shape[0]*0.75:
  23. map_x[i,j] = 2 * (j-map_x.shape[1]*0.25) + 0.5
  24. map_y[i,j] = 2 * (i-map_y.shape[0]*0.25) + 0.5
  25. else:
  26. map_x[i,j] = 0
  27. map_y[i,j] = 0
  28. #img = cv2.imread("./sample_img/apple.jpg")
  29. img = cv2.imread("./sample_img/Back_Projection_Theory2.jpg", 0)
  30. # 创建两个映射矩阵 x, y
  31. map_x = np.zeros((img.shape[0], img.shape[1]), dtype=np.float32)
  32. map_y = np.zeros((img.shape[0], img.shape[1]), dtype=np.float32)
  33. cv2.imshow("img", img)
  34. for i in range(4):
  35. remap_mode(i, map_x, map_y)
  36. dst = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR)
  37. cv2.imshow("dst"+str(i), dst)
  38. #print(map_x, map_y, map_x.shape, map_y.shape)
  39. cv2.waitKey(0)
  40. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图7

部分参数解释(以倒置为例)
  1. img = np.array(np.arange(10, 30).reshape(4, 5), dtype=np.uint8)
  2. map1 = np.zeros((img.shape[0], img.shape[1]), dtype=np.float32)
  3. map2 = np.zeros((img.shape[0], img.shape[1]), dtype=np.float32)
  4. # 倒置
  5. for i in range(map1.shape[0]):
  6. map1[i,:] = [x for x in range(map1.shape[1])] # 行坐标
  7. for j in range(map2.shape[1]):
  8. map2[:,j] = [map2.shape[0]-y-1 for y in range(map2.shape[0])] # 列坐标
  9. print(map1, map2, sep='\n')
  10. print(img, cv2.remap(img, map1, map2, cv2.INTER_LINEAR), sep='\n')
  1. [[0. 1. 2. 3. 4.]
  2. [0. 1. 2. 3. 4.]
  3. [0. 1. 2. 3. 4.]
  4. [0. 1. 2. 3. 4.]]
  5. [[3. 3. 3. 3. 3.]
  6. [2. 2. 2. 2. 2.]
  7. [1. 1. 1. 1. 1.]
  8. [0. 0. 0. 0. 0.]]
  9. [[10 11 12 13 14]
  10. [15 16 17 18 19]
  11. [20 21 22 23 24]
  12. [25 26 27 28 29]]
  13. [[25 26 27 28 29]
  14. [20 21 22 23 24]
  15. [15 16 17 18 19]
  16. [10 11 12 13 14]]

cv2.remap() 函数中的 map1, map2 参数代表目标图中的 (x,y) 点在原图中的 x 坐标(由map2提供)与 y 坐标(由map1提供)待定
OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图8

与 cv2.flip() 函数比较

flip(src, flipCode[, dst]) -> dst

  • src: 源图像
  • flipCode: 变换代码
    • 0: 倒影
    • 1: 左右镜面
    • 2: 0+1 的效果
  1. import cv2
  2. img = cv2.imread("./sample_img/apple.jpg")
  3. flip = cv2.flip(img, 0)
  4. flip_2 = cv2.flip(img, 1)
  5. flip_3 = cv2.flip(img, -1)
  6. cv2.imshow("img", img)
  7. cv2.imshow("flip", flip)
  8. cv2.imshow("flip_2", flip_2)
  9. cv2.imshow("flip_3", flip_3)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图9

直方图

直方图基本概念,分析

学习目标:

  • OpenCV 和 Numpy 中的函数查找直方图
  • 绘制直方图
  • cv2.calcHist(), np.histogram(), plt.hist()

直方图就是灰色图像每个像素,横坐标代表像素值(0 - 255),纵坐标代表每个像素值的个数。直方图可以直观了解该图像的对比度,亮度,强度分布。
类似:
OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图10

OpenCV 中的直方图计算函数

calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist

  • images: uint8 或者 float32 类型的图像, 使用时格式为 [img]
  • channels: 通道标识,若图像为灰度图,则使用 [0],彩色图用 [0], [1], [2] 分别代表蓝绿红通道的直方图
  • mask: 掩模,抽取图像中的某块区域时使用,创建的掩模白底就是目标区域,否则为黑色,mask 大小需与原图一致
  • histSize: 量程,代表直方图横坐标的最大值,对于图像来说使用 [256]
  • range: 像素值的范围 [0, 256]
  • hist: 返回一个序列,代表每个像素值的数量
  1. import cv2
  2. import numpy as np
  3. img = cv2.imread("./sample_img/apple.jpg")
  4. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  5. hue = np.zeros(hsv.shape, dtype=np.uint8)
  6. hue = cv2.mixChannels(hsv, hue, [0, 0])
  7. hist = cv2.calcHist(hue, [0], None, [180], [0, 180]) # 返回的是 (0, 256) 每个像素值的个数

Numpy 中的直方图计算函数
  1. import numpy as np
  2. hist, bins = np.histogram(img.ravel(), 256, [0, 256])
  1. hist = np.bincount(img.ravel(), minlength=256)

对于一维直方图,使用 np.bincount() 的速度比 np.histogram() 快 10 倍,但是 OpenCV 的方法更快,所以还是使用 OpenCV 的直方图方法

直方图的绘制

plt.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)

  • x: 序列或者数组,大小为 (n, )
  • bins: 在直方图中(像素值),代表将 256 等分的分数
  • range: 像素值的范围,例如 (0, 256)
  1. import cv2
  2. import matplotlib.pyplot as plt
  3. img = cv2.imread("./sample_img/apple.jpg", 0)
  4. plt.hist(img.ravel(), 256, [0, 256])
  5. plt.show()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图11

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. img = cv2.imread('./sample_img/apple.jpg')
  5. color = ('b', 'g', 'r')
  6. for i, col in enumerate(color):
  7. histr = cv2.calcHist([img], [i], None, [256], [0, 256])
  8. plt.plot(histr, color=col)
  9. plt.xlim([0, 256]) # 横坐标限制在 0 - 256
  10. plt.show()
  11. cv2.imshow("img", img)

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图12

添加掩模
  1. import cv2
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. img = cv2.imread('./sample_img/apple.jpg', 0)
  5. # create a mask
  6. mask = np.zeros(img.shape[:2], np.uint8)
  7. mask[100:300, 100:400] = 255
  8. masked_img = cv2.bitwise_and(img, img, mask=mask)
  9. # Calculate histogram with mask and without mask
  10. # Check third argument for mask
  11. hist_full = cv2.calcHist([img], [0], None, [256], [0, 256])
  12. hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 256])
  13. plt.subplot(221), plt.imshow(img, 'gray')
  14. plt.subplot(222), plt.imshow(mask, 'gray')
  15. plt.subplot(223), plt.imshow(masked_img, 'gray')
  16. plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
  17. plt.xlim([0, 256])
  18. plt.show()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图13

直方图均衡化

直方图均衡化被用来改善图像的对比度

直方图均衡化简单来说,就是把原始图像的直方图的分布均匀到所有像素。

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图14

均衡化
  1. import cv2
  2. img = cv2.imread("./sample_img/Histograms.jpg")
  3. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  4. histograms = cv2.equalizeHist(gray)
  5. cv2.imshow("img", img)
  6. cv2.imshow("histograms", histograms)
  7. cv2.waitKey(0)
  8. cv2.destroyAllWindows()

对图像的灰度图像进行直方图均衡化

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图15

  1. import cv2
  2. img = cv2.imread("./sample_img/Histograms.jpg")
  3. blue, green, red = cv2.split(img)
  4. blue_histograms = cv2.equalizeHist(blue)
  5. green_histograms = cv2.equalizeHist(green)
  6. red_histograms = cv2.equalizeHist(red)
  7. new_img = cv2.merge((blue_histograms, green_histograms, red_histograms))
  8. cv2.imshow("img", img)
  9. cv2.imshow("new_img", new_img)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()

对图像是 BGR 三通道进行直方图均衡化
OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图16

  1. import cv2
  2. img = cv2.imread("./sample_img/Histograms.jpg")
  3. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
  4. h, l, s = cv2.split(img)
  5. #h = cv2.equalizeHist(h)
  6. l = cv2.equalizeHist(l)
  7. #s = cv2.equalizeHist(s)
  8. new_img = cv2.merge((h, s, v))
  9. cv2.imshow("img", img)
  10. cv2.imshow("new_img", new_img)
  11. cv2.waitKey(0)
  12. cv2.destroyAllWindows()

对图像的 HLS 三通道的 L(亮度) 通道进行直方图均衡化

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图17

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. img = cv2.imread('./sample_img/wiki.jpg', 0)
  5. hist, bins = np.histogram(img.flatten(), 256, [0, 256])
  6. cdf = hist.cumsum() # 返回一个给定轴上的元素的累积和
  7. cdf_normalized = cdf * hist.max() / cdf.max()
  8. plt.plot(cdf_normalized, color='b')
  9. plt.hist(img.flatten(), 256, [0, 256], color='r')
  10. plt.xlim([0, 256])
  11. plt.legend(('cdf', 'histogram'), loc='upper left')
  12. plt.show()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图18

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. img = cv2.imread('./sample_img/wiki.jpg', 0)
  5. equ = cv2.equalizeHist(img)
  6. new_img = np.hstack((img, equ))
  7. cv2.imshow("new_img", new_img)
  8. #cv2.imwrite("./sample_img/equalizeHist.jpg", new_img)
  9. cv2.waitKey(0)
  10. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图19

CLAHE (Contrast Limited Adaptive Histogram Equalization) 自适应均衡化

以上图像的均衡化使用的是全局的对比度,但有时候全局的对比度不一定是最好的,故引申出自适应均衡化

  1. import numpy as np
  2. import cv2
  3. img = cv2.imread('./sample_img/tsukuba_l.png',0)
  4. histograms = cv2.equalizeHist(img)
  5. hstack_img = np.hstack((img, histograms))
  6. cv2.imshow("hstack_img", hstack_img)
  7. cv2.waitKey(0)
  8. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图20

直方图均衡后背景对比度有所提高。但比较两个图像中的雕像的脸。由于亮度过高,我们丢失了大部分信息。这是因为它的直方图并不局限于特定区域。

自适应均衡化的原理是:图像被分成称为“tile”的小块(在OpenCV中,tileSize 默认为 8x8)。然后像往常一样对这些块中的每一个进行直方图均衡。所以在一个小区域内,直方图会限制在一个小区域(除非有噪音)。如果有噪音,它会被放大。为避免这种情况,应用对比度限制。如果任何直方图区间高于指定的对比度限制(在 OpenCV 中默认为 40 ),则在应用直方图均衡之前,将这些像素剪切并均匀分布到其他区间。均衡后,为了去除图块边框中的瑕疵,应用双线性插值。

cv2.createCLAHE([, clipLimit[, tileGridSize]]) -> retval

  • clipLimit: 用于对比度限制的阈值
  • tileGridSize: 用于直方图均衡化的网格大小。输入图像将被分割成大小相等的矩形块。tileGridSize 定义行和列中的块的数量。

apply(src, [, dst]) -> dst :利用对比度有限的自适应直方图均衡化来均衡灰度图像的直方图。

  • src: CV_8UC1 类型的图像
  • dst: 输出的图像
  1. import numpy as np
  2. import cv2
  3. img = cv2.imread('./sample_img/tsukuba_l.png', 0)
  4. # create a CLAHE object (Arguments are optional).
  5. clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
  6. cl1 = clahe.apply(img)
  7. cv2.imshow('clahe_2', cl1)
  8. cv2.waitKey(0)
  9. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图21

雕像区域的轮廓变得清晰可见了

如何调整图像的对比度(附加的)
  1. import cv2
  2. import numpy as np
  3. img = cv2.imread("./sample_img/brain.jpg", 0)
  4. hist = cv2.equalizeHist(img) # 直方图均衡
  5. threshold = img.copy()
  6. threshold[threshold > 127] = 255
  7. threshold[threshold <= 127] = 0
  8. clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
  9. cl1 = clahe.apply(img)
  10. cv2.imshow("img", img)
  11. cv2.imshow("hist", hist)
  12. cv2.imshow("threshold", threshold)
  13. cv2.imshow("cl1", cl1)
  14. cv2.waitKey(0)
  15. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图22
OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图23

2D 直方图

以上绘制的是一维直方图(只考虑一个特征,即灰度强度值),本节将讨论 2D 直方图,考虑两个特征(色调和饱和度),用于查找颜色直方图。

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. img = cv2.imread("./sample_img/home.jpg")
  5. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  6. # OpenCV 方法
  7. # [0, 1] 代表 H 和 S 平面
  8. # [190, 256] 180 是 H 平面最大幅值,256 是 S 平面最大幅值
  9. hist_cv = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
  10. # Numpy 方法
  11. hist_numpy, xbins, ybins = np.histogram2d(hsv[0].ravel(), hsv[1].ravel(),
  12. [180, 256], [[0, 180], [0, 256]])
  13. cv2.imshow("original", img)
  14. cv2.imshow("hist_cv", hist_cv)
  15. cv2.imshow("hist_numpy", hist_numpy)
  16. cv2.waitKey(0)
  17. cv2.destroyAllWindows()
  1. plt.imshow(hist_cv, interpolation="nearest")

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图24

  1. plt.imshow(hist_numpy, interpolation="nearest")

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图25

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图26

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread('./sample_img/home.jpg')
  5. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  6. hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
  7. plt.imshow(hist, interpolation='nearest')
  8. plt.show()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图27

直方图比较

  1. import cv2
  2. base_img = cv2.imread("./sample_img/comparehist (1).jpg")
  3. half_base = base_img[0:base_img.shape[0]//2, 0:base_img.shape[1]//2]
  4. test_img = cv2.imread("./sample_img/comparehist (2).jpg")
  5. test2_img = cv2.imread("./sample_img/comparehist (3).jpg")
  6. base_hsv = cv2.cvtColor(base_img, cv2.COLOR_BGR2HSV)
  7. half_hsv = cv2.cvtColor(half_base, cv2.COLOR_BGR2HSV)
  8. test_hsv = cv2.cvtColor(test_img, cv2.COLOR_BGR2HSV)
  9. test2_hsv = cv2.cvtColor(test2_img, cv2.COLOR_BGR2HSV)
  10. base_hist = cv2.calcHist([base_hsv], [0, 1], None, [
  11. 180, 256], [0, 180, 0, 256])
  12. half_hist = cv2.calcHist([half_hsv], [0, 1], None, [
  13. 180, 256], [0, 180, 0, 256])
  14. test_hist = cv2.calcHist([test_hsv], [0, 1], None, [
  15. 180, 256], [0, 180, 0, 256])
  16. test2_hist = cv2.calcHist([test2_hsv], [0, 1], None, [
  17. 180, 256], [0, 180, 0, 256])
  18. base_hist = cv2.normalize(base_hist, base_hist, 0, 1, cv2.NORM_MINMAX, -1)
  19. half_hist = cv2.normalize(half_hist, half_hist, 0, 1, cv2.NORM_MINMAX, -1)
  20. test_hist = cv2.normalize(test_hist, test_hist, 0, 1, cv2.NORM_MINMAX, -1)
  21. test2_hist = cv2.normalize(test2_hist, test2_hist, 0, 1, cv2.NORM_MINMAX, -1)
  22. methods = ['cv2.HISTCMP_CORREL', 'cv2.HISTCMP_CHISQR', 'cv2.HISTCMP_INTERSECT',
  23. 'cv2.HISTCMP_BHATTACHARYYA', 'cv2.HISTCMP_HELLINGER', 'cv2.HISTCMP_CHISQR_ALT', 'cv2.HISTCMP_KL_DIV']
  24. for i in range(5):
  25. base_base = cv2.compareHist(base_hist, base_hist, i)
  26. half_base = cv2.compareHist(base_hist, half_hist, i)
  27. base_test = cv2.compareHist(base_hist, test_hist, i)
  28. base_test2 = cv2.compareHist(base_hist, test2_hist, i)
  29. print("method:"+str(methods[i]), sep='\n')
  30. print("base_base:", base_base, "half_base", half_base,
  31. "base_test", base_test, "base_test2", base_test2)

结果

  • method:cv2.HISTCMP_CORREL base_base: 1.0 half_base 0.868398994704612 base_test 0.08065514950383738 base_test2 0.05901126222644142
  • method:cv2.HISTCMP_CHISQR base_base: 0.0 half_base 21.246470173301855 base_test 53204.28067584426 base_test2 1583.6240597344906
  • method:cv2.HISTCMP_INTERSECT base_base: 37.25639865006087 half_base 14.123541084351018 base_test 17.150308343523648 base_test2 9.475103021075483
  • method:cv2.HISTCMP_BHATTACHARYYA base_base: 0.0 half_base 0.47332301423304435 base_test 0.7157340757283192 base_test2 0.6902075908999336
  • method:cv2.HISTCMP_HELLINGER base_base: 0.0 half_base 38.69455570916867 base_test 697.8930204983994 base_test2 77.96448766974669

cv2.HISTCMP_CORREcv2.HISTCMP_INTERSECT

直方图 API

更多资料

本节资料)
Cambridge in Color website)

维基百科直方图均衡化)

How can I adjust contrast in OpenCV in C?
)

  • 直方图处理
  • 阈值化处理
  • 其他

反向投影

理论知识

反向投影可以用来做图像分割,寻找感兴趣区间。

  • 反向投影是一种记录给定图像中的像素点如何适应直方图模型像素分布的方式。
  • 简单的讲, 所谓反向投影就是首先计算某一特征的直方图模型,然后使用模型去寻找图像中存在的该特征。
  • 例如, 你有一个肤色直方图 ( Hue-Saturation 直方图 ),你可以用它来寻找图像中的肤色区域:

例程

  1. import cv2
  2. import numpy as np
  3. def nothing(x):
  4. pass
  5. img = cv2.imread("./sample_img/back.jpg")
  6. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  7. hue = np.zeros(hsv.shape, dtype=np.uint8)
  8. cv2.createTrackbar("bins", "dst", 0, 255, nothing)
  9. while(1):
  10. bins = cv2.getTrackbarPos("bins", "dst")
  11. hue = cv2.mixChannels(hsv, hue, [0, 0])
  12. hist = cv2.calcHist(hue, [0], None, [180], [0, 180])
  13. hist = cv2.normalize(hist, hist, 0, 1, cv2.NORM_MINMAX, -1)
  14. dst = cv2.calcBackProject([hsv], [0], hist, [0, 180], 1)
  15. cv2.imshow("dst", dst)
  16. k = cv2.waitKey(1) & 0xff
  17. if k == 27:
  18. break
  19. cv2.destroyAllWindows()
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. def calc_backproject():
  5. sample = cv2.imread("./sample_img/sample.png")
  6. target = cv2.imread("./sample_img/target.png")
  7. roi_hsv = cv2.cvtColor(sample, cv2.COLOR_BGR2HSV)
  8. target_hsv = cv2.cvtColor(target, cv2.COLOR_BGR2HSV)
  9. cv2.imshow("sample", sample)
  10. cv2.imshow("target", target)
  11. roiHist = cv2.calcHist(roi_hsv, [0, 1], None,
  12. [30, 30], [0, 180, 0, 256])
  13. cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
  14. dst = cv2.calcBackProject([target_hsv], [0, 1],
  15. roiHist, [0, 180, 0, 256], 1)
  16. cv2.imshow("dst", dst)
  17. plt.imshow(dst)
  18. calc_backproject()
  19. cv2.waitKey(0)
  20. cv2.destroyAllWindows()

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图28

模板匹配

理论

学习目标

  • 查找图像中的指定对象
  • cv2.matchTemplate(), cv2.minMaxLoc()

在一副图像中查找与模板图像最匹配(相似)的部分。对模板图像进行滑动匹配源图像

matchTemplate(image, templ, method[, result[, mask]]) -> result

  • image: 待搜索图像, 8 位或者 32 位浮点型图像 大小: W×H
  • templ: 搜索模板,需要和原图像有一样的数据类型,且大小不能超过原图像 大小: w×h
  • method: 匹配方法
    • M_SQDIFF = 0, 平方差匹配 数值越小,有着越高的匹配结果
    • TM_SQDIFF_NORMED = 1, 归一化 平方差匹配 数值越小,有着越高的匹配结果
    • 以下方法,数值越小,匹配结果越好
    • TM_CCORR = 2, 相关匹配
    • TM_CCORR_NORMED = 3, 归一化相关匹配
    • TM_CCOEFF = 4, 系数匹配
    • TM_CCOEFF_NORMED = 5 归一化相关系数匹配
  • result: 输出图像 大小 (W-w+1×H-h+1)
    模板匹配方法

简单法:平方差方法,速度快,但不是很精确
复杂法:相关系数法,计算量大,较精确
综合考虑选择运用哪种匹配方法

minMaxLoc(src[, mask]) -> minVal, maxVal, minLoc, maxLoc

  • 函数功能:找到数组或者图像最小最大值及其位置
  • src: 要查找的数组
  • minVal, maxVal: 最小,最大值
  • minLoc, maxLoc; 最小,最大值的位置
  1. import numpy as np
  2. img = np.array(np.arange(25)).reshape(5, 5)
  3. print(img, cv2.minMaxLoc(img), sep='\n')
  1. [[ 0 1 2 3 4]
  2. [ 5 6 7 8 9]
  3. [10 11 12 13 14]
  4. [15 16 17 18 19]
  5. [20 21 22 23 24]]
  6. (0.0, 24.0, (0, 0), (4, 4))

eval() 用法说明

  1. import cv2
  2. methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
  3. 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
  4. for method in methods:
  5. print(eval(method)) # 可以判断 methods 里面的值在 OpenCV 中对应的值
  6. # 运行结果
  7. '''
  8. 4
  9. 5
  10. 2
  11. 3
  12. 0
  13. 1
  14. '''

例程

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread('./sample_img/template (1).jpg', 0)
  5. img = cv2.resize(img, (600, 600))
  6. img2 = img.copy()
  7. template = cv2.imread('./sample_img/template (2).jpg', 0)
  8. w, h = template.shape[::-1]
  9. # All the 6 methods for comparison in a list
  10. methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
  11. 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
  12. for meth in methods:
  13. img = img2.copy()
  14. method = eval(meth) # 可以判断 methods 里面的值在 OpenCV 中对应的值
  15. # Apply template Matching
  16. res = cv2.matchTemplate(img, template, method)
  17. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  18. #print(res)
  19. # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
  20. if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
  21. top_left = min_loc
  22. else:
  23. top_left = max_loc
  24. bottom_right = (top_left[0] + w, top_left[1] + h)
  25. cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2)
  26. #cv2.imshow('Matching Result'+str(meth), res)
  27. cv2.imshow('Detected Point'+str(meth), img)
  28. cv2.waitKey(0)
  29. cv2.destroyAllWindows()

cv2.TM_CCORR 方法的效果不是很好

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图29

多目标匹配

cv2.TM_CCOEFF_NORMED 方法
  1. import cv2
  2. import numpy as np
  3. import time
  4. t0 = time.time()
  5. img = cv2.imread("./sample_img/mario.jpg")
  6. template = img[170:220, 334:373, :]
  7. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  8. template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
  9. w, h = template.shape[::-1]
  10. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) # 用归一化的方法,阈值的设定比较方便
  11. threshold = 0.8 # 表示模板和检测目标 80% 的匹配程度就认为是所要查找的目标
  12. loc = np.where(res >= threshold)
  13. for pt in zip(*loc[::-1]):
  14. cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
  15. print("time:", time.time()-t0)
  16. cv2.imshow("img", img)
  17. cv2.waitKey(0)
  18. cv2.destroyAllWindows()
  1. time: 0.07096028327941895

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图30

  1. import matplotlib.pyplot as plt
  2. plt.hist(res.ravel());

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图31

cv2.TM_SQDIFF_NORMED 方法
  1. import cv2
  2. import numpy as np
  3. import time
  4. t0 = time.time()
  5. img = cv2.imread("./sample_img/mario.jpg")
  6. template = img[170:220, 334:373, :]
  7. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  8. template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
  9. w, h = template.shape[::-1]
  10. res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF_NORMED) # 在
  11. threshold = 0.2 # 表示模板和检测目标 80% 的匹配程度就认为是所要查找的目标
  12. loc = np.where(res <= threshold)
  13. for pt in zip(*loc[::-1]):
  14. cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
  15. print("time:", time.time()-t0)
  16. cv2.imshow("img", img)
  17. cv2.waitKey(0)
  18. cv2.destroyAllWindows()
  1. time: 0.09594225883483887

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图32

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图33

cv2.TM_CCORR_NORMED 方法
  1. import cv2
  2. import numpy as np
  3. import time
  4. t0 = time.time()
  5. img = cv2.imread("./sample_img/mario.jpg")
  6. template = img[170:220, 334:373, :]
  7. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  8. template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
  9. w, h = template.shape[::-1]
  10. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCORR_NORMED) # 在
  11. threshold = 0.95 # 表示模板和检测目标 80% 的匹配程度就认为是所要查找的目标
  12. loc = np.where(res >= threshold)
  13. for pt in zip(*loc[::-1]):
  14. cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
  15. print("time:", time.time()-t0)
  16. cv2.imshow("img", img)
  17. cv2.waitKey(0)
  18. cv2.destroyAllWindows()
  1. time: 0.05097007751464844

此方法把一个不是很明显的目标也检测出来,此方法的性能估计比较高
OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图34

  1. import matplotlib.pyplot as plt
  2. plt.hist(res.ravel());

OpenCV 系列教程 5 | OpenCV 图像处理(中) - 图35

Tips
  1. a = [1, 2, 3, 4]
  2. a[::-1] # 反转
  3. #结果 [4, 3, 2, 1]

Python zip 功能说明

  1. a = [1, 2, 3]
  2. b = [4, 5, 6]
  3. c = [4, 5, 6, 7, 8]
  4. zipped = zip(a, b) # 打包为元组的列表
  5. [k for k in zip(*zipped)] # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
  6. # 结果 [(1, 2, 3), (4, 5, 6)]