opencv image cropping and stitching

Discard not divisible part of the image is cut into a large-sized panel of m rows and n columns, the row and column of FIG relatively large storage panel in an image name

After Panel target detection target position marked

Then turn mosaic panels, paved enlarge

. 1  # Coding = UTF-. 8 
2  from the PIL Import Image
 . 3  # PIL Paste can image stitching 
. 4  Import CV2
 . 5  Import numpy AS NP
 . 6  Import glob AS glob
 . 7  Import OS
 . 8  "" " 
. 9  Input: image path (path + filename) , obtained by cutting the number of columns of small images, the number of rows (i.e., width, height)
 10  output: None
 . 11  "" " 
12 is  DEF crop_one_picture (path, filename, cols, rows):
 13 is      IMG cv2.imread = (filename,. 1)   # # reading a color image, the transparency (alpha channel) of the image is ignored, the default parameters; grayscale image; reading an original image, including the alpha channel; can be 0, -1, denoted 
14     = img.shape sum_rows [0]   # height 
15      sum_cols img.shape = [. 1]   # width 
16      the save_path + = path " \\ Crop Allows you. 1 {0}} _ {\\ " .format (cols, rows)   # Saved path 
. 17      IF  Not os.path.exists (the save_path):
 18 is          os.makdirs (the save_path)
 . 19      Print ( " crop image resulting column {0}, {1} image line. " .format (int (sum_cols / cols), int (sum_rows / rows)))
 20 is  
21 is      for I in Range (int (sum_cols / cols)):
 22 is          for Jin 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 is              # cv2.imwrite ( '.// // Origin-IMG _ {{0} } .jpg'.format. 1 (I, J), IMG [* J rows: (J +. 1) rows *, I * cols: (I +. 1) cols *]) 
27      Print ( " cut is completed, to give {0 } images. " .format (int (sum_cols / cols) * int (sum_rows / rows)))
 28      Print ( " file in 0} { " .format (the save_path))
 29  
30  
31 is  " "" traversal file folder an image format "" " 
32  DEF file_name (the root_path, PictureType):
 33 is      filename = []
 34 is      for the root, dirs, Filesin 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)

 

Merge Image:

. 1  # Coding = UTF-. 8 
2  from the PIL Import Image
 . 3  # PIL Paste can image stitching 
. 4  Import CV2
 . 5  Import numpy AS NP
 . 6  Import glob AS glob
 . 7  Import OS
 . 8  
. 9  "" " 
10  
. 11  Input: image path (path + filename), the number of columns of the image are cropped, the number of rows
 12  output: None
 13 is  "" " 
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 "" " Traverse a format picture file folder " "" 
49  DEF file_name (the root_path, PictureType):
 50      filename = []
 51 is      for the root, dirs, Files in os.walk (the root_path):
 52 is          for File in Files:
 53 is              IF os.path.splitext (File) [. 1] == PictureType:
 54 is                  filename.append (the os.path.join (the root, File))
 55      return filename
 56 is  
57 is  
58  
59 merge_path = " \\ \\ crop500_500 Origin-IMG. \\ "    # file to merge the folder where the small picture 
60  
61 merge_picture(merge_path)

Guess you like

Origin www.cnblogs.com/wind-chaser/p/12093767.html