numpy数组做图片拼接的实现(concatenate、vstack、hstack)
两种方法拼接
#img=np.vstack((img,img2))#vstack按垂直方向,hstack按水平方向 img=np.concatenate((img,img2),axis=0)#axis=0按垂直方向,axis=1按水平方向
统一图片大小,保证数组维度一致避免拼接失败。把图片全部调整成第一张图的宽高
defimg_size(image_names,width,height): foriinimage_names: img=cv2.imread(os.path.join(img_path,i)) img_resize=cv2.resize(img,(width,height),interpolation=cv2.INTER_CUBIC) cv2.imwrite(os.path.join(img_path,i),img_resize) print(os.path.join(img_path,i))
完整案例,拼接文件夹中的所有图片
importcv2 importos importnumpyasnp defimg_size(image_names,width,height): foriinimage_names: img=cv2.imread(os.path.join(img_path,i)) img_resize=cv2.resize(img,(width,height),interpolation=cv2.INTER_CUBIC) cv2.imwrite(os.path.join(img_path,i),img_resize) print(os.path.join(img_path,i)) if__name__=='__main__': img_path=r'F:\studytest' image_names=[namefornameinos.listdir(img_path)ifos.path.splitext(name)[1]==".jpg"] img1=cv2.imread(os.path.join(img_path,image_names[0])) width,height=img1.shape[:2][::-1] img_size(image_names,width,height) img=img1 foriinrange(1,len(image_names)): img_page=image_names[i] img2=cv2.imread(os.path.join(img_path,img_page)) #img=np.vstack((img,img2))#vstack按垂直方向,hstack按水平方向 img=np.concatenate((img,img2),axis=0)#axis=0按垂直方向,axis=1按水平方向 cv2.imwrite(os.path.join(img_path,"res.jpg"),img) #cv2.imshow("img",img) #cv2.waitKey() ``
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。