Пример #1
0
    def cropImages_mp(self,
                      xRange,
                      yRange,
                      padding,
                      out_bands=[],
                      Process_cnt=10):
        """

        :param prefix_output:
        :param xRange:
        :param yRange:
        :param padding: overlap
        :param out_bands:
        :return:
        """
        czhUtils.gdal.UseExceptions()

        #get images in inputPath
        imgFiles = []
        czhUtils.getfilepath(self.inputPath, imgFiles)

        imgFiles_mp = mp.Manager().list()
        for file in natsorted(imgFiles):
            imgFiles_mp.append(file)
            # print(file)

        self.imgFiles = imgFiles_mp

        pool = mp.Pool(processes=Process_cnt)
        for i in range(Process_cnt):
            print(i)
            pool.apply_async(self.cropImages,
                             args=(xRange, yRange, padding, out_bands))
        pool.close()
        pool.join()
Пример #2
0
    def cropImages(self, xRange, yRange, padding, out_bands=[]):
        """

        :param prefix_output:
        :param xRange:
        :param yRange:
        :param padding: overlap
        :param out_bands:
        :return:
        """
        czhUtils.gdal.UseExceptions()

        #get images in inputPath
        imgFiles = []
        czhUtils.getfilepath(self.inputPath, imgFiles)
        cnt = 0
        if len(imgFiles) > 0:
            for imgFilePath in imgFiles:
                cnt += 1
                print("Processing: {} / {}, {}".format(cnt, len(imgFiles),
                                                       imgFilePath))
                rasters = czhUtils.gdal.Open(imgFilePath)
                srcArray = czhUtils.gdalnumeric.LoadFile(imgFilePath)
                img_Width = rasters.RasterXSize
                img_Height = rasters.RasterYSize
                geoTrans = rasters.GetGeoTransform()

                #get full path of file without ext
                filename, fileext = os.path.splitext(
                    os.path.basename(imgFilePath))

                #origin point world coordinate
                # originX = geoTrans[0]
                # originY = geoTrans[3]

                h0, w0 = 0, 0
                col, row = 1, 1
                while (h0 < img_Height):
                    while (w0 < img_Width):
                        #recalculate originX and originY
                        # originX,originY = czhUtils.pixeloffset2coord(geoTrans,w0,h0)
                        #crop image
                        # clip, pixel_ul_x, pixel_ul_y, geoTrans2 = self.cropImage(rasters,srcArray,originX,originY,xRange,yRange,out_bands)
                        clip, pixel_ul_x, pixel_ul_y, geoTrans2 = self.cropImage(
                            rasters, srcArray, w0, h0, xRange, yRange,
                            out_bands)
                        #save image
                        # rasterOutputPath = czhUtils.os.path.join(self.outputPath,filename +"_{}_{}".format(row,col)+fileext)
                        rasterOutputPath = czhUtils.os.path.join(
                            self.outputPath,
                            filename + "_{}_{}".format(row, col))

                        self.saveCropImage(rasters, clip, rasterOutputPath,
                                           pixel_ul_x, pixel_ul_y, geoTrans2)
                        #moving window along x-axil
                        w0 = w0 + xRange - padding
                        col = col + 1

                    #moving window alogn y_axil
                    h0 = h0 + yRange - padding
                    w0 = 0
                    col = 1
                    row = row + 1
Пример #3
0
    def mergeImages(self, image_dic, out_dic, t_w, t_h, n_w, n_h, overlap=0):
        pngs = []
        #(filename ,row ,col)
        fileSplits = []
        pathSplits = []

        czhUtils.getfilepath(image_dic, pngs, ('png', 'PNG'))
        for png in pngs:
            png_base, _ = czhUtils.os.path.splitext(
                czhUtils.os.path.basename(png))
            png_path = czhUtils.os.path.dirname(png)
            fileSplits.append(png_base.split('_'))
            pathSplits.append(png_path)

        #loop fileSplits
        fileNames = czhUtils.getUniqueValue(fileSplits, 1)

        #bug if n_w/t_w or n_h/t_h is exactly divisible
        # if (n_w+2*padding) % t_w ==0:
        if (n_w - overlap) % (t_w - overlap) == 0:
            # maxRows = math.floor((n_w+padding)/t_w)
            maxRows = math.floor((n_w - overlap) / (t_w - overlap))
        else:
            # maxRows = math.floor((n_w+padding)/t_w)+1
            maxRows = math.floor((n_w - overlap) / (t_w - overlap)) + 1

        # if (n_h+2*padding)%t_h ==0:
        if (n_h - overlap) / (t_h - overlap) == 0:
            # maxCols = math.floor((n_h+padding)/t_h)
            maxCols = math.floor((n_h - overlap) / (t_h - overlap))
        else:
            # maxCols = math.floor((n_h+padding)/t_h)+1
            maxCols = math.floor((n_h - overlap) / (t_h - overlap)) + 1

        for filename in fileNames:
            #reconstruct image
            #calculate rows and colsl
            rows = max([
                int(filesplit[1]) for filesplit in fileSplits
                if filesplit[0] == filename
            ])
            cols = max([
                int(filesplit[2]) for filesplit in fileSplits
                if filesplit[0] == filename
            ])
            bStart = True
            #topleft point
            # cur_l = 0
            # cur_t = 0

            for row in range(rows):
                # cur_l=padding
                cur_l = 0
                if row == 0:
                    # img_t = padding  #tile image
                    img_t = 0
                    cur_t = 0
                else:
                    # get image data in roi
                    cur_t = cur_t + t_h - img_t
                    if cur_t + t_h - img_t > n_h:
                        img_t = imgH - n_h + cur_t
                    else:
                        img_t = overlap

                for col in range(cols):
                    #check file exist
                    #get image width and height
                    try:
                        idx = fileSplits.index(
                            [filename, str(row + 1),
                             str(col + 1)])
                    except:
                        idx = -1
                    if idx != -1:
                        png_path = pathSplits[
                            idx] + "\\" + "{}_{}_{}.png".format(
                                filename, row + 1, col + 1)
                        img = cv2.imread(png_path)
                        imgH = img.shape[0]
                        imgW = img.shape[1]
                        assert imgH == t_h
                        assert imgW == t_w

                        channels = img.shape[2]
                        dType = img.dtype
                        if bStart:
                            dst = czhUtils.np.zeros((n_h, n_w, channels),
                                                    dtype=dType)
                            bStart = False

                        #calculate actual valid image left top point
                        if col == 0:
                            # img_l = padding
                            img_l = 0
                        else:
                            #get image data in roi
                            cur_l = cur_l + t_w - img_l
                            if cur_l + t_w - img_l > n_w:
                                img_l = imgW - n_w + cur_l
                            else:
                                img_l = overlap
                        roi = img[img_t:imgH, img_l:imgW, :]
                        dst[cur_t:cur_t + imgH - img_t,
                            cur_l:cur_l + imgW - img_l] = roi

            #write image file if possible export tif with tfw file.
            cv2.imwrite(out_dic + "\\" + filename + ".tif", dst)