How to divide an image into 8 equal parts ?
조회 수: 5 (최근 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!