由于原图有白边，对预测结果有影响(除了预测出了裂缝，还包括上下的白边)，所以预测结果如下（prediction1）：

``````img1=cv.imread('738.png')#读取prediction1
print(img1.shape)
print(img2.shape)

r,img3=cv.threshold(img2,127,255,cv.THRESH_BINARY_INV)#将gt1反阈值化
print(img3.shape)
cv.imwrite('img3.png',img3)#保存``````

下面执行相加操作：prediction1+img3=img4

+       =

``````# img=cv.addWeighted(img1,1,img2,1)
cv.imwrite('img4.png',img4)``````

``````img5=img4#复制一份
l=np.where(img2<20)#找出黑色的坐标  判断条件只要大于0即可
print(l)
print(len(l))#为3，分别为 h w c``````

``````(array([  0,   0,   0, ..., 511, 511, 511], dtype=int64), array([  0,   0,   0, ..., 511, 511, 511], dtype=int64), array([0, 1, 2, ..., 0, 1, 2], dtype=int64))
3``````

``````index=list(zip(l[0],l[1],l[2]))
print(index)``````

输出如下：

把img4中相应位置变成黑色，代码如下：

``````for i in index:
img5[i]=0
cv.imwrite('img5.png',img5)``````

批量相加  完整代码如下：

``````import cv2 as cv
import numpy as np
import os
prediction=r'D:Crack_codemodecode-attention\U2Net-Attention\test_dataStone 331\U2Net_ECA_AS-pre\'#预测图片路径

prediction_name=[]
for f in os.listdir(path=prediction):
# print(f)
prediction_name.append(f.strip('png'))
print(prediction_name)

for  name in prediction_name:
# print(img1.shape)
# print(img2.shape)
r, img3 = cv.threshold(img2, 127, 255, cv.THRESH_BINARY_INV)#对mask取反