how to divide an image diagonally into two part?

 Accepted Answer

Soumyadip - consider using tril or triu to obtain the lower triangular part or upper triangular part of the image (matrix).

2 Comments

x=imread('12.jpg');
x1=size(x)
x1=
168 300 3
then how can i apply tril/triu on it to get the diagonally divided image?
What do you want? Do you want a 1-D list of all pixels values in each triangle? Or do you want a masked image where half the image is the original, and half is set to some value, like black (0) or white (255)?

Sign in to comment.

More Answers (1)

Not exactly sure what you mean so I did it two different ways, one to give a masked image and another that has a 1-D list of extracted values. Make note that the pixels along the main diagonal will occur in both the upper and lower images.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = imread('cameraman.tif');
% Display the original gray scale image.
subplot(3, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% 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')
upperPixelImage = triu(grayImage); % 2D image
% Display the original gray scale image.
subplot(3, 1, 2);
imshow(upperPixelImage, []);
title('Upper Triangle', 'FontSize', fontSize);
% Get the 1-D list of upper values.
mask = triu(ones(size(grayImage))) > 0;
upperList = upperPixelImage(mask);
lowerPixelImage = tril(grayImage); % 2D image
% Display the original gray scale image.
subplot(3, 1, 3);
imshow(lowerPixelImage, []);
title('Lower Triangle', 'FontSize', fontSize);
% Get the 1-D list of lower values.
mask = tril(ones(size(grayImage))) > 0;
lowerList = lowerPixelImage(mask);

1 Comment

Thank you for your help but Triu is Tril works only on square matrix. Please help in finding the diagonal half of an image or a matrix, where the size of rows and column are different.Screenshot (1).png

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!