How to divide an image in two blocks

How can I divide an image into two halves? Then, after dividing it make each half independent, say
A = top half and
B = the bottom half.
So that I can extract features from each half.

2 comentarios

Nourou
Nourou el 8 de Ag. de 2014
Editada: Image Analyst el 8 de Ag. de 2014
Hi Jack, Azzi,
Please I need a clarification about the usage of the following method:
im=imread('yourimage')
n=fix(size(im,1)/2)
A=im(1:n,:,:);
B=im(n+1:end,:,:)
imshow(A)
figure
imshow(B)
I try to use it on a grayscale image, but the results are 2 blank images. Please how can I edit your code to make it divide my image. I know you use it with a rgb image.
Thank you!
Image Analyst
Image Analyst el 8 de Ag. de 2014
Editada: Image Analyst el 8 de Ag. de 2014
A and B are probably double for some reason. Try
imshow(A, []);
imshow(B, []);
Start your own question if you need more help.

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 2 de Feb. de 2013
im=imread('yourimage')
n=fix(size(im,1)/2)
A=im(1:n,:,:);
B=im(n+1:end,:,:)
imshow(A)
figure
imshow(B)

10 comentarios

Jack Sparrow
Jack Sparrow el 2 de Feb. de 2013
Thank you Azzi. .Is it also thesame procedure for more than two blocks?
Azzi Abdelmalek
Azzi Abdelmalek el 2 de Feb. de 2013
I guess it's the same, unless there are tools for that in image processing toolbox.
Jack Sparrow
Jack Sparrow el 2 de Feb. de 2013
okay thanks
Image Analyst
Image Analyst el 2 de Feb. de 2013
There are tool in the Image Processing Toolbox. imcrop(). But Azzi, your method is simpler. imcrop() unfortunately it doesn't have an option (yet) where you can just pass in starting and ending rows and columns. It's based off a "rectangle" where you have to pass in [x, y, width, height] so you have to calculate width and height. It's a pain and I'm constantly telling the image processing team that we need a pixel-based option.
Jack Sparrow
Jack Sparrow el 2 de Feb. de 2013
Thanks image analyst. Although I have already used the solution provided by Azzi
Explorer
Explorer el 31 de Dic. de 2013
Editada: Azzi Abdelmalek el 31 de Dic. de 2013
Please do necessary changes in below mentioned code for splitting image as left half and right half.
im=imread('yourimage')
n=fix(size(im,1)/2)
A=im(1:n,:,:);
B=im(n+1:end,:,:)
imshow(A)
figure
imshow(B)
% Read in image and display it.
rgbImage=imread('peppers.png');
subplot(2, 2, 1);
imshow(rgbImage);
% Get sizes of the images.
[rows, columns, numberOfColorChannels] = size(rgbImage);
middleColumn = int32(columns/2)
title('Right Half', 'FontSize', 25);
% Split them up.
leftHalf = rgbImage(:, 1:middleColumn, :);
rightHalf=rgbImage(:, middleColumn+1:end, :);
% Display them.
subplot(2, 2, 3);
imshow(leftHalf);
title('Left Half', 'FontSize', 25);
subplot(2, 2, 4);
imshow(rightHalf);
title('Right Half', 'FontSize', 25);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Explorer
Explorer el 1 de En. de 2014
Thank you Image Analyst.
zohreh nezami
zohreh nezami el 22 de Jun. de 2017
thank you
suma g
suma g el 2 de Abr. de 2018
sir u have given for left half and right half of the image... very helpful.i want to segment the face image into upper and bottom half of the face ... upper- forehead,eyes and eyebrows & lower- nose lips and chin CAN U GIVE ME SOME IDEA

Iniciar sesión para comentar.

Más respuestas (1)

utkarsh kumar
utkarsh kumar el 26 de Abr. de 2019

0 votos

hi i am having a doubt in understanding the code.....in the below code
im=imread('yourimage')
n=fix(size(im,1)/2)
A=im(1:n,:,:);
B=im(n+1:end,:,:)
imshow(A)
figure
imshow(B)
What is the meaning of the highlighted one .
Thanks

1 comentario

Image Analyst
Image Analyst el 26 de Abr. de 2019
It says A is equal to rows 1 through n, all columns, all color rows. The colon means "all" if it's by itself, or "to" if it's in an expressions like 1:n.
So if n is the middle row, A will be the top half. Then B picks up from one row below the middle row. It says B equals all rows from one row below the middle row to the last row, and all columns and all color channels. If it's grayscale, having an extra : in there for the third index will not hurt - it will just ignore it.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 2 de Feb. de 2013

Comentada:

el 26 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by