检测和分割目标图像实例
代码示例:
import cv2 as cv
import numpy as np
# 读取图片
img_path = r"D:\workplace\data\opencv\football.jpg"
img = cv.imread(img_path)
# 转灰度
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# 对灰度图,高斯去噪
blurred = cv.GaussianBlur(gray, (9, 9), 0) # todo: 如何去噪
cv.imshow('blurred_img', blurred)
# 提取图像梯度,以sobel算子,计算x,y方向上的梯度
grad_x = cv.Sobel(blurred, ddepth=cv.CV_32F, dx=1, dy=0) # 或者对blurred,gray
grad_y = cv.Sobel(blurred, ddepth=cv.CV_32F, dx=0, dy=1)
gradient = cv.subtract(grad_x, grad_y)
gradient = cv.convertScaleAbs(gradient)
cv.imshow('gradient_img', gradient)
# 去噪
blurred = cv.GaussianBlur(gradient, (9,9),0)
(_, thresh) = cv.threshold(blurred, 90,255,cv.THRESH_BINARY)
# 图像形态学
# 建立一个椭圆函数
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (25,25))
closed = cv.morphologyEx(thresh, cv.MORPH_CLOSE, kernel)
# 细节刻画
closed = cv.erode(closed, None, iterations=4)
closed = cv.dilate(closed, None, iterations=4)
# 找出区域轮廓
(_,cnts, _) = cv.findContours(closed.copy(),cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# 画出轮廓
c = sorted(cnts, key=cv.contourArea, reverse=True)[0]
rect = cv.minAreaRect(c)
box = np.int0(cv.boxPoints(rect))
draw_img = cv.drawContours(img.copy(), [box], -1, (0,0,255), 3)
# 剪切出对区域
xs = [i[0] for i in box]
ys = [i[1] for i in box]
x1 = abs(min(xs))
x2 = abs(max(xs))
y1 = abs(min(ys))
y2 = abs(max(ys))
height = y2 - y1
width = x2 - x1
crop_img = img[y1:y1+height, x1:x1+width]
if __name__ == '__main__':
# 显示
# cv.imshow('origin_img', origin_img)
# cv.imshow('blurred_img', blurred)
# cv.imshow('grad_x_img', grad_x)
# cv.imshow('grad_y_img', grad_y)
# cv.imshow('gradient_img', gradient)
# cv.imshow('thresh_img', thresh)
# cv.imshow('closed_img', closed)
cv.imshow('draw_img', draw_img)
cv.imshow('select_img', crop_img)
cv.waitKey(0)
cv.destroyAllWindows()
结果展示:
opencv api函数调用
# 用来转化图像格式的
img = cv2.cvtColor(src,
COLOR_BGR2HSV # BGR---->HSV
COLOR_HSV2BGR # HSV---->BGR
...)
# For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]
# 返回一个阈值,和二值化图像,第一个阈值是用来otsu方法时候用的
# 不过现在不用了,因为可以通过mahotas直接实现
T = ret = mahotas.threshold(blurred)
ret, thresh_img = cv2.threshold(src, # 一般是灰度图像
num1, # 图像阈值
num2, # 如果大于或者num1, 像素值将会变成 num2
# 最后一个二值化参数
cv2.THRESH_BINARY # 将大于阈值的灰度值设为最大灰度值,小于阈值的值设为0
cv2.THRESH_BINARY_INV # 将大于阈值的灰度值设为0,大于阈值的值设为最大灰度值
cv2.THRESH_TRUNC # 将大于阈值的灰度值设为阈值,小于阈值的值保持不变
cv2.THRESH_TOZERO # 将小于阈值的灰度值设为0,大于阈值的值保持不变
cv2.THRESH_TOZERO_INV # 将大于阈值的灰度值设为0,小于阈值的值保持不变
)
thresh = cv2.AdaptiveThreshold(src,
dst,
maxValue,
# adaptive_method
ADAPTIVE_THRESH_MEAN_C,
ADAPTIVE_THRESH_GAUSSIAN_C,
# thresholdType
THRESH_BINARY,
THRESH_BINARY_INV,
blockSize=3,
param1=5
)
# 一般是在黑色背景中找白色物体,所以原始图像背景最好是黑色
# 在执行找边缘的时候,一般是threshold 或者是canny 边缘检测后进行的。
# warning:此函数会修改原始图像、
# 返回:坐标位置(x,y),
(_, cnts, _) = cv2.findContours(mask.copy(),
# cv2.RETR_EXTERNAL, #表示只检测外轮廓
# cv2.RETR_CCOMP, #建立两个等级的轮廓,上一层是边界
cv2.RETR_LIST, #检测的轮廓不建立等级关系
# cv2.RETR_TREE, #建立一个等级树结构的轮廓
# cv2.CHAIN_APPROX_NONE, #存储所有的轮廓点,相邻的两个点的像素位置差不超过1
cv2.CHAIN_APPROX_SIMPLE, #例如一个矩形轮廓只需4个点来保存轮廓信息
# cv2.CHAIN_APPROX_TC89_L1,
# cv2.CHAIN_APPROX_TC89_KCOS
)
img = cv2.drawContours(src, cnts, whichToDraw(-1), color, line)
img = cv2.imwrite(filename, dst, # 文件路径,和目标图像文件矩阵
# 对于JPEG,其表示的是图像的质量,用0-100的整数表示,默认为95
# 注意,cv2.IMWRITE_JPEG_QUALITY类型为Long,必须转换成int
[int(cv2.IMWRITE_JPEG_QUALITY), 5]
[int(cv2.IMWRITE_JPEG_QUALITY), 95]
# 从0到9,压缩级别越高,图像尺寸越小。默认级别为3
[int(cv2.IMWRITE_PNG_COMPRESSION), 5])
[int(cv2.IMWRITE_PNG_COMPRESSION), 9])
# 寻找某个函数或者变量
events = [i for i in dir(cv2) if 'PNG' in i]
print( events )
寻找某个变量开头的flags
flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
print flags
批量读取文件名字
import os
filename_rgb = r'C:\Users\...'
for filename in os.listdir(filename_rgb): #listdir的参数是文件夹的路径
print (filename)