转换补丁图像

时间:2016-07-13 12:40:49

标签: matlab image-processing deep-learning

我有一个代码,可以从图像的给定中心坐标中找到单个补丁。我想转换它,所以它应该找到大小为32X32和步幅为16的多个补丁。补丁应该来自图像,既不是来自边框,也不是来自它的背景。

centerCoord = [70,125];
patchSize = 32;
patchSourceImage = im(centerCoord(1)-ceil(patchSize/2):centerCoord(1)+ceil(patchSize/2)-1,centerCoord(2)-ceil(patchSize/2):centerCoord(2)+ceil(patchSize/2)-1);

2 个答案:

答案 0 :(得分:1)

你的代码行很大,即使它有效,我也建议让代码更容易,这样你就可以检查补丁的某些区域(我们称之为Regio Of Interest ROI)是否落在图像之外< / p>

示例:

im = rand(150, 150);    % 150x150 random "image"
[nrows, ncols] = size(im);

% center coordinates of 4 Patches
centerCoord = [50,50; 100,50; 50,100; 100,100];
patchSize = 32;

% results will be stored in matrix Patches
Patches = zeros(patchSize, patchSize, size(centerCoord,1));

for i=1:size(centerCoord,1)
    top_left = ceil(centerCoord(i,:) - [patchSize/2, patchSize/2]);
    bottom_right = top_left + [patchSize-1, patchSize-1];

    if any(top_left <= 0) || any((bottom_right - [nrows, ncols]) > 0)
        %some regions of the patch fall outside the image. Handle this case here.
    else
        Patches(:,:,i) = im(top_left(1):bottom_right(1), top_left(2):bottom_right(2));
    end
end

此处Patches是一个包含4个补丁的32x32x4矩阵。

答案 1 :(得分:-1)

im = imread('image98.jpeg');   
 % 150x150 random "image"
 [nrows, ncols] = size(im);

 % center coordinates of 4 Patches
 centerCoord = [75 125];
 patchSize = 32;

 % results will be stored in matrix Patches

 for r=1:16:nrows
     for c =1:16:ncols
     top_left = ceil(centerCoord(1) - [patchSize/2, patchSize/2]);
     bottom_right = top_left + [patchSize-1, patchSize-1];

     if any(top_left <= 0) || any((bottom_right - [nrows, ncols]) > 0)
         fprint('Outside');
     else
        patch = im(centerCoord(1)-ceil(patchSize/2):centerCoord(1)+ceil(patchSize/2)-1,centerCoord(2)- ceil(patchSize/2):centerCoord(2)+ceil(patchSize/2)-1);
         imshow(patch)
         centerCoord(2) =centerCoord(2)+16;
     end
      centerCoord(1) =centerCoord(1)+16
     end
 end