使用python ,opencv进行数据集的初步处理
数据来源
最近从实验室师兄拿到一批太阳能板的EL图数据集,想通过语义分割的方式进行太阳能板的裂纹检测,由于拍摄角度比较倾斜不定,得到的图像没有规则,如下图:

编写如下脚本,主要实现如下功能:1,将文件夹中的图片路径提取并保存为txt文件:
import os
path = os.listdir('huizhou_Detection')
f = open('huizhou_Detection.txt', 'w')
for _ in path:
name="huizhou_Detection/"+ _
f.write(name + '\n')
f.close()
2: 对图像进行处理,处理顺序为:通过图片路径读取图片-》将图片转换为灰度图-》对图片进行适当缩小-》进行中值滤波-》图像二值化-》膨胀处理-》膨胀再腐蚀操作-》寻找图像的轮廓(由于返回的轮廓是点集,因此我们采用包含最多点的集合作为我们的轮廓集合)-》筛选出轮廓点的左上、左下、右上、右下四个端点-》用最小矩阵将四个端点包围-》生成透视变换矩阵-》生成经过透视操作后的图片-》将图片的保存
def order_points(pts):
# 对坐标点进行排序,左上=》 右上=》左下=》右下 pts是很多个点,先求出左上角和右下角的点
rect = np.zeros((4, 2), dtype="float32")
# 按行排序
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# 纵坐标减去行坐标 得左下 右上
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
return rect
def point_distance(a, b):
return int(np.sqrt(np.sum(np.square(a - b))))
def src_dst(path_src, savepath):
name = path_src.split('/')[-1]
name =str(savepath)+'/'+name
"""前期处理,灰度-》二值化-》中值滤波-》膨胀-》腐蚀"""
image = cv2.imread(path_src)
srcHeight, srcWidth, channels = image.shape
image = cv2.resize(image, (int(srcWidth * 0.7), int(srcHeight * 0.7)))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
binary = cv2.medianBlur(gray, 3)
ret, binary = cv2.threshold(binary, 50, 255, cv2.THRESH_BINARY)
binary = cv2.dilate(binary, None, iterations=2)
img = cv2.erode(cv2.dilate(binary,None, iterations=2),None, iterations=2)
"""得到轮廓,筛选进而找到最大的轮廓"""
contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
list1 = []
for i in range(0,len(contours)):
p= contours[i].shape[0]
list1.append(p)
idx = list1.index(max(list1))
# 改变点几个的形状,-》n*2
contours[idx] =contours[idx].reshape(contours[idx].shape[0],contours[idx].shape[-1])
# 获得四个点集,就是轮廓的四个顶点
quard= order_points(contours[idx])
# 得到最小包围矩形,返回的值是,中心点坐标,长宽以及旋转角度
approxBox =cv2.minAreaRect(quard)
"""生成透视矩阵,得到透视后的图片"""
# 转换器矩形的四个端点的坐标
box = cv2.boxPoints(approxBox)
box = np.int0(box)
box = box.reshape(4, 2)
box = order_points(box)
# 待切割区域的原始位置,四边形的四个坐标
src_rect = order_points(quard)
w, h = point_distance(box[0], box[1]), point_distance(box[1], box[2])
# 生成透视变换矩阵
dst_rect = np.array([
[0, 0],
[w - 1, 0],
[w - 1, h - 1],
[0, h - 1]],
dtype="float32")
# 透视变换,生成变换视角
m = cv2.getPerspectiveTransform(src_rect, dst_rect)
# 得到透视变换后的图像
warped = cv2.warpPerspective(image, m, (w, h))
# 将变换后的结果图像写入png文
cv2.imwrite(name, warped, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
这里上传一个我自己整理的一个太阳能板数据集,数据来源主要是自己获取以及公开数据集的融合,供大家使用
**链接:https://pan.baidu.com/s/1r1rdu8OGJ1rhKpW9-CS_Tg
提取码:md1t**