当前位置 : 主页 > 网络编程 > 其它编程 >

python将多张图融合与换成二值图

来源:互联网 收集:自由互联 发布时间:2023-07-02
把下面的图片融合黑色背景保持剩下的物体画在一起importosimportcv2importnumpyasnppatharD:\gxq\cod 把下面的图片融合黑色背景保持剩下的物体画在一起 import osimport cv2import numpy as nppathar"D:\gxq\co
把下面的图片融合黑色背景保持剩下的物体画在一起importosimportcv2importnumpyasnppatharD:\gxq\cod

把下面的图片融合黑色背景保持剩下的物体画在一起 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

import osimport cv2import numpy as nppathar"D:\gxq\code\weightlifting\WeightLifting\Competitions\31\45\l_line_BW"pathbr"D:\gxq\code\weightlifting\WeightLifting\Competitions\31\45\l_seg"pathcr"D:\gxq\code\weightlifting\WeightLifting\Competitions\31\61\700006253759"newpathr"D:\gxq\code\weightlifting\WeightLifting\Competitions\31\45\c"if not os.path.exists(newpath):os.mkdir(newpath)print(newpath)path_listaos.listdir(patha)path_lista.sort()path_lista_lenlen(path_lista)path_listbos.listdir(pathb)path_listb.sort()path_listb_lenlen(path_listb)path_listcos.listdir(pathc)path_listc.sort()path_listc_lenlen(path_listc)lenmin(path_lista_len,path_listb_len,path_listc_len)for k in range(0,len):src1cv2.imread(pathar"\\"path_lista[k])src1np.array(src1)src2cv2.imread(pathbr"\\"path_listb[k])src2np.array(src2)src3cv2.imread(pathcr"\\"path_listc[k])src3np.array(src3)wsrc1.shape[1]hsrc1.shape[0]print(w,h)for i in range(0,h):for j in range(0,w):b,g,rsrc2[i,j]if b!0 and g!0 and r!0:src1[i][j]src2[i][j]else:b,g,rsrc3[i,j]if b!0 and g!0 and r!0:src1[i][j]src3[i][j]cv2.imwrite(newpathr"\\img_"str(100000000k)".jpg",src1)#cv2.imshow(playground,src1)#cv2.waitKey()#cv.imwrite(newpathr\\portion[0].jpg,src1)

最后的效果如下 在这里插入图片描述 如要将上图变成二值效果即将黑色的不变剩余的全部换成白色即可

import osimport cv2import numpy as nppathar"D:\gxq\code\weightlifting\WeightLifting\Competitions\31\69\700006253759"newpathr"D:\gxq\code\weightlifting\WeightLifting\Competitions\31\69\1"if not os.path.exists(newpath):os.mkdir(newpath)print(newpath)path_listaos.listdir(patha)path_lista.sort()path_lista_lenlen(path_lista)for k in range(0,path_lista_len):src1cv2.imread(pathar"\\"path_lista[k])src1np.array(src1)wsrc1.shape[1]hsrc1.shape[0]print(w,h)for i in range(0,h):for j in range(0,w):b,g,rsrc1[i,j]#if not(all(src1[i,j][0,0,0])):if b!0 and g!0 and r!0:src1[i][j][255,255,255]cv2.imwrite(newpathr"\\img_"str(100000000k)".jpg",src1)

效果如下 在这里插入图片描述

网友评论