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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM