Cut 2

import cv2
import numpy as np
import os
import shutil
def clip(image,overlap,nh,nw):
h,w,c=image.shape
y_step=nh-overlap
x_step=nw-overlap

rois=[]
a=[]

for row in range(0,h,y_step):
    for col in range(0,w,x_step):
        if row==0 and col==0:
            rois.append(image[row:row+nh,col:col+nw,:])
            a.append((row,col))
        if row==0 and col>0:

            if (col+nw)>=w:
                ww=w-col
            else:
                ww=nw
            if ww<=0:
                continue
            if ww >= 416:
                rois.append(image[row:row+nh,col:col+ww,:])
                a.append((row, col))
           # print((row,col))

        if row>0 and col==0:


            if (row+nw)>=h:
                hh=h-row
            else:
                hh=nh
            if hh<=0:
                continue
            if  hh >=416:

                rois.append(image[row:row+hh,col:col+nw,:])
                a.append((row, col))
           # print((row,col))

        if row>0 and col >0:


            if (col+nw)>=w:
                ww=w-col
            else:
                ww=nw
            if (row+nh)>=h:
                hh=h-row
            else:
                hh=nh

            if ww<=0 or hh<=0:
                continue
            if ww>=416 and hh>=416:
                rois.append(image[row:row+hh,col:col+ww,:])
                a.append((row, col))

            #print((row,col))

# print(a)

for i, val in enumerate(a):

    print('序号:%s  值:%s'%(i,val))

return np.asarray(rois)

if name == ‘main’:

if os.path.exists('D:/cut_43/cut_D43_CODE/data/1/'):
    shutil.rmtree('D:/cut_43/cut_D43_CODE/data/1/')
os.mkdir('D:/cut_43/cut_D43_CODE/data/1/')


path = r"D:/cut_43/cut_D43_CODE/data/0/"
filelist = os.listdir(path)
for image1 in filelist:
    image2 = image1.split('.')[0]
    if os.path.exists('D:/cut_43/cut_D43_CODE/data/1/' + str(image2)):
        shutil.rmtree('D:/cut_43/cut_D43_CODE/data/1/' + str(image2))

    os.mkdir('D:/cut_43/cut_D43_CODE/data/1/' + str(image2))

    adr = path + image1
    image = cv2.imread(adr)

   # imgs = clip(image, 200, 800, 800)
    imgs=clip(image,150,800,800)


    for i in range(len(imgs)):
        # print(i)


        cv2.imwrite('D:/cut_43/cut_D43_CODE/data/1/'+str(image2) +'/'+ str(i) + '.jpg',
                    imgs[i])
Published 41 original articles · won praise 7 · views 3684

Guess you like

Origin blog.csdn.net/weixin_43091087/article/details/103487064
cut