フィルターのクリア

how can i divide an image into 2 equal parts.horizontaly,?

3 ビュー (過去 30 日間)
sara
sara 2015 年 4 月 20 日
回答済み: vahid rowghanian 2021 年 4 月 4 日
this code works for 3 equal horizontal parts,how can i change it for 2 parts?
if true
[n,m]=size(im) % im is your image
id=fix(n/3)
im1=im(1:id,:);
im2=im(id+1:2*id,:);
im3=im(2*id+1:n,:);
end

採用された回答

Image Analyst
Image Analyst 2015 年 4 月 20 日
You can use imcrop, which will work for color and gray scale images:
[rows, columns, numberOfColorChannels] = size(im);
topHalf = imcrop(im, [1, 1, columns, floor(rows/2)]);
bottomHalf = imcrop(im, [1, ceil(rows/2), columns, floor(rows/2)]);
Or if you know for a fact that it's a 2D monochrome image
[rows, columns, numberOfColorChannels] = size(im);
middleRow = floor(rows/2);
topHalf = im(1:middleRow, :);
bottomHalf = im(1+middleRow:end, :);
  3 件のコメント
Sudeep Albal
Sudeep Albal 2017 年 1 月 25 日
How can i obtain original image from those two parts?
Image Analyst
Image Analyst 2017 年 1 月 25 日
fullImage = [topHalf; bottomHalf];

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

その他の回答 (1 件)

vahid rowghanian
vahid rowghanian 2021 年 4 月 4 日
If you want to divide the image into squares of size gs ( e.g. 2,3,4,5,6,7,8,9,10,…), you can use the following code. This function extracts gs by gs patches from the input image and stores them into 'patch' variable. Notice that, if width or height of the input image is not a multiple of gs, then an offset equal to residual of the division won't be covered in patch extraction.
function [ patchim , npatchim ] = divideimage (im , gs)
imheight=size(im,1);
imwidth=size(im,2);
maxgsrow = floor( imheight / gs);
maxgscol = floor( imwidth / gs );
npatch = 1;
for i = 1 : maxgsrow
for j = 1 : maxgscol
rmin = ( i - 1 ) * gs + 1;
rmax = i * gs;
cmin = ( j - 1) * gs + 1;
cmax = j * gs;
%%%%% do processes
patch ( : , : , : , npatch) = im( rmin : rmax , cmin : cmax , : );
npatch = npatch + 1;
endfor
endfor
npatchim = npatch - 1;
patchim = patch;
end

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by