divide image into different percent every time

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.

回答 (1 件)

Image Analyst
Image Analyst 2019 年 2 月 23 日

0 投票

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 件のコメント

pasta pontikaki
pasta pontikaki 2019 年 2 月 23 日
編集済み: pasta pontikaki 2019 年 2 月 23 日
Exactly i want to divide my initial image into 25%, creating 4 blocks with 50% overlap each block.
Then divide each sub block of this 4, into sub blocks of 20% with zero overlap
this is my image
hole4.jpg
Image Analyst
Image Analyst 2019 年 2 月 23 日
You saw how I computed the rows and columns to split it at. Can't you adapt that to handle overlaps? (Because I'm going to be out for several hours after this.) It's not hard.
pasta pontikaki
pasta pontikaki 2019 年 2 月 23 日
ok, i will try thank you very much
Image Analyst
Image Analyst 2019 年 2 月 23 日
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.

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

タグ

質問済み:

2019 年 2 月 23 日

コメント済み:

2019 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by