如何计算图像中重叠块的数量 [英] How to count the number of overlapping blocks in an Image

查看:114
本文介绍了如何计算图像中重叠块的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图像尺寸为512X512,并且为整个图像制作了4x4的重叠块.如何计算重叠块的数量并将其保存在matlab中的数组中. 对于4x4重叠的块,我已经完成了以下操作.现在,如何计算块数并使用数组进行存储.

I have a 512X512 size of image and I have made 4x4 overlapping blocks for the entire image.How can i count the number of overlapping blocks and save it in an Array in matlab. I have done like below for 4x4 overlapping blocks. Now how to count the no of blocks and store it using an Array.

[e f] = size(outImg);
l=0;
for i=2:e-2
    for j=2:f-2
        H =double(outImg((i-1:i+2),(j-1:j+2)));
        eval(['out_' num2str(l) '=H']);
        l=l+1
    end;
end;

推荐答案

据我了解的问题,您想知道图像中可以容纳多少4x4块,然后将其存储.

From what I understand the question, you want to know how many blocks of 4x4 can fit in the image, and then store them.

计算块数很简单,在您给出的示例代码中,l是计数的元素数.当然,它的值是确定性的(由fe确定).无需遍历它们即可获得计数值.

Calculating the number of blocks is trivial, in the code that you give as example, l is the number of element counted. Of course, that its value is deterministic (determined by f and e). No need to loop over them to get the value of the count.

count = (f-3)*(e-3);

如果要将值保存在数组中(假设此处是矩阵而不是单元格数组),则需要决定如何表示它,可以将其存储为4D e-3 x f-3 x 4 x 4矩阵(如@ Steffen建议),或者作为3D 4 x 4 x count矩阵,我认为后者更为直观.无论如何,您都应该事先为矩阵分配内存,而不要即时分配:

If you want to save the values in an array (assuming that you mean here a matrix and not a cell array) you need to decide how to represent it, you can store it as a 4D e-3 x f-3 x 4 x 4 matrix (as @Steffen suggested), or as a 3D 4 x 4 x count matrix, I think that the later is more intuitive. In any case you should assign the memory for the matrix in advance and not on the fly:

[e f] = size(outImg);
count = (f-3)*(e-3);
outMat = zeros(4,4,count); % assign the memory for the matrix
l = 0;
for i=2:e-2
    for j=2:f-2
        l = l + 1;
        outMat(:,:,l) = double(outImg((i-1:i+2),(j-1:j+2)));
    end;
end;

块的数量存储为countl,但是预先计算count允许提前分配所需的内存,i块存储为outMat(:,:,i).

The number of blocks is stored as both count and l, but calculating count in advance allows to assign the needed memory in advance, the i block is stored as outMat(:,:,i).

使用4D矩阵的实现为:

An implementation using the 4D matrix would be:

[e f] = size(outImg);
count = (f-3)*(e-3);
outMat = zeros((f-3),(e-3),4,4); % assign the memory for the matrix
for i=2:e-2
    for j=2:f-2
        outMat(i,j,:,:) = double(outImg((i-1:i+2),(j-1:j+2)));
    end;
end;

在这种情况下,不需要l,每个块(索引为ij)位于outMat(i,j,:,:)

In this case, l isn't needed and each block (indexed i,j) is located at outMat(i,j,:,:)

关于单元阵列与矩阵,由于矩阵需要在内存中连续放置,因此您可能要考虑使用单元阵列而不是矩阵. 512x512x4的双精度矩阵需要(假定为8字节表示)8MB(512 * 512 * 8 * 4 = 8 * 1024 * 1024).如果尺寸较大,或者由于(连续)内存而受束缚,则单元阵列可能是更好的解决方案.您可以在在Matlab中单元格与矩阵之间的差异来了解更多有关差异的信息..

Regarding cell array vs. a matrix, since a matrix requires a continuous place in the memory, you may want to consider using a cell array instead of a matrix. A 512x512x4 matrix of doubles requires (assuming 8 Byte representation) 8MB (512*512*8*4 = 8*1024*1024). If the dimensions were bigger, or if you are strapped for (continuous) memory a cell array may be a better solution. You can read more about the difference at Difference between cell and matrix in matlab?.

实现将非常相似.

[e f] = size(outImg);
count = (f-3)*(e-3);
outArray = cell(1,count);
l = 0;
for i=2:e-2
    for j=2:f-2
        l = l + 1;
        outArray{1,l} = double(outImg((i-1:i+2),(j-1:j+2)));
    end;
end;

这篇关于如何计算图像中重叠块的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