# -*- coding: utf-8 -*- import cv2 print("---------------------------------单幅图像读取-----------------------------") # 打印opencv版本 print(cv2.version.opencv_version) # 图像读取 img = cv2.imread('Figure_1.png') if img is None: print('Image read error!') else: # 图像显示 cv2.imshow('open text', img) # 等待读者操作 cv2.waitKey(0) # 窗口对象销毁 cv2.destroyWindow('open text')
=====================================================
import cv2 import numpy as np
print("----------------------单幅图像读取并多幅显示在同一窗口---------------------")
转载这位博主的代码地址:python opencv 同窗口显示多个图像_opencv一个窗口显示多张图_Coding的叶子的博客-CSDN博客
def show_imgs_multi(scale, imglist, order=None, border=10, border_color=(255, 0, 255)): """ :param scale: float 原图缩放尺寸 :param imglist: list 待显示图像序列 :param order: list or tuple 显示顺序 行*列 :param border: int 图像间隔的距离 :param border_color: 图像间隔颜色 :return: """ # step1:判断order是否为空 if order is None: order = [1, len(imglist)] # step2:将imglist重新赋值给一个新的列表 allimgs = imglist.copy() # step3: 创建两个新的列表存储图像集的宽和高 ws, hs = [], [] # step4:循环获取复制后的图像数量并改变图像维度及大小 for i, img in enumerate(allimgs): if np.ndim(i) == 2: # 主要作用是将图像从一种颜色空间转换为另一种颜色空间,包括RGB、灰度、HSV等颜色空间,以满足不同应用领域的需求 allimgs[i] = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # 主要作用是将待显示的图像重新调整宽和高,x轴,y轴 allimgs[i] = cv2.resize(img, dsize=(0, 0), fx=scale, fy=scale) ws.append(allimgs[i].shape[1]) hs.append(allimgs[i].shape[0]) # 创建两各变量存储最大宽和高 w = max(ws) h = max(hs) # 将待显示的图像拼接起来 sub = int(order[0] * order[1] - len(imglist)) ### 判断输入的显示格式与待显示图像数量的大小关系 if sub > 0: for s in sub: # np.zeros_like(a)的目的是构建一个与a同维度的数组,并初始化所有变量为零 allimgs.append(np.zeros_like(allimgs[0])) elif sub < 0: allimgs = allimgs[:sub] imgblank = np.zeros(((h+border) * order[0], (w+border) * order[1], 3)) + border_color imgblank = imgblank.astype(np.uint8) for i in range(order[0]): for j in range(order[1]): imgblank[(i * h + i * border):((i+1) * h + i * border), (j * w + j * border):((j+1) * w + j * border), :] = allimgs[i * order[1] + j] return imgblank if __name__ == '__main__': image = cv2.imread('Figure_1.png') img = show_imgs_multi(0.9, [image, image, image, image, image, image], (2,3)) cv2.namedWindow('multi', 0) cv2.imshow('multi', img) cv2.waitKey(0)
===============================================================
import cv2 import os import numpy as np print("---------------------------------图像读取全部显示-----------------------------") path = 'C:/Users/HASEE/Desktop/IMS/' # res = cv2.imreadmulti(filename=path, mats=list, flags='IMREAD_COLOR(1)') # 读取path文件夹下所有的名字 imglist = os.listdir(path) print(imglist) for img in imglist: if img.endswith('.jpg'): image = cv2.imread(path+img) cv2.imshow('pic', image) cv2.waitKey(2000) cv2.destroyAllWindows()