会变魔术的 OpenCV_04 图像处理


🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


OpenCV是计算机视觉中经典的专用库,其支持多语言、跨平台,功能强大 ;

本篇具体介绍如何利用OpenCV内部的不同 图像处理函数

改变颜色空间

改变颜色空间

BGR↔灰度图 :cv.COLOR_BGR2GRAY
BGR↔HSV: cv.COLOR_BGR2HSV

所有的颜色空间转换方法 :

import cv2 as cv
flags = [i for i in dir(cv) if i.startswith('COLOR_')]
print( flags )
['COLOR_BAYER_BG2BGR', 
'COLOR_BAYER_BG2BGRA',
... ...   # 超过150种方法
'COLOR_YUV420sp2RGBA',
'COLOR_mRGBA2RGBA']

对象追踪

cv.cvtColor()
cv.inRange()

在视频中提取一个蓝色的对象,在HSV中比在BGR颜色空间中更容易表示颜色 :

import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while(1):
    # 读取帧
    _, frame = cap.read()
    # 转换颜色空间 BGR 到 HSV
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # 定义HSV中蓝色的范围
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    
    # 设置HSV的阈值使得只取蓝色
    mask = cv.inRange(hsv, lower_blue, upper_blue)
    # 将掩膜和图像逐像素相加
    res = cv.bitwise_and(frame,frame, mask= mask)
    
    cv.imshow('Original Frame',frame)
    cv.imshow('Mask Image',mask)
    cv.imshow('Final Result',res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()

运行结果:

在这里插入图片描述


追踪HSV值

green = np.uint8([[[0,255,0 ]]])
hsv_green = cv.cvtColor(green,cv.COLOR_BGR2HSV)
print( hsv_green )

运行结果:

[[[ 60 255 255]]]

则查找的Green颜色的 HSV 值的 上下限 为:[60-10, 255, 255][60+10, 255, 255]


图像的几何变换

缩放

cv.resize()

import numpy as np
import cv2 as cv
img = cv.imread('./001.png')
print(img.shape)
res = cv.resize(img, None, fx=2, fy=2, interpolation = cv.INTER_CUBIC)   # fx fy:缩放比
print(res.shape)

运行结果:

(118, 100, 3)
(236, 200, 3)

或 :

import numpy as np
import cv2 as cv
img = cv.imread('./001.png')
print(img.shape)
height, width = img.shape[:2]
res = cv.resize(img, (2*width, 2*height), interpolation = cv.INTER_CUBIC)
print(res.shape)

运行结果:

(118, 100, 3)
(236, 200, 3)

平移

cv2.warpAffine(src, M, dsize)
src : 输入图像
M : 2×3,2行3列变换矩阵
dsize : 输出图像的大小

import numpy as np
import cv2 as cv
img = cv.imread('./001.png', 0)
rows,cols = img.shape
M = np.float32([[1,0,50], [0,1,20]])    # 偏移量为(50, 20)
dst = cv.warpAffine(img, M, (cols,rows))    # 输出图像大小 (cols,rows)
cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:

在这里插入图片描述


旋转

cv2.getRotationMatrix2D(center, angle, scale)
center:旋转中心位置
angle :旋转角度
scale :缩放比例,不缩放时为1

import cv2 as cv
img = cv.imread('./001.png', 0)
rows,cols = img.shape
center=((rows-1)/2.0, (rows-1)/2.0)
M = cv.getRotationMatrix2D(center, 90, 1)
dst = cv.warpAffine(img,M,(cols,rows))
cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:

在这里插入图片描述

方法2:

cv2.rotate(src, rotateCode)
cv2.ROTATE_90_CLOCKWISE :顺时针旋转90度
cv2.ROTATE_180 :旋转180度,不区分顺时针或逆时针,效果一样
cv2.ROTATE_90_COUNTERCLOCKWISE :逆时针旋转90度,等同于顺时针270度

import cv2 as cv
img = cv.imread('./001.png', 0)
dst = cv.rotate(img, cv.ROTATE_90_CLOCKWISE)
cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:
在这里插入图片描述


仿射变换

cv.getAffineTransform()

原始图像中的所有平行线在输出图像中仍将平行,需要输入图像中的三个点及其在输出图像中的对应位置 :

img = cv.imread('drawing.png')
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
dst = cv.warpAffine(img,M,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')

运行结果:

在这里插入图片描述


透视变换

cv.getPerspectiveTransform()

需要在输入图像上有4个点,在输出图像上需要相应的点,且其中三个不应共线 :

img = cv.imread('sudoku.png')
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

运行结果:

在这里插入图片描述


图像阈值

简单阈值

cv2.threshold(src, thresh, maxval, type)
src:源图像
thresh:比较的阈值
maxval:阈值化方法为THRESH_BINARY和THRESH_BINARY_INV时单个像素转换后的最大值
type:阈值化类型

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./001.png', 0)
ret,thresh1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img, 127, 255, cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
	plt.figure(figsize=(20,20))
    plt.subplot(2,3,i+1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


自适应阈值

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
src :源图像
maxValue :单个像素转换后的最大值
adaptiveMethod :自适应方法
thresholdType :阈值化类型,THRESH_BINARYTHRESH_BINARY_INV
blockSize :计算某个像素使用的阈值时采用的窗口大小,奇数值
C :平均值或加权平均值减去的常量值;可以为正值,0或负值

cv.ADAPTIVE_THRESH_MEAN_C :平均阈值是邻近区域的平均值减去常数C
cv.ADAPTIVE_THRESH_GAUSSIAN_C :高斯阈值是邻域值的高斯加权总和减去常数C

import cv2 as cv
img = cv.imread('./001.png')
img_gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
img_ret1 = cv.adaptiveThreshold(img_gray, 255, cv.ADAPTIVE_THRESH_MEAN_C,
								cv.THRESH_BINARY, 55, 0 ) 
img_ret2 = cv.adaptiveThreshold(img_gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,
								cv.THRESH_BINARY, 55, 0 ) 
cv.imshow('img',img)
cv.imshow('MEAN',img_ret1)
cv.imshow('GAUSSIAN',img_ret2)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:

在这里插入图片描述

Otsu二值化

在全局阈值化中,我们使用任意选择的值作为阈值;

而在Otsu的方法中从图像直方图中确定最佳全局阈值:

import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./002.jpg',0)
# 全局阈值
ret1,th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
# Otsu阈值
ret2,th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
# 高斯滤波后再采用Otsu阈值
blur = cv.GaussianBlur(img, (5,5), 0)
ret3,th3 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
# 绘制所有图像及其直方图
images = [img, 0, th1,
          img, 0, th2,
          blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
          'Original Noisy Image','Histogram',"Otsu's Thresholding",
          'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in range(3):
    plt.subplot(3,3,i*3+1), plt.imshow(images[i*3], 'gray')
    plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+2), plt.hist(images[i*3].ravel(), 256)
    plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+3), plt.imshow(images[i*3+2], 'gray')
    plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


图像平滑

2D卷积(图像过滤)

cv.filter2D():对图像进行卷积操作;

低通滤波器(LPF):消除噪声,使图像模糊;
高通滤波器(HPF):在图像中找到边缘;

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./001.png')
kernel = np.ones((5,5), np.float32)/25
dst = cv.filter2D(img, -1, kernel)
fig = plt.figure(figsize=(15, 15))
plt.subplot(121), plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


图像模糊

平均滤波

cv.blur() :获取内核区域下所有像素的平均值,并替换中心元素;

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./001.png')
blur = cv.blur(img,(5,5))
fig = plt.figure(figsize=(15, 15))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


高斯滤波

cv.GaussianBlur():使用高斯核代替盒式滤波器,宽度和高度应为正数和奇数,对于从图像中去除高斯噪声非常有效;

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./001.png')
blur = cv.GaussianBlur(img,(5,5),0)
fig = plt.figure(figsize=(15, 15))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


中位滤波

cv.medianBlur():提取内核区域下所有像素的中值,并将中心元素替换为该中值,内核大小应为正奇数整数,对于椒盐噪声非常有效;

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./001.png')
median = cv.medianBlur(img,5)
fig = plt.figure(figsize=(15, 15))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(median),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


双边滤波

cv.bilateralFilter():去除噪声的同时保持边缘清晰锐利;

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./001.png')
blur = cv.bilateralFilter(img,9,75,75)
fig = plt.figure(figsize=(15, 15))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


形态转换

图像腐蚀

cv.erode()

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5,5), np.uint8)
erosion = cv.erode(img, kernel, iterations = 1)
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('Erosion')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


