Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何防止在将图像分割为块时超过矩阵维数?_Image_Matlab_Image Processing_Image Segmentation - Fatal编程技术网

Image 如何防止在将图像分割为块时超过矩阵维数?

Image 如何防止在将图像分割为块时超过矩阵维数?,image,matlab,image-processing,image-segmentation,Image,Matlab,Image Processing,Image Segmentation,我有一个图像,我想分为重叠块 我已经将框的大小设置为8行8列,重叠因子设置为4行/列 这是我为解决这个问题而写的: img = imread('a03-017-05.png'); overlap = 4 count = 1; for i = 1:overlap:size(img,1) for j = 1:overlap:size(img,2) new{count} = img(i:i+8,j:j+8); count = count+1; end e

我有一个图像,我想分为重叠块

我已经将框的大小设置为8行8列,重叠因子设置为4行/列

这是我为解决这个问题而写的:

img = imread('a03-017-05.png');
overlap = 4
count = 1;
for i = 1:overlap:size(img,1)
    for j = 1:overlap:size(img,2)
        new{count} = img(i:i+8,j:j+8);
        count = count+1;
    end
end
此操作一直有效,直到到达图像的末尾,
j+8
i+8
将超出图像的尺寸。有没有办法以最小的数据丢失来避免这种情况


谢谢

如果您只是想忽略位于完整子块之外的列/行,只需从相应的循环范围中减去子块的宽度/高度:

overlap = 4
blockWidth = 8;
blockHeight = 8;
count = 1;
for i = 1:overlap:size(img,1) - blockHeight + 1
    for j = 1:overlap:size(img,2) - blockWidth + 1
        ...
假设您的图像是16x16。从列
8开始的块将说明列的其余部分,因此起始索引在9和16之间是没有意义的

另外,我认为您的示例错误地计算了块大小。。。你得到的积木是9x9。我想你想做:

        new{count} = img(i:i+blockHeight-1,j:j+blockWidth-1);

例如,在具有上述代码的13x13图像中,行索引将为
[1,5]
,块的行范围将为
1:8
5:12
。第13行/第13列将被忽略。

如果您只想忽略位于完整子块之外的列/行,只需从相应的循环范围中减去子块的宽度/高度:

overlap = 4
blockWidth = 8;
blockHeight = 8;
count = 1;
for i = 1:overlap:size(img,1) - blockHeight + 1
    for j = 1:overlap:size(img,2) - blockWidth + 1
        ...
假设您的图像是16x16。从列
8开始的块将说明列的其余部分,因此起始索引在9和16之间是没有意义的

另外,我认为您的示例错误地计算了块大小。。。你得到的积木是9x9。我想你想做:

        new{count} = img(i:i+blockHeight-1,j:j+blockWidth-1);

例如,在具有上述代码的13x13图像中,行索引将为
[1,5]
,块的行范围将为
1:8
5:12
。第13行/第13列将被忽略。

如果您只想忽略位于完整子块之外的列/行,只需从相应的循环范围中减去子块的宽度/高度:

overlap = 4
blockWidth = 8;
blockHeight = 8;
count = 1;
for i = 1:overlap:size(img,1) - blockHeight + 1
    for j = 1:overlap:size(img,2) - blockWidth + 1
        ...
假设您的图像是16x16。从列
8开始的块将说明列的其余部分,因此起始索引在9和16之间是没有意义的

另外,我认为您的示例错误地计算了块大小。。。你得到的积木是9x9。我想你想做:

        new{count} = img(i:i+blockHeight-1,j:j+blockWidth-1);

例如,在具有上述代码的13x13图像中,行索引将为
[1,5]
,块的行范围将为
1:8
5:12
。第13行/第13列将被忽略。

如果您只想忽略位于完整子块之外的列/行,只需从相应的循环范围中减去子块的宽度/高度:

overlap = 4
blockWidth = 8;
blockHeight = 8;
count = 1;
for i = 1:overlap:size(img,1) - blockHeight + 1
    for j = 1:overlap:size(img,2) - blockWidth + 1
        ...
假设您的图像是16x16。从列
8开始的块将说明列的其余部分,因此起始索引在9和16之间是没有意义的

另外,我认为您的示例错误地计算了块大小。。。你得到的积木是9x9。我想你想做:

        new{count} = img(i:i+blockHeight-1,j:j+blockWidth-1);

例如,在具有上述代码的13x13图像中,行索引将为
[1,5]
,块的行范围将为
1:8
5:12
。第13行/第13列将被省略。

我不确定您希望如何安排它们,但我做了一个棋盘格图案,即

x [] x [] ...
[] x [] x ...
所以要做到这一点

%The box size
k = 8;
%The overlap
overlap = 4;

%One layer of the checker board
new1 = mat2cell(img, [k*ones(1, floor(size(img,1)/k)), mod(size(img,1), k)], [k*ones(1, floor(size(img,2)/k)), mod(size(img,2), k)]);

%Get the overlap cells
img2 = img(overlap + 1:end - overlap, overlap + 1:end - overlap);

