How to divide an image into 8 equal parts ?
6 次查看(过去 30 天)
显示 更早的评论
I am working on medical imaging and I have to segment an image into 8 equal parts.Can anyone help me with the code?
回答(2 个)
Walter Roberson
2015-8-26
This is not always possible. In order to divide an image into 8 equal parts, the number of elements of the array must be divisible by 8. If the image is grayscale then the third dimension is 1, which will not help the number be divisible by 8; if the image is color then the third dimension is 3, which will not help the number be divisible by 8. Therefore the produce of the number of row and number of colors must be divisible by 8. And that is certainly not always the case, since the number of rows and number of columns could both be odd.
The easiest case would be if the number of rows or the number of columns was exactly divisible by 8, as then you could divide it up into 8 pieces along that dimension.
If the neither the rows nor columns are exactly divisible by 8, you can still create 8 equal pieces if either the rows or columns is divisible by 4 and the other dimension is divisible by 2
If the number of elements is divisible by 8, then one way to divide into 8 equal pieces for grayscale images would be:
pieces = cell(8,1);
nel = numel(YourImage);
nel8 = nel / 8;
for K = 1 : 8
pieces{K} = YourImage((K-1)*nel8 + 1 : K * nel8);
end
The resulting pieces will be column vectors. You never said that the pieces had to be meaningful, only that they be equal. If the number of columns happens to be divisible by 8 then those pieces could be reshaped to be equal vertical strips, but not otherwise.
0 个评论
Jordan Kennedy
2021-5-1
Divide up image by specifying number of rows and columns you want and save off sub images.
filename=' path to image image.tif';
E = imread(filename);
[row, col, ~] = size(E);
num_img_h = 6; %define number col
num_img_v = 3; %number of rows
row_breaks = round(linspace(1,row,num_img_v+1),0)
col_breaks = round(linspace(1,col,num_img_h+1),0)
loop = 1
for iter1 = 1:num_img_v
iter1
for iter2 = 1:num_img_h
iter2
imwrite(E(row_breaks(iter1):row_breaks(iter1+1),col_breaks(iter2):col_breaks(iter2+1),1:3),['temp', num2str(loop), '.tiff']);
loop = loop + 1
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!