Batch cropping and weighted splicing method of remote sensing images----Based on Matlab

Batch cropping and weighted splicing method of remote sensing images----Based on Matlab


In the field of remote sensing + deep learning , it is often necessary to batch crop larger-sized remote sensing images to create sample sets. The main parameters during the cutting process are:

  • crop size
  • step size
  • Edge cutting method (if complete cutting is not possible, no cutting, zero-fill cutting, or direction-changing cutting)

After cropping, the image blocks predicted by the model need to be spliced. If the cropping step is set during the cropping process , adjacent image blocks will overlap. At this time, in the stitching process, it is often necessary to perform weighted stitching on the repeated areas. The image overlap situation in the edge area is more complicated and needs to be discussed on a case-by-case basis.
This article uses matlab to organize the methods of batch cropping and weighted splicing. Taking the 143 * 248 * 1 image as an example, you can change the parameters for images of different sizes.

Cutting method

Cropping can be done using a two-layer for loop.
Notice:

  • If you can't crop it completely, you can fill the edges of the original image with all zeros.
  • The step length is divided into the step length for left and right cutting and the step length for up and down cutting. Generally, the two are equal.

Take 143 * 248 single-channel image as an example.

  • Cut Size: 24
  • Slide step: 4

Width 143 cannot be completely cut, you can add a line of 0, 144 can be completely cut.
There are 1767 cropped image blocks in total, (57 * 31).
The average stitching of 1767 cropped image blocks is performed below.

close all;
clear;
clc;
%批量裁剪代码

file_path = '';%待裁切影像数据文件夹
crop_file_dir = ''; %裁切结果文件夹

img_path_list = dir(strcat(file_path,'*.tif'));
img_num = length(img_path_list);
for j = 1:1:img_num  %大循环,遍历每一张影像
    image_name = img_path_list(j).name;
    image = imread(strcat(file_path,image_name));
    % 为每幅待裁切的影像创建一个文件夹,存放该影像的裁剪结果
    crop_image_directory = strcat(crop_file_dir,image_name);
    if exist(crop_image_directory,'dir')
        rmdir(crop_image_directory,'s');
    end
    mkdir(crop_image_directory);
    [h,w] = size(image);
    k = 0;
    % 添加10,使完全裁切  248 * 14424*24裁切,步长为4
    add_0 = zeros(1,248);
    img = [image;add_0];
    for x = 1:4:h-23
        for y = 1:4:w-23
            k=k+1;
            tmp_img = image(x:x+23, y:y+23);
            str2 = strcat(crop_image_directory,'\',num2str(k),'.tif');
            imwrite2tif(tmp_img,[],str2,'single','Copyright',...
                'MRI', 'Compression',1);
        end
    end
end

weighted stitching method

The stitching of 1767 image blocks, due to the use of sliding cropping, each pixel of the original image is covered by multiple cropped image blocks, and the situation is more complicated.
This paper adopts a method of adding first and then averaging .

  • First, in the order of cropping, the cropped image blocks are superimposed on the 0 matrix.
  • Secondly, average by region

Take the original size of 144*248, the cutting step length 4, the cutting size 24, and the cutting quantity 1767 as an example.
Let’s do a simple sum first:

    %叠加
    for x = 1:4:h-23
        for y = 1:4:w-23
            img = imread(strcat(img_folder,img_list{
    
    n}));
            result(x:x+23,y:y+23) = result(x:x+23,y:y+23)+img;
            n = n+1;
        end
    end 

Discuss on a case-by-case basis.
The whole is divided into three areas.

  • four corner area
  • four side regions
  • an inner area
    insert image description here

Among them, the corner area and side area are as shown below. The number represents how many cropped image blocks there are on this pixel, that is, how many should be divided by for averaging.
Each pixel in the middle area is covered by 36 image blocks, just divide by 36.
insert image description here
The splicing code is as follows:
Note
Since the cropped image blocks are named as 1.tif 2.tif ...
you need to pay attention to the order when reading, and sort the files in folder_list = dir(dir_path).

close all;
clc;
clear;
% 加权拼接方式
% 思路:先将所有影像块叠加到0矩阵上(相加方式),然后分区域进行平均取值!
dir_path = 'E:\delete\crop4\';%裁剪文件夹目录
out_path = 'E:\delete\weight_pinjie\';
mask = double(imread('E:\delete\china_mask2.tif'));
folder_list = dir(dir_path);
folder_list(1) = [];folder_list(1) = [];

for date = 1:length(folder_list)% 第二天开始
    img_date = folder_list(date).name;
    img_folder = strcat(dir_path,img_date,'\all_pred\'); 
    img_list = dir(strcat(img_folder,'*.tif'));
    num = length(img_list);
    nameCell = cell(length(img_list),1);
    for i = 1:length(img_list)
        nameCell{
    
    i} = img_list(i).name;
    end
    img_list = sort_nat(nameCell);% 排序
    h = 144;
    w = 248;
    result = zeros(h,w);
    n = 1;
    %叠加
    for x = 1:4:h-23
        for y = 1:4:w-23
            img = imread(strcat(img_folder,img_list{
    
    n}));
            result(x:x+23,y:y+23) = result(x:x+23,y:y+23)+img;
            n = n+1;
        end
    end 
    %按区域进行平均 
    % 分为四个角、四条边、一个中  共三部分。除了中部,每个部分再切分为6种情况
    %%四个顶
    %1(1:24,1:24)2(1:24,225,248)3(121:144,1:24)4(121:144,225:248)
    %%四条边
    %上边(1:24,25:224)
    %下边(121:144,25:224)
    %左边(25:120,1:24)
    %右边(25:120,225:248)
    %%一个中
    %中部(25:120,25:224)
    
    %% 四个角(只需考虑一个右上(1:24,225,248)%只需考虑一个右上(1:24,225,248)
    x = 1;
    for weight = 6:-1:1
        w1 = weight*1;
        w2 = weight*2;
        w3 = weight*3;
        w4 = weight*4;
        w5 = weight*5;
        w6 = weight*6;
        
        result(1:4,224+x:224+x+3) = result(1:4,224+x:224+x+3)./w1;
        result(5:8,224+x:224+x+3) = result(5:8,224+x:224+x+3)./w2;
        result(9:12,224+x:224+x+3) = result(9:12,224+x:224+x+3)./w3;
        result(13:16,224+x:224+x+3) = result(13:16,224+x:224+x+3)./w4;
        result(17:20,224+x:224+x+3) = result(17:20,224+x:224+x+3)./w5;
        result(21:24,224+x:224+x+3) = result(21:24,224+x:224+x+3)./w6;
        x = x+4;
    end
    %% 四条边,每一条边6种情况。分别除以 6 12 18 24 30 36
    x=1;
    for weight=6:6:36
        result(x:x+3,25:224) = result(x:x+3,25:224)./weight;   %result(120+x:120+x+3,25:224) = result(120+x:120+x+3,25:224)./(42-weight);%result(25:120,x:x+3) =  result(25:120,x:x+3)./weight; %result(25:120,224+x:224+x+3) =  result(25:120,224+x:224+x+3)./(42-weight); % 左
        x=x+4;
    end
    %% 一个中
    result(25:120,25:224) = result(25:120,25:224)./36;
    result = result.*mask;
    result(result==0) =NaN; 
    str = strcat(out_path,img_date,'_average2.tif');% 恢复到143*248,最后一行无用为0
    imwrite2tif(result,[],str,'single','Copyright','MRI', 'Compression',1);
end

The idea is the same for other image situations, just change the corresponding parameters. The global weighted average method can better remove splicing traces and improve the visual effect of the spliced ​​image.

The global weighted average is just a splicing method, and many methods can also be used. For example, ignoring the splicing method of edges, for repeated image blocks, only the non-repeated parts in the middle are used for splicing.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_51205206/article/details/128063671