在 MATLAB 中组合两个图像
在本文中,我们将学习如何在 MATLAB 中组合两个相同大小的图像。
现在,让我们假设我们得到了两个不同大小的彩色图像。一张图片左侧是主要人物,另一张图片右侧是主要人物。我们需要组合这两个图像,以便两个数字都出现在它们上面。
我们需要将它们结合起来。
步骤 1:使 right_side_image 的左半部分为 0 强度图像(黑色)。此外,使 left_side_image 0 强度区域的右半部分。
Syntax:
image_var = imread(” image path here “);
var_name = rgb2gray (image_var);
size( image_var )
imshow( var_name , [ ]);
or
imtool( var_name, [ ]);
Access half columns :
image_var( :, 1:150);
例子一:
Matlab
% MATLAB code for read the left
% and right side of images.
left=imread("logo_left.png");
right=imread("logo_right.png");
% Convert them into grayscale images.
left=rgb2gray(left);
right=rgb2gray(right);
% Display the gray scaled images.
imtool(left, []);
imtool(right, []);
% Display the size of both images, then choose resize.
size(left) % size of left is= 160 300 3
size(right) % size of right is= 160 300 3
% Make other half as 0 intensity region.
left(:, 150:300)=0;
right(:, 1:150)=0;
% Display both images.
imtool(left, []);
imtool(right, []);
Matlab
% MATLAB code for image combine
% read the left and right sided images.
left=imread("logo_left.png");
right=imread("logo_right.png");
% Convert them into grayscale images.
left=rgb2gray(left);
right=rgb2gray(right);
% Display the gray scaled images.
imtool(left, []);
imtool(right, []);
% Display the size of both images, then choose resize.
size(left) % size of left is= 160 300 3
size(right) % size of right is= 160 300 3
% Make other half as 0 intensity region.
left(:, 150:300)=0;
right(:, 1:150)=0;
% Display both images.
imtool(left, []);
imtool(right, []);
% Combine left and right images and display.
combined=uint8(left+right);
imtool(combined,[]);
输出:
- 在上面的程序中,我们首先读取左右图像。
- 将彩色图像转换为灰度图像。
- 显示图像。
- 显示两个图像的大小。
- 将另一半强度转换为 0。使一半变暗。
- 显示修改后的图像。
第2步:组合左右部分。
Syntax:
new_image = image_1 + image_2
// condition: both images must be of same size and type.
例二:
MATLAB
% MATLAB code for image combine
% read the left and right sided images.
left=imread("logo_left.png");
right=imread("logo_right.png");
% Convert them into grayscale images.
left=rgb2gray(left);
right=rgb2gray(right);
% Display the gray scaled images.
imtool(left, []);
imtool(right, []);
% Display the size of both images, then choose resize.
size(left) % size of left is= 160 300 3
size(right) % size of right is= 160 300 3
% Make other half as 0 intensity region.
left(:, 150:300)=0;
right(:, 1:150)=0;
% Display both images.
imtool(left, []);
imtool(right, []);
% Combine left and right images and display.
combined=uint8(left+right);
imtool(combined,[]);
输出:
输出图像是两个图像的组合。