%Create the other part of the checker board
new2 = mat2cell(img2, [k*ones(1, floor(size(img2,1)/k)), mod(size(img2,1), k)], [k*ones(1, floor(size(img2,2)/k)), mod(size(img2,2), k)]);

%They will all end up in new
new = cell(size(new1) + size(new2));
new(1:2:end, 1:2:end) = new1;
new(2:2:end, 2:2:end) = new2;

我不知道你想怎么安排,但我做了一个棋盘格图案

x [] x [] ...
[] x [] x ...
所以要做到这一点

%The box size
k = 8;
%The overlap
overlap = 4;

%One layer of the checker board
new1 = mat2cell(img, [k*ones(1, floor(size(img,1)/k)), mod(size(img,1), k)], [k*ones(1, floor(size(img,2)/k)), mod(size(img,2), k)]);

%Get the overlap cells
img2 = img(overlap + 1:end - overlap, overlap + 1:end - overlap);

%Create the other part of the checker board
new2 = mat2cell(img2, [k*ones(1, floor(size(img2,1)/k)), mod(size(img2,1), k)], [k*ones(1, floor(size(img2,2)/k)), mod(size(img2,2), k)]);

%They will all end up in new
new = cell(size(new1) + size(new2));
new(1:2:end, 1:2:end) = new1;
new(2:2:end, 2:2:end) = new2;

我不知道你想怎么安排,但我做了一个棋盘格图案

x [] x [] ...
[] x [] x ...
所以要做到这一点

%The box size
k = 8;
%The overlap
overlap = 4;

%One layer of the checker board
new1 = mat2cell(img, [k*ones(1, floor(size(img,1)/k)), mod(size(img,1), k)], [k*ones(1, floor(size(img,2)/k)), mod(size(img,2), k)]);

%Get the overlap cells
img2 = img(overlap + 1:end - overlap, overlap + 1:end - overlap);

%Create the other part of the checker board
new2 = mat2cell(img2, [k*ones(1, floor(size(img2,1)/k)), mod(size(img2,1), k)], [k*ones(1, floor(size(img2,2)/k)), mod(size(img2,2), k)]);

%They will all end up in new
new = cell(size(new1) + size(new2));
new(1:2:end, 1:2:end) = new1;
new(2:2:end, 2:2:end) = new2;

我不知道你想怎么安排,但我做了一个棋盘格图案

x [] x [] ...
[] x [] x ...
所以要做到这一点

%The box size
k = 8;
%The overlap
overlap = 4;

%One layer of the checker board
new1 = mat2cell(img, [k*ones(1, floor(size(img,1)/k)), mod(size(img,1), k)], [k*ones(1, floor(size(img,2)/k)), mod(size(img,2), k)]);

%Get the overlap cells
img2 = img(overlap + 1:end - overlap, overlap + 1:end - overlap);

%Create the other part of the checker board
new2 = mat2cell(img2, [k*ones(1, floor(size(img2,1)/k)), mod(size(img2,1), k)], [k*ones(1, floor(size(img2,2)/k)), mod(size(img2,2), k)]);

%They will all end up in new
new = cell(size(new1) + size(new2));
new(1:2:end, 1:2:end) = new1;
new(2:2:end, 2:2:end) = new2;


因此,如果您有一个包含13列的图像,而忽略最后一列?您是否尝试过
img(i:end,j:end)
?实际上,示例中的框大小是9x9…如果可用,使用此功能:@Daniel
blockproc
是否允许您重叠块?因此,如果您有一个包含13列的图像,您将忽略最后一列?您是否尝试过
img(i:end,j:end)
?实际上,示例中的框大小是9x9…如果可用,使用此功能:@Daniel
blockproc
是否允许您重叠块?因此,如果您有一个包含13列的图像,您将忽略最后一列?您是否尝试过
img(i:end,j:end)
?实际上,示例中的框大小是9x9…如果可用,使用此功能:@Daniel
blockproc
是否允许您重叠块?因此,如果您有一个包含13列的图像,您将忽略最后一列?您是否尝试过
img(i:end,j:end)
?实际上,示例中的框大小是9x9…如果可用,使用此功能:@Daniel
blockproc
允许您重叠块吗?谢谢,这就是我要找的。这只是一个从for循环中减去块大小的简单问题,但它只是没有点击。另外,你说的块大小比1大是对的,谢谢你指出这一点。谢谢你,这就是我要找的。这只是一个从for循环中减去块大小的简单问题,但它只是没有点击。另外,你说的块大小比1大是对的,谢谢你指出这一点。谢谢你,这就是我要找的。这只是一个从for循环中减去块大小的简单问题,但它只是没有点击。另外,你说的块大小比1大是对的,谢谢你指出这一点。谢谢你,这就是我要找的。这只是一个从for循环中减去块大小的简单问题,但它只是没有点击。另外,你说的块大小比1大是对的,谢谢你指出这一点非常感谢,那真的很有帮助非常感谢,那真的很有帮助非常感谢,那真的很有帮助非常感谢,那真的很有帮助