divide image into different percent every time
3 views (last 30 days)
Show older comments
Hi, i want to split my image into 25%. This means i want 4 halves. I know there is a way to do this like the above code:
col1 = 1;
col2 = floor(columns/2);
col3 = col2 + 1;
row1 = 1;
row2 = floor(rows/2);
row3 = row2 + 1;
etc.etc....
But i want create a function to change this with percent every time if i want.
For example if i want to split image into 50% (two halves) or if i want to split into 25% (4 halves) or 20% etc..
Is there any way to change this at will.
Thanks you.
0 Comments
Answers (1)
Image Analyst
on 23 Feb 2019
Do you have a fixed list of percents, or is the percentage to be determined each time as a random number between 1 and the number of rows and columns in the image?
Try this, where I compiled a list of percentages up in advance using random numbers, then split up the image into unequal sized "quadrants" 10 times:
grayImage = imread('moon.tif');
[rows, columns, numColorChannels] = size(grayImage);
numTrials = 10; % However many times you want to split it up.
listOfPercentsX = 1 + (columns - 2) * rand(1, numTrials);
listOfPercentsY = 1 + (rows - 2) * rand(1, numTrials);
for k = 1 : numTrials
row1 = floor(listOfPercentsY(k))
row2 = row1 + 1;
col1 = floor(listOfPercentsX(k))
col2 = col1 + 1
% Make upper left image.
subImage1 = grayImage(1:row1, 1:col1);
subplot(2, 2, 1);
imshow(subImage1);
axis('on', 'image');
% Make upper right image.
subImage2 = grayImage(1:row1, col2:end);
subplot(2, 2, 2);
imshow(subImage2);
axis('on', 'image');
% Make lower left image.
subImage3 = grayImage(row2:end, 1:col1);
subplot(2, 2, 3);
imshow(subImage3);
axis('on', 'image');
% Make lower right image.
subImage4 = grayImage(row2:end, col2:end);
subplot(2, 2, 4);
imshow(subImage4);
axis('on', 'image');
drawnow;
end
4 Comments
Image Analyst
on 23 Feb 2019
What are you going to do once you've split them up into overlapping tiles? Presumably you will do something, so why not use blockproc(), the function made for this? See attached demo.
I have other demos for NON-overlapping tiles, if you want those too.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!