2016-07-30 35 views

回答

0

找到兩個圖像的中心。
評估該分區域並找出圍繞這些中心的相應指數。
使用這些索引來引用一個圖像並分配到另一個。
例如

%% example images 
    img1 = magic (100);     
    img2 = randi (10000, [80, 60]); 

%% 
    c1 = floor (size (img1)/2).'; % centre point of img1 as vector 
    c2 = floor (size (img2)/2).'; % centre point of img2 as vector 
    sub = [50, 30].';     % size of desired sub-region as vector 

%% safety first! 
    assert (sub <= size (img1).' && sub <= size (img2).', ... 
      'Subregion must be smaller than the images it is applied to'); 

%% 
    ind1 = [c1 - (sub/2), c1 + (sub/2)]; % corresponding indices for img1 
    ind2 = [c2 - (sub/2), c2 + (sub/2)]; % corresponding indices for img2 

%% create new image from img1, assign subregion from img2 in the right location 
    New = img1; 
    New( ind1(1,1) : ind1(1,2), ind1(2,1) : ind1(2,2) ) = ... 
     img2(ind2(1,1) : ind2(1,2), ind2(2,1) : ind2(2,2)); 

%% see the result 
    imagesc(New) 

enter image description here 理想的情況下創建,一般爲您完成此功能;最終 索引操作相當難看。

0
color_img = imread('test.jpg'); % read the first image 
[y, x, z] = size(color_img); % find the sizes 
rect = [(x/2)-50, (y/2)-50, 99, 99]; % form a rectangle for cropping 
I2 = imcrop(color_img,rect); % crop it 


another_image = imread('test1.jpg'); % read another image 
[y1, x1, z1]=size(another_image); % find the size of another image 
subrect = [(x1/2)-50, (y1/2)-50, 99, 99]; % form a rectangle for cropping 
subI2 = padarray(imcrop(another_image,subrect), [y1/2-50, x1/2-50]); % padd the zeros to cropped one to make main image and cropped image of equal size 
antimg = another_image - subI2; % make center area zeros in another image 
new = padarray(I2, [y1/2-50, x1/2-50]); % padd zeros to cropped one from first image to make it equal to size of another image 
new1 = imadd(new, antimg); % add cropped of older image to new image(which has zeros at center due to substraction) 
% show the result 
imshow(new1) 

該解決方案也適用於非等分分辨率。