# How to divide an image into 8 equal parts ?

7 Ansichten (letzte 30 Tage)
Sneha Suresh am 26 Aug. 2015
I am working on medical imaging and I have to segment an image into 8 equal parts.Can anyone help me with the code?
##### 1 KommentarKeine anzeigenKeine ausblenden
Equal in what way?

Melden Sie sich an, um zu kommentieren.

### Antworten (2)

Walter Roberson am 26 Aug. 2015
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 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Jordan Kennedy am 1 Mai 2021
Divide up image by specifying number of rows and columns you want and save off sub images.
filename=' path to image image.tif';
[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 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Region and Image Properties finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!