图像膨胀

cv.dilate()

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5,5), np.uint8)
dilation = cv.dilate(img, kernel, iterations = 1) 
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dilation),plt.title('Dilation')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:
在这里插入图片描述


开运算

cv.morphologyEx() cv.MORPH_OPEN :先腐蚀后膨胀,用于消除噪声;

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5,5), np.uint8)
opening = cv.morphologyEx(img, cv.MORPH_OPEN, kernel) 
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(opening),plt.title('Opening')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


闭运算

cv.morphologyEx() cv.MORPH_CLOSE :先膨胀后腐蚀,用于关闭前景对象内部的小孔或对象上的小黑点;

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5,5), np.uint8)
closing = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel) 
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(closing),plt.title('Closing')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


形态学梯度

cv.morphologyEx() cv.MORPH_GRADIENT :图像腐蚀与膨胀之差;

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5,5), np.uint8)
gradient = cv.morphologyEx(img, cv.MORPH_GRADIENT, kernel) 
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(gradient),plt.title('Gradient')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


顶帽

cv.morphologyEx() cv.MORPH_TOPHAT :输入图像和图像开运算之差;

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5, 5), np.uint8)
tophat = cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel) 
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(tophat),plt.title('Tophat')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


黑帽

cv.morphologyEx() cv.MORPH_BLACKHAT :输入图像和图像闭运算之差;

