How split an image into four parts?

조회 수: 9 (최근 30일)
Bajdar Nouredine
Bajdar Nouredine 2023년 1월 3일
이동: Matt J 2023년 1월 6일
I have 2x10 cell array each cell contains 224x896 double, I want to split each image inside the cells to 224x224

채택된 답변

Voss
Voss 2023년 1월 3일
편집: Voss 2023년 1월 3일
% since I don't have your cell array, I make one up:
C = repmat({randi([0 255],224,896)},2,10)
C = 2×10 cell array
{224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double}
% split each matrix in C into 4:
C_new = cellfun(@(x)mat2cell(x,224,224*ones(1,4)),C,'UniformOutput',false)
C_new = 2×10 cell array
{1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell}
% each cell of C_new contains a 1x4 cell array containing the 4 224x224 matrices:
C_new{1,1}
ans = 1×4 cell array
{224×224 double} {224×224 double} {224×224 double} {224×224 double}
C_new{2,1}
ans = 1×4 cell array
{224×224 double} {224×224 double} {224×224 double} {224×224 double}
% etc.
  댓글 수: 5
Voss
Voss 2023년 1월 6일
Instead of 4 separate variables, how about a single cell array of size 2x10x4?
% (same as before) generation of random data:
C = repmat({randi([0 255],224,896)},2,10)
C = 2×10 cell array
{224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double} {224×896 double}
% (same as before) split each matrix in C into 4:
C_new = cellfun(@(x)mat2cell(x,224,224*ones(1,4)),C,'UniformOutput',false)
C_new = 2×10 cell array
{1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell}
% rearrange the contents of C_new:
C_newer = permute(reshape([C_new{:}],4,2,[]),[2 3 1])
C_newer = 2×10×4 cell array
C_newer(:,:,1) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} C_newer(:,:,2) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} C_newer(:,:,3) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} C_newer(:,:,4) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double}
So, C_newer{i,j,k} is the matrix stored in the kth cell of C_new{i,j}, i.e., C_new{i,j}{k}.
Put another way, C_newer{i,j,k} is the kth quarter of the original C{i,j}.
% Verification:
all_ok = true;
for i = 1:2
for j = 1:10
for k = 1:4
if ~isequal(C_newer{i,j,k}, C_new{i,j}{k}, C{i,j}(:,(end/4)*(k-1)+1:(end/4)*k))
all_ok = false;
break
end
end
if ~all_ok, break; end
end
if ~all_ok, break; end
end
if all_ok
disp('Looks good!');
else
disp('Something''s wrong!');
end
Looks good!
(If you really want 4 separate variables, you can easily do that:)
C_newer_1 = C_newer(:,:,1)
C_newer_1 = 2×10 cell array
{224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double}
C_newer_2 = C_newer(:,:,2)
C_newer_2 = 2×10 cell array
{224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double}
C_newer_3 = C_newer(:,:,3)
C_newer_3 = 2×10 cell array
{224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double}
C_newer_4 = C_newer(:,:,4)
C_newer_4 = 2×10 cell array
{224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double}
Bajdar Nouredine
Bajdar Nouredine 2023년 1월 6일
이동: Matt J 2023년 1월 6일

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

추가 답변 (1개)

Matt J
Matt J 2023년 1월 6일
편집: Matt J 2023년 1월 6일
Using mat2tiles from this FEX download,
C(1:2,1:10)={rand(224,896)}; %hypothetical input
A= mat2tiles( cell2mat(C), [224,224]);
A=permute(reshape(A,2,4,10),[1,3,2]) %final result
A = 2×10×4 cell array
A(:,:,1) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} A(:,:,2) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} A(:,:,3) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} A(:,:,4) = {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double} {224×224 double}

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by