Convert image into Patches of size 64*64 and get each patch

71 ビュー (過去 30 日間)
Tahir
Tahir 2015 年 5 月 14 日
編集済み: Neha Rathore 2022 年 4 月 28 日
Hello, I have RGB image dataset.I want to convert an image into patches and save each patch.How to do this in matlab. The patch size should be 64*64
  3 件のコメント
Image Analyst
Image Analyst 2021 年 4 月 6 日
@Rajesh Das, and should be down below in the official "Answer" section with all the other answers of this question.

サインインしてコメントする。

採用された回答

Sven
Sven 2015 年 5 月 14 日
編集済み: Sven 2015 年 5 月 14 日
Hi Tahir, try this:
I = imread('rice.png');
imSz = size(I);
patchSz = [64 64];
xIdxs = [1:patchSz(2):imSz(2) imSz(2)+1];
yIdxs = [1:patchSz(1):imSz(1) imSz(1)+1];
patches = cell(length(yIdxs)-1,length(xIdxs)-1);
for i = 1:length(yIdxs)-1
Isub = I(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patches{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);
end
end
This produces a cell of your 64x64 patches:
>> patches
patches =
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
And you can access each one:
figure, imagesc(patches{2,3})
Did that do what you wanted?
Thanks, Sven.
  6 件のコメント
Neha Rathore
Neha Rathore 2022 年 4 月 28 日
編集済み: Neha Rathore 2022 年 4 月 28 日
Hi! The code worked well. How can i recombine these patches to form an original image.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 5 月 14 日
This is answered by the FAQ in two different ways (mat2cell and indexing). Take your pick as to which is easier, more convenient, and intuitive for you.
  11 件のコメント
Image Analyst
Image Analyst 2018 年 9 月 8 日
I do have a version of blockproc() that uses a separate function, where you can do more complicated operations than in an anonymous function. It is attached.
There is also a function called graycoprops() that you may want instead of graycomatrix(). It gives you key features from the GLCM matrix.

サインインしてコメントする。


Ahmad
Ahmad 2017 年 3 月 30 日
hello. its work well on 256 by 256 image but i have 512 by 512 image. it gives the following error. Index exceeds matrix dimensions.
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 3 月 30 日
See the FAQ that Image Analyst posted.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by