Sliding cropping and weighted stitching of images ----- based on matlab

Sliding cropping and weighted stitching of images ----- based on matlab


**When sliding and cropping images (moving window cropping), the repetition rate (that is, the sliding step) is often set. When using deep learning for data reconstruction, since image blocks with a repetitive rate are used, the repeated parts of the data need to be weighted during splicing. Use matlab to realize the method.
The specific parameters are:
cropping window size
step

slide cut section

close all;
clear;
clc;
img_path = 'E:\裁切拼接算法\test.tif';   %待裁切的影像
crop_image_directory = 'E:\裁切拼接算法\cropimgs\';   %输出路径
image = imread(img_path);
[h,w] = size(image);
k = 0;
for x = 1:4:h-23                  %窗口大小为24*24
    for y = 1:4:w-23             %从左到右,从上到下,步长均为4
        k=k+1;
        strk=num2str(k);
        str2 = [strk ,'.tif'];  
        tmp_img = image(x:x+23, y:y+23);
        str2 = strcat(crop_image_directory,str2);
        imwrite2tif(tmp_img,[],str2,'single','Copyright','MRI', 'Compression',1);
    end
end

insert image description here

weighted stitching part

The cropping order is from left to right, with a step size of 4; then from top to bottom, with a step size of 4. The
output image size is 144*248 and
the number of cropped image blocks is 1767.
The entire image is divided according to 24 and 24. ( step size is 24 ), except for the first column and the last column, every pixel in the middle is covered by 6 cropped image blocks. Take the average value of the pixel values ​​at the corresponding positions of the 6 image blocks.

close all;
clc;
clear;

crop_image_directory = 'G:\AMSR_LST_China\Day_S1_crop\20100517\all_pred';  % 待拼接影像块
mosaic_img_path = 'E:\裁切拼接算法\mosaicimgs\';   %输出路径
img_list = dir(strcat(crop_image_directory,'*.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= 25:4:w-23-3
        img2 = imread(strcat(crop_image_directory,img_list{n+1}));
        img3 = imread(strcat(crop_image_directory,img_list{n+2}));
        img4 = imread(strcat(crop_image_directory,img_list{n+3}));
        img5 = imread(strcat(crop_image_directory,img_list{n+4}));
        img6 = imread(strcat(crop_image_directory,img_list{n+5}));
        img7 = imread(strcat(crop_image_directory,img_list{n+6}));
        result(x:x+23,y:y+3) = (img2(:,21:24)...
            +img3(:,17:20)+img4(:,13:16)+img5(:,9:12)+img6(:,5:8)+img7(:,1:4))./6;
        n = n+1;
    end
    n = n + 7;%跳到下一行的第二列
end

% 第一列---最后一列
n = 1;
for x=1:4:h-23
    % 第一列 分为24/4==6个区域,每个区域权重不同
    img1 = imread(strcat(crop_image_directory,img_list{n}));
    img2 = imread(strcat(crop_image_directory,img_list{n+1}));
    img3 = imread(strcat(crop_image_directory,img_list{n+2}));
    img4 = imread(strcat(crop_image_directory,img_list{n+3}));
    img5 = imread(strcat(crop_image_directory,img_list{n+4}));
    img6 = imread(strcat(crop_image_directory,img_list{n+5}));
    result(x:x+23,1:4) = img1(:,1:4);
    result(x:x+23,5:8) = (img2(:,1:4)+img1(:,5:8))./2;
    result(x:x+23,9:12)= (img3(:,1:4)+img2(:,5:8)+img1(:,9:12))./3;
    result(x:x+23,13:16)= (img4(:,1:4)+img3(:,5:8)+img2(:,9:12)+img1(:,13:16))./4;
    result(x:x+23,17:20)=(img5(:,1:4)+img4(:,5:8)+img3(:,9:12)+img2(:,13:16)+img1(:,17:20))./5;
    result(x:x+23,21:24)=(img6(:,1:4)+img5(:,5:8)+img4(:,9:12)+img3(:,13:16)+img2(:,17:20)+img1(:,21:24))./6;
    
    % 最后一列 分为24/4==6个区域,每个区域权重不同
    img11 = imread(strcat(crop_image_directory,img_list{n+56}));
    img22 = imread(strcat(crop_image_directory,img_list{n+55}));
    img33 = imread(strcat(crop_image_directory,img_list{n+54}));
    img44 = imread(strcat(crop_image_directory,img_list{n+53}));
    img55 = imread(strcat(crop_image_directory,img_list{n+52}));
    img66 = imread(strcat(crop_image_directory,img_list{n+51}));
    result(x:x+23,225:228)=(img66(:,21:24)+img55(:,17:20)+img44(:,13:16)+img33(:,9:12)+img22(:,5:8)+img11(:,1:4))./6;
    result(x:x+23,229:232)=(img55(:,21:24)+img44(:,17:20)+img33(:,13:16)+img22(:,9:12)+img11(:,5:8))./5;
    result(x:x+23,233:236)=(img44(:,21:24)+img33(:,17:20)+img22(:,13:16)+img11(:,9:12))./4;
    result(x:x+23,237:240)=(img33(:,21:24)+img22(:,17:20)+img11(:,13:16))./3;
    result(x:x+23,241:244)=(img22(:,21:24)+img11(:,17:20))./2;
    result(x:x+23,245:248)=img11(:,21:24);
    %换到下一行
    n = n + 57;
end

str = strcat(mosaic_img_path,'拼接结果5','.tif');
imwrite2tif(result,[],str,'single','Copyright','MRI', 'Compression',1);

Guess you like

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