# -*- coding:utf-8 -*-
import os
import numpy as np
import cv2
font = cv2.FONT_HERSHEY_SIMPLEX # 使用默认字体
def labelAndInfer(root, root1, root2, imgs):
for img in imgs:
if img.endswith('png'):
print(img)
img1 = cv2.imread(root1 + '/' + img)
img2 = cv2.imread(root2 + '/' + img)
h, w = img1.shape[:2]
image = np.zeros((h, w, 3), dtype=np.uint8) # label
image.fill(255)
xy1 = np.where(img1[:, :] < 250) # 返回满足像素值等于255的行列号,xy[0]为行,xy[1]为列
for i in range(len(xy1[0])):
image[xy1[0][i], xy1[1][i]] = (0, 0, 255)
cv2.imwrite(root + '/img/l' + img, image) # label红色
image1 = np.zeros((h, w, 3), dtype=np.uint8) # infer
image1.fill(255)
xy2 = np.where(img2[:, :] < 250)
for i in range(len(xy2[0])):
image1[xy2[0][i], xy2[1][i]] = (0, 255, 0)
image[xy2[0][i], xy2[1][i]] = (0, 255, 0)
cv2.imwrite(root + '/img/i' + img, image1) # infer绿色
for i in range(len(xy1[0])):
if all(image[xy1[0][i], xy1[1][i]] == [0, 255, 0]):
image[xy1[0][i], xy1[1][i]] = (0, 0, 0)
cv2.imwrite(root + '/' + img, image) # 黑色
def labelInInfer(root, root1, root2, imgs):
for img in imgs:
if img.endswith('-.jpg'):
print(img)
idx = img.split('-.')[0]
img1 = cv2.imread(root1 + '/' + img)
img2 = cv2.imread(root2 + '/img/' + idx + '+.jpg')
h, w = img1.shape[:2]
image = np.zeros((h, w, 3), dtype=np.uint8) # label红色
image.fill(255)
xy1 = np.where(img1[:, :] < 250)
for i in range(len(xy1[0])):
image[xy1[0][i], xy1[1][i]] = (0, 0, 255)
cv2.imwrite(root + '/img/li-lk' + idx + '.jpg', image)
image1 = np.zeros((h, w, 3), dtype=np.uint8)
image1.fill(255)
xy2 = np.where(img2[:, :] < 250)
for i in range(len(xy2[0])):
image1[xy2[0][i], xy2[1][i]] = (0, 255, 0)
image[xy2[0][i], xy2[1][i]] = (0, 255, 0)
cv2.imwrite(root + '/img/li-i+' + idx + '.jpg', image1)
for i in range(len(xy1[0])):
if all(image[xy1[0][i], xy1[1][i]] == [0, 255, 0]):
image[xy1[0][i], xy1[1][i]] = (0, 0, 0)
cv2.imwrite(root + '/li' + idx + '.jpg', image)
def inferInLabel(root, root1, root2, imgs):
for img in imgs:
if img.endswith('+.jpg'):
print(img)
idx = img.split('+.')[0]
img1 = cv2.imread(root1 + '/' + img)
img2 = cv2.imread(root2 + '/img/' + idx + '-.jpg')
h, w = img1.shape[:2]
image = np.zeros((h, w, 3), dtype=np.uint8) # 新建一个大小相等的图片
image.fill(255)
xy1 = np.where(img1[:, :] < 250) # 返回满足像素值等于255的行列号,xy[0]为行,xy[1]为列
for i in range(len(xy1[0])):
image[xy1[0][i], xy1[1][i]] = (0, 0, 255)
cv2.imwrite(root + '/img/il-l+' + idx + '.jpg', image)
image1 = np.zeros((h, w, 3), dtype=np.uint8) # 新建一个大小相等的图片
image1.fill(255)
xy2 = np.where(img2[:, :] < 250) # 返回满足像素值等于255的行列号,xy[0]为行,xy[1]为列
for i in range(len(xy2[0])):
image1[xy2[0][i], xy2[1][i]] = (0, 255, 0)
cv2.imwrite(root + '/img/il-ik' + idx + '.jpg', image1)
cv2.imwrite(root + '/il' + idx + '.jpg', image1/2+image/2)
def main():
root = 'F:/result/datasets/metirc' # label+^
root1 = 'F:/result/datasets/test_label' # label
model = {
0: 'datasets/infer', # 31.60% 29.98%
1: 'RuRes3/106', # 25.99% 51.69%
2: 'RuDense/097', # 22.75% 53.03%
3: 'double_scale4/120', # 22.51% 43.93%
4: 'RFB_PPD3_M5_Res2Net1/127', # 32.19% 38.74%
5: 'double_scale34/124',
6: 'DbCrackNet/094',
7: 'double_scale31/150',
}
root2 = 'F:/result/' + model[5] # 31.10 44.72
os.makedirs(root, exist_ok=True)
imgs = os.listdir(root1)
labelAndInfer(root, root1, root2, imgs) # 三种颜色label\infer
labelInInfer(root, root1, root2, imgs) # label骨架在infer增强
inferInLabel(root, root1, root2, imgs)
if __name__ == '__main__':
main()