How to divide images from folder into 4x4 blocks

조회 수: 2 (최근 30일)
Petrus van Aswegen
Petrus van Aswegen 2021년 5월 5일
댓글: Petrus van Aswegen 2021년 5월 5일
I want to divide all images in a folder into 4x4 blocks and then store the RGB information in a zeros matrix. A sample of what the images look like is attached. Does anyone know how to divide into these blocks and then store the information in the matrix.
  댓글 수: 3
Turlough Hughes
Turlough Hughes 2021년 5월 5일
편집: Turlough Hughes 2021년 5월 5일
Also, consider the blockproc function. This might be the way to go depending on what it is you're trying to do.
Petrus van Aswegen
Petrus van Aswegen 2021년 5월 5일
Sorry for the poor wording mate. The end state is to use the RGB colour information in each 4x4 patch to isolate the properties of the solar panels, and then use support vector machine to detect solar panels in other images. I basically need to divide the images into patches, and then store the information within each patch into a matrix

댓글을 달려면 로그인하십시오.

채택된 답변

Turlough Hughes
Turlough Hughes 2021년 5월 5일
Hi Petrus, the mat2cell approach that Jonas pointed out can be done more generally as follows. Note, this handles the scenario where image dimensions are not a factor of the corresponding block dimensions (the remainder rows or columns are ommited)
1. Parameters
blockHeight = 4;
blockWidth = 4;
folderName = 'C://...'; % folder containing images
2. Directory. These three lines can be used by others to replicate the demo. Once you modify folderName you can replace the following three lines with the fourth one.
[fils(1:6).folder] = deal('');
fnames = cellfun(@(x) sprintf('office_%s.jpg',x),{'1','2','3','4','5','6'},'uni',false);
[fils.name] = fnames{:};
%fils = dir(fullfile(folderName,'*.PNG')); < Replace the above with this line
3. Load an initial image to get some metadata:
I0 = imread(fullfile(fils(1).folder,fils(1).name));
N1 = blockHeight*ones(floor(size(I0,1)/blockHeight),1);
N2 = blockWidth*ones(floor(size(I0,2)/blockWidth),1);
4. Loop through your directory:
L = cell(numel(fils),1);
for ii = 1:numel(fils)
I = imread(fullfile(fils(ii).folder,fils(ii).name));
b = mat2cell(I(1:blockHeight*numel(N1),1:blockWidth*numel(N2),:),N1,N2,3);
L{ii} = cat(4,b{:}); % alternatively L{ii} = b;
end
L
L = 6×1 cell array
{4×4×3×33750 uint8} {4×4×3×33750 uint8} {4×4×3×33750 uint8} {4×4×3×33750 uint8} {4×4×3×33750 uint8} {4×4×3×33750 uint8}
The result, L, is then a 6 by 1 cell array of matrices where each cell corresponds to an image. The matrix dimensions (4x4x3x3750) correspond to the block height, block width, RGB, and a linear index for the block.
  댓글 수: 1
Petrus van Aswegen
Petrus van Aswegen 2021년 5월 5일
This worked perfectly, thank you so much mate. Appreciate your help

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jonas
Jonas 2021년 5월 5일
you could use mat2cell
A = rand(256,256,3); % your matrix here
N = 4*ones(1,64);
B = mat2cell(A,N,N,3);
adjusted from here
  댓글 수: 3
Jonas
Jonas 2021년 5월 5일
catch all files you are interested in eg by {dir('*.jpg').name} and loop through the file names and open them with imread()
Petrus van Aswegen
Petrus van Aswegen 2021년 5월 5일
Legend. Thanks for the help mate, much appreciated.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by