opencv图像裁剪与拼接

舍弃不够整除的部分,对大尺寸的图像裁剪成m行n列的小图,将小图相对大图的行列位置存储在图像名中

之后对小图进行目标检测标注目标位置

再将小图依次拼接,铺成大图

 1 # coding=utf-8
 2 from PIL import Image
 3 # pil paste可以进行图片拼接
 4 import cv2
 5 import numpy as np
 6 import glob as glob
 7 import os
 8 """
 9 输入:图片路径(path+filename),裁剪获得小图片的列数、行数(也即宽、高)
10 输出:无
11 """
12 def crop_one_picture(path, filename, cols, rows):
13     img = cv2.imread(filename,1)  ##读取彩色图像,图像的透明度(alpha通道)被忽略,默认参数;灰度图像;读取原始图像,包括alpha通道;可以用1,0,-1来表示
14     sum_rows = img.shape[0]  # 高度
15     sum_cols = img.shape[1]  # 宽度
16     save_path = path + "\\crop{0}_{1}\\".format(cols, rows)  # 保存的路径
17     if not os.path.exists(save_path):
18         os.makedirs(save_path)
19     print("裁剪所得{0}列图片,{1}行图片.".format(int(sum_cols / cols), int(sum_rows / rows)))
20 
21     for i in range(int(sum_cols / cols)):
22         for j in range(int(sum_rows / rows)):
23             print(save_path+str(os.path.splitext(filename)[0].split("\\")[-1]) + '_' + str(j) + '_' + str(i) + '.jpg')
24             cv2.imwrite(
25                 save_path + str(os.path.splitext(filename)[0].split("\\")[-1]) + '_' + str(j) + '_' + str(i) + '.jpg', img[j * rows:(j + 1) * rows, i * cols:(i + 1) * cols, :])
26             #cv2.imwrite('.//origin-img//{0}_{1}.jpg'.format(i,j), img[j * rows:(j + 1) * rows, i * cols:(i + 1) * cols])
27     print("裁剪完成,得到{0}张图片.".format(int(sum_cols / cols) * int(sum_rows / rows)))
28     print("文件保存在{0}".format(save_path))
29 
30 
31 """遍历文件夹下某格式图片"""
32 def file_name(root_path,picturetype):
33     filename=[]
34     for root,dirs,files in os.walk(root_path):
35         for file in files:
36             if os.path.splitext(file)[1]==picturetype:
37                 filename.append(os.path.join(root,file))
38     return filename
39 
40 root_path='.\\origin-img\\'
41 filenamelist=file_name(root_path,'.jpg')
42 
43 print(filenamelist)
44 each_name_list=[]
45 for each_name in filenamelist:
46     each_name_list.append(each_name.split("\\")[-1])
47 print(each_name_list)  # final name
48 w=500
49 h=500
50 for each_img in each_name_list:
51     crop_one_picture(root_path,each_name,w,h)

 

合并图像:

 1 # coding=utf-8
 2 from PIL import Image
 3 # pil paste可以进行图片拼接
 4 import cv2
 5 import numpy as np
 6 import glob as glob
 7 import os
 8 
 9 """
10 
11 输入:图片路径(path+filename),裁剪所的图片的列的数量、行的数量
12 输出:无
13 """
14 def merge_picture(merge_path):
15     filename=file_name(merge_path,".jpg")
16     shape=cv2.imread(filename[0],1).shape    #三通道的影像需把-1改成1
17     cols=shape[1]
18     rows=shape[0]
19     channels=shape[2]
20 
21 
22     max_cols_th = 0
23     max_rows_th = 0
24     for i in range(len(filename)):
25         img=cv2.imread(filename[i],1)
26         cols_th=int(filename[i].split("_")[-1].split('.')[0])
27         if cols_th>max_cols_th:
28             max_cols_th=cols_th
29         rows_th=int(filename[i].split("_")[-2])
30         if rows_th>max_rows_th:
31             max_rows_th=rows_th
32     print(max_rows_th,max_cols_th)
33     num_of_cols=max_cols_th+1
34     num_of_rows=max_rows_th+1
35 
36 
37     dst=np.zeros((rows*num_of_rows,cols*num_of_cols,channels),np.uint8)
38     for i in range(len(filename)):
39         img=cv2.imread(filename[i],1)
40         cols_th=int(filename[i].split("_")[-1].split('.')[0])
41         rows_th=int(filename[i].split("_")[-2])
42         print(rows_th,cols_th)
43         roi=img[0:rows,0:cols,:]
44 
45         dst[rows_th*rows:(rows_th+1)*rows,cols_th*cols:(cols_th+1)*cols,:]=roi
46     cv2.imwrite(merge_path+"merge.jpg",dst)
47 
48 """遍历文件夹下某格式图片"""
49 def file_name(root_path,picturetype):
50     filename=[]
51     for root,dirs,files in os.walk(root_path):
52         for file in files:
53             if os.path.splitext(file)[1]==picturetype:
54                 filename.append(os.path.join(root,file))
55     return filename
56 
57 
58 
59 merge_path=".\\origin-img\\crop500_500\\"   #要合并的小图片所在的文件夹
60 
61 merge_picture(merge_path)
上一篇:layui的table渲染cols的标题错乱问题,number转成string


下一篇:剑指offer-面试题12-矩阵中的路径-回溯法