Matlab在图像上创建窗口

时间:2014-04-02 16:15:24

标签: matlab

我需要创建一个16X16窗口来扫描matlab中的整个图像,并在窗口中记录具有最大灰度级的像素的位置和灰度值。

任何人都可以指导我如何做到这一点?无法找到有关在图像上创建窗口的任何帮助。

由于

4 个答案:

答案 0 :(得分:2)

只是为了好玩,这是实现此目的的另一种方法:将窗口中最大值的强度和位置存储为复数的实部和虚部。然后,您可以使用nfilter函数执行移动过滤:

fun = @(x) complex(double(max(x(:))),double(find(x==max(x(:)), 1, 'first')));
B = nlfilter(YourImage,[16 16],fun);

然后,您可以从复杂地图访问最大值及其位置。以下是应用于this post中提供的图像之一的结果示例:

邻域中的最大强度(imagesc(real(B))):

enter image description here

最大值(imagesc(img(B)))的位置:

enter image description here

答案 1 :(得分:1)

你可以这样做:

% img = [matrix representing your image]

N = 16;

window = repmat(struct, ceil(size(img, 1) / N), ceil(size(img, 2) / N));
for row = 1:N:size(img, 1)
    for col = 1:N:size(img, 2)
        r = (row - 1) / N + 1;
        c = (col - 1) / N + 1;

        imgWindow = img(row:min(end,row+N-1), col:min(end,col+N-1));
        largest = max(imgWindow(:));
        [rLarg, cLarg] = find(imgWindow == largest, 1, 'first');

        window(r, c).largest = largest;
        window(r, c).row = rLarg + row - 1;
        window(r, c).col = cLarg + col - 1;
    end
end

您将有一个名为 window 的矩阵,其中窗口(r,c)包含有关窗口(r,c)的信息,其中包含以下字段:

  • 窗口(r,c).largest:最大像素的灰度级
  • 窗口(r,c).row,window(r,c).col:原始图像上最大像素的位置

答案 2 :(得分:1)

旧派for-Loop方法 -

%%// Outputs that you are interested in are - img, x1 and y1
img = rgb2gray(input_image); %%// Gray level values
x1 = zeros(size(img)); %%// Row values for the maximum pixel in the 16x16 window
y1 = zeros(size(img)); %%// Column values for the maximum pixel in the 16x16 window
for k1= 1:size(img,1)-15
    for k2= 1:size(img,2)-15
        img1 = img(k1:k1+15,k2:k2+15);        
        [val,ind1] = max(img1(:));
        img(k1+8,k2+8)=val; %%// Store the max grey value into the image
        [x1(k1,k2),y1(k1,k2)] = ind2sub([16 16],ind1);
    end
end

编辑1:要计算此滑动窗口的平均值,请使用此 -

window_size = 16; %%// Edit this to your window size

wsz = window_size-1;
mp = round(window_size/2);

%%// Outputs that you are interested in are - img, x1 and y1
img = rgb2gray(input_image); %%// Gray level values
x1 = zeros(size(img)); %%// Row values for the maximum pixel in the 16x16 window
y1 = zeros(size(img)); %%// Column values for the maximum pixel in the 16x16 window

img1 = img;
for k1= 1:size(img,1)-wsz
    for k2= 1:size(img,2)-wsz
        window_data = img(k1:k1+wsz,k2:k2+wsz);        
        val = round(mean(window_data(:)));
        img1(k1+mp,k2+mp)=val; %%// Store the mean grey value into the image
    end
end

figure,imshow(img1)

编辑2:

img1 = Z;
for k1= 1:size(Z,1)-wsz
    for k2= 1:size(Z,2)-wsz
        window_data = Z(k1:k1+wsz,k2:k2+wsz);        
        val = mean(window_data(:))
        if (val~=0)
            keyboard;
            error('Look, there is a non-zero mean value!');
        end
       % img1(k1+mp,k2+mp)=val; %%// Store the mean grey value into the image
        display(val);
    end
end

答案 3 :(得分:0)

您需要的关键步骤是在给定的扫描窗口(即矩形区域)中提取子图像。如果扫描窗口roi的格式为[x, y, width, height],则只需拨打imcrop

subImage = imcrop(Image, roi);

然后你可以在子图像中找到最大灰度级,就像这样

[value, location] = max(subImage(:));

当然,您需要更新扫描窗口,即roi,以扫描整个图像。