【问题标题】:How can I select the inner center square region of 100x100 pixels of an image in octave?如何以八度音阶选择图像的 100x100 像素的内部中心正方形区域?
【发布时间】:2016-07-30 08:21:36
【问题描述】:

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

【问题讨论】:

  • 请提供更多细节。

标签: matlab octave


【解决方案1】:

找到两个图像的中心。
评估子区域并找到中心周围的相应索引。
使用这些索引来引用一个图像并分配给另一个图像。
例如

%% 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)

理想情况下,创建一个通常为您执行此操作的函数;那场决赛 索引操作很丑。

【讨论】:

    【解决方案2】:
    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)
    

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

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      相关资源
      最近更新 更多