简体   繁体   English

在Matlab中将图像拆分为块

[英]Splitting an image into blocks in Matlab

I'm new to Matlab and right now, I'm trying to split an image into NxN blocks as shown in an example below. 我是Matlab的新手,现在,我正尝试将图像分成NxN个块,如下面的示例所示。

在此处输入图片说明

In matlab, I'm currently trying something: 在matlab中,我目前正在尝试一些方法:

my_image = imread('cat.jpg')
figure, imshow(my_image)

nrow = 12; %12 by 12 blocks as shown in the example above.
ncol = 12;

for i =1:nrow
    for j=2:ncol
       block = ...
    end
end

But I'm not entirely sure on how am i supposed to get those lines on the image. 但是我不确定我应该如何在图像上显示这些线条。 I've googled on this but non of them matches what I'm trying to get. 我已经用谷歌搜索了,但是没有一个与我想要的匹配。 Could anyone demonstrate on how is this done? 谁能证明这是怎么做的?

You don't need to do anything. 您什么都不需要做。 Matlab's array indexing already supports retrieving and modifying subblocks. Matlab的数组索引已经支持检索和修改子块。

For example, the 2nd 12x12 block is 例如,第二个12x12块是

my_image(1:12,13:24,:)

(I assumed 1 extra dimension for color. I'll let you handle the colors.) (我假设颜色有1个额外的尺寸。我让您处理颜色。)

You do not want to re-create the blocks unless you need both the original and the partitioned ones for computation. 除非需要原始块和分区块,否则您不希望重新创建块。 As much as possible, you want to let Matlab handle the copying and memory allocation. 您想让Matlab尽可能处理复制和内存分配。

If you really really need to generate the subblocks explicitly, see mat2cell and also this SO question . 如果您确实确实需要显式生成子块,请参见mat2cell以及此SO问题

I've googled on this but non of them matches what I'm trying to get. 我已经用谷歌搜索了,但是没有一个与我想要的匹配。

When you Google, you want to Google the most generic and core concepts. 当您使用Google时,您想要Google最通用和最核心的概念。 What is an image in Matlab? Matlab中的图像是什么? It is a matrix. 它是一个矩阵。 What you need is to search how to split a matrix into subblocks. 您需要搜索如何将矩阵拆分为子块。 Along this line, search any possible phrase that describes the concept and you will see tons of examples in search returns. 沿着这条线,搜索描述该概念的任何可能短语,您将在搜索返回中看到大量示例。

I believe you can use the idea below without the third dimension. 我相信您可以在没有第三个维度的情况下使用以下想法。

M = magic(4);

N = 2;

x = 1:N;
y = 1:N;

[ X, Y] = meshgrid( x, y);

X = X(:);
Y = Y(:);

ind = sub2ind( size(M), X, Y);
ind = sort(ind);

subM = M(ind);
subM = reshape( subM, [N N])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM