how to break an image into sub images

i have an image having pixels 512*512 . i want to break it in equal sub images (for eg like into images of 8*8) and want to apply a function (can be any) on each sub image ??

답변 (3개)

Zainab Ishfaq
Zainab Ishfaq 2018년 5월 29일
편집: Walter Roberson 2020년 2월 13일

0 개 추천

Height=8; width=8;
currentimage = imread('Image.png');
[r,c,~]=size( currentimage );
count=0;
for i=0:Height:r
count=count+1;
for j=0:width:c
if((i+Height)<r && (j+width)<c)
Image=imcrop( currentimage ,[(i+1) (j+1) Height width]);
imwrite(Image,horzcat(currentfilename, num2str(count),'.tif'));
end
end
end

댓글 수: 9

ahmad Al sarairah
ahmad Al sarairah 2019년 10월 9일
I have an image with (643x643) dimensions , and i need to split it horizontally and vertically into 100 sub images,so that each sub image is (64x64)pixels using matlab code .
The sub images are blocks with (64x64) pixels ,so the height and the width of each block is equal 64 .
i used this code , but i get only 10 sub images .
shital shinde
shital shinde 2020년 2월 13일
anyone please send me above code but using parallel concept. Means using parfor loop. Actually I am working for image denoising, I want to divide the image into blocks and then try to perofrm denoisinng it. Please help me.
Change
for i=0:Height:r
count=count+1;
to
parfor i=0:Height:r
count=i+1;
shital shinde
shital shinde 2020년 2월 14일
error- Undefined function or variable 'currentfilename'.
I got this error when I am trying to run it,
infilename = 'Image.png';
Height=8; width=8;
currentimage = imread(infilename);
[r,c,~]=size( currentimage );
[~, currentfilename, ~] = fileparts(infilename);
for i=0:Height:r
ridx = floor(i/Height) + 1;
for j=0:width:c
cidx = floor(j/width) + 1;
if((i+Height)<r && (j+width)<c)
Image=imcrop( currentimage ,[(i+1) (j+1) Height width]);
outfilename = sprintf('%s_%03d_%03d.tif', ridx, cidx)
imwrite(Image, outfilename);
end
end
end
shital shinde
shital shinde 2020년 2월 18일
편집: shital shinde 2020년 2월 18일
It again gives the error sir. I attached screenshot. I make some modifications, but it didn't work. please tell me .
outfilename = sprintf('%s_%03d_%03d.tif', currentfilename, ridx, cidx)
shital shinde
shital shinde 2020년 2월 21일
편집: shital shinde 2020년 2월 21일
Thanks sir. It work now. I have one question. Can we parallelize the above example using parallel computing toolbox.
Walter Roberson
Walter Roberson 2020년 2월 21일
편집: Walter Roberson 2020년 2월 21일
Yes. Just change for i to parfor i
As we have advised you before, the result will almost certainly be notably slower than your current code. But I know from your previous questions that you feel inclined to do that testing yourself.

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

image-pro
image-pro 2021년 10월 19일

0 개 추천

i have an error please help
Error in p8 (line 11)
Image=imcrop(currentimage ,[(i+1) (j+1) Height width]);

질문:

Rao
2012년 9월 4일

댓글:

2021년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by