python—opencv批量截取图像指定区域

版权声明:转载请说明来源,谢谢 https://blog.csdn.net/wsp_1138886114/article/details/82778409

一、批量读取图像并截取所需区域

import os
import cv2 

for i in range(1,201):
    if i==169 or i==189:
        i = i+1
    path = "C:\\Users\\"+str(i)+".png"
    image = cv2.imread(path)               #从指定路径读取图像
    cropImg = image[600:1200,750:1500]    #获取感兴趣区域
    cv2.imwrite("C:\\Users\\temp\\"+str(i)+".png",cropImg)  #保存到指定目录

二、指定图像位置的裁剪处理

import os   
import cv2 
 
# 遍历指定目录,显示目录下的所有文件名
def CropImage4File(filepath,destpath):
    pathDir =  os.listdir(filepath)    # 列出文件路径中的所有路径或文件
    for allDir in pathDir:
        child = os.path.join(filepath, allDir)
        dest = os.path.join(destpath,allDir)
        if os.path.isfile(child):
        	image = cv2.imread(child) 
            sp = image.shape            #获取图像形状:返回【行数值,列数值】列表
            sz1 = sp[0]                 #图像的高度(行 范围)
            sz2 = sp[1]                 #图像的宽度(列 范围)
            #sz3 = sp[2]                #像素值由【RGB】三原色组成
            
            #你想对文件的操作
            a=int(sz1/2-64) # x start
            b=int(sz1/2+64) # x end
            c=int(sz2/2-64) # y start
            d=int(sz2/2+64) # y end
            cropImg = image[a:b,c:d]   #裁剪图像
            cv2.imwrite(dest,cropImg)  #写入图像路径
           
if __name__ == '__main__':
    filepath ='F:\\\maomi'             #源图像
    destpath='F:\\maomi_resize'        # resized images saved here
    CropImage4File(filepath,destpath)

猜你喜欢

转载自blog.csdn.net/wsp_1138886114/article/details/82778409