import cv2 as cv
import numpy as np
img = cv.imread('./002.png')
kernel = np.ones((5, 5), np.uint8)
blackhat = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel) 
fig = plt.figure(figsize=(6, 6))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blackhat),plt.title('Blackhat')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


图像梯度

cv.Sobel()cv.Scharr() :ksize = -1,则使用 3x3 Scharr 滤波器;
cv.Laplacian()

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./003.png', 0)
laplacian = cv.Laplacian(img, cv.CV_64F)
sobelx = cv.Sobel(img, cv.CV_64F, 1,0, ksize=5)    # 水平导数方向
sobely = cv.Sobel(img, cv.CV_64F, 0,1, ksize=5)    # 垂直导数方向
fig = plt.figure(figsize=(10, 10))
plt.subplot(2,2,1),plt.imshow(img, cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian, cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx, cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely, cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


Canny边缘检测

cv.Canny()

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./003.png',0)
edges = cv.Canny(img, 100, 100)
fig = plt.figure(figsize=(10, 10))
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

在这里插入图片描述


图像轮廓

轮廓

cv.findContours()
cv.drawContours()

import numpy as np
import cv2 as cv
img = cv.imread('./001.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(img, contours, -1, (255,0,0), 1)    # -1表示绘制所有轮廓

cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:

在这里插入图片描述


轮廓特征

特征矩

import numpy as np
import cv2 as cv
img = cv.imread('./005.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cv.drawContours(img, contours, 0, (255,0,0), 3)    # 0表示绘制所有轮廓
cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

cnt = contours[0]
M = cv.moments(cnt)
print(M)

cx = int(M['m10']/M['m00'])    # 对应对象的重心
cy = int(M['m01']/M['m00'])
print((cx, cy))

运行结果:

{'m00': 33499.0, 
'm10': 8384054.0, 
'm01': 8793702.666666666, 
... ...
'nu12': 8.823186369609854e-06,
'nu03': 5.661727831836976e-06}

(250, 262)

轮廓面积

area = cv.contourArea(cnt)

运行结果:

33499.0

轮廓周长

perimeter = cv.arcLength(cnt,True) :闭合轮廓 (True)

运行结果:

974.1463149785995

轮廓近似

import numpy as np
import cv2 as cv
img = cv.imread('./004.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]
epsilon = 0.1*cv.arcLength(cnt,True)     # 显示ε=弧长10%的近似曲线
approx = cv.approxPolyDP(cnt,epsilon,True)
print(approx)

cv.polylines(img, [approx], True, (255, 0, 0), 3)
cv.imshow('img', img)      # 原图、0.1、0.01
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:

在这里插入图片描述


边界矩形

import numpy as np
import cv2 as cv
img = cv.imread('./006.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]

x,y,w,h = cv.boundingRect(cnt)
cv.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)     # 绘制第一个矩形边框(黄色)

rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img,[box],0,(0,255,0),2)     # 绘制第二个矩形边框(绿色)

cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:

在这里插入图片描述


最小闭合圈

import numpy as np
import cv2 as cv
img = cv.imread('./006.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]

(x,y),radius = cv.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv.circle(img,center,radius,(0,255,0),2)

cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:
在这里插入图片描述


拟合椭圆

import numpy as np
import cv2 as cv
img = cv.imread('./006.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]

ellipse = cv.fitEllipse(cnt)
cv.ellipse(img,ellipse,(0,255,0),2)

cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:
在这里插入图片描述


拟合直线

import numpy as np
import cv2 as cv
img = cv.imread('./006.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]

rows,cols = img.shape[:2]
[vx,vy,x,y] = cv.fitLine(cnt, cv.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
cv.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)

cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:
在这里插入图片描述


轮廓属性

长宽

x,y,w,h = cv.boundingRect(cnt)

掩码

import numpy as np
import cv2 as cv
img = cv.imread('./005.png')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]

mask = np.zeros(imgray.shape,np.uint8)
cv.drawContours(mask, [cnt], 0, 255, -1)

cv.imshow('mask', mask)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:
在这里插入图片描述


直方图

通过直方图可以总体了解图像的该图像的对比度,亮度,强度分布等;

X轴上具有像素值(不总是从0到255的范围),在Y轴上具有图像中相应像素数的图;

OpenCV中的直方图

cv.calcHist(images,channels,mask,histSize,ranges [,hist [,accumulate]])

images:它是uint8或float32类型的源图像;
channels:计算直方图通道的索引;
      对于灰度图像,则其值为[0];
      对于彩色图像,[0],[1]或[2]分别代表计算蓝色,绿色或红色通道的直方图
mask:图像掩码;
histSize:BIN计数,需要放在方括号中,对于全尺寸,我们通过[256];
ranges:RANGE,通常为[0,256];


import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./001.png',0)
# create a mask
mask = np.zeros(img.shape[:2], np.uint8)
mask[50:250, 100:433] = 255
masked_img = cv.bitwise_and(img,img,mask = mask)

hist_full = cv.calcHist([img],[0],None,[256],[0,256])
hist_mask = cv.calcHist([img],[0],mask,[256],[0,256])

plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(mask,'gray')
plt.subplot(223), plt.imshow(masked_img, 'gray')
plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
plt.xlim([0,256])
plt.show()

运行结果:
在这里插入图片描述


多通道显示:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./001.png')
color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()

运行结果:
在这里插入图片描述


直方图均衡

直方图均衡

cv.equalizeHist()

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./001.png',0)
equ = cv.equalizeHist(img)

hist_img = cv.calcHist([img],[0],None,[256],[0,256])
hist_equ = cv.calcHist([equ],[0],None,[256],[0,256])

plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(equ,'gray')
plt.subplot(223), plt.plot(hist_img)
plt.subplot(224), plt.plot(hist_equ)
plt.xlim([0,256])
plt.show()

运行结果:

在这里插入图片描述


自适应直方图均衡

cv2.createCLAHE()

clipLimit:对比对限制阈值,默认为40;
tileGridSize:直方图均衡的栅格尺寸,默认为8×8;

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('./001.png',0)
clahe = cv.createCLAHE(clipLimit=5.0, tileGridSize=(8,8))
img_clahe = clahe.apply(img)

hist_img = cv.calcHist([img],[0],None,[256],[0,256])
hist_clahe = cv.calcHist([img_clahe],[0],None,[256],[0,256])

plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(img_clahe,'gray')
plt.subplot(223), plt.plot(hist_img)
plt.subplot(224), plt.plot(hist_clahe)
plt.xlim([0,256])
plt.show()

运行结果:

在这里插入图片描述


模板匹配

cv.matchTemplate()

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./001.png',0)
img2 = img.copy()
template = cv.imread('./0001.png',0)
w, h = template.shape[::-1]

# 6种方法比较
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
            'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']

for meth in methods:
    img = img2.copy()
    method = eval(meth)
    # 模板匹配
    res = cv.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)

    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    
    print(meth)
    cv.rectangle(img,top_left, bottom_right, 255, 2)
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.show()

运行结果:

在这里插入图片描述


多对象的模板匹配

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_rgb = cv.imread('./001.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('./0001.png',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite('res.png',img_rgb)

运行结果:
在这里插入图片描述


霍夫变换

霍夫直线变换

cv.HoughLines()

应用霍夫变换之前,需先使用阈值或使用Canny边缘检测;

import cv2 as cv
import numpy as np
img = cv.imread(cv.samples.findFile('./001.png'))
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray,50,150,apertureSize = 3)
lines = cv.HoughLines(edges,1,np.pi/180,200)
for line in lines:
    rho,theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))
    cv.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:


霍夫圆圈变换

cv.HoughCircles

import numpy as np
import cv2 as cv
img = cv.imread('./001.png',0)
img = cv.medianBlur(img,5)
cimg = cv.cvtColor(img,cv.COLOR_GRAY2BGR)
circles = cv.HoughCircles(img,cv.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # 绘制外圆
    cv.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # 绘制圆心
    cv.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv.imshow('circles',cimg)
cv.waitKey(0)
cv.destroyAllWindows()

运行结果:
在这里插入图片描述




OpenCV官方文档链接🔗 :OpenCV-Python 官方文档

OpenCV系列链接🔗:会变魔术的 OpenCV

会变魔术的 OpenCV_01 简介与安装
会变魔术的 OpenCV_02 GUI特性
会变魔术的 OpenCV_03 核心操作
会变魔术的 OpenCV_04 图像处理
会变魔术的 OpenCV_05 实例分析


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值