如何以八度为单位选择图像的100x100像素的内中心正方形区域?

时间:2016-07-30 08:21:37

标签: matlab octave

我想选择图像的内部中心区域并将其插入另一个图像的中心。

2 个答案:

答案 0 :(得分: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 理想情况下,创建一个为您提供此功能的功能;那个决赛 索引操作非常难看。

答案 1 :(得分: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)

此解决方案也适用于不相等的分辨率。