📜  如何在 MATLAB 中为图像添加边框?

📅  最后修改于: 2022-05-13 01:54:35.731000             🧑  作者: Mango

如何在 MATLAB 中为图像添加边框?

图像的边框/框架是添加到图像的外边界。它不是主图像的一部分。区域边界和边缘密切相关,因为区域边界处的强度通常会急剧调整。对于灰度图像或任何彩色图像,我们可以添加一个二进制或灰度帧,因为它只包含一个通道。框架的设计没有限制。我们可以添加任何东西作为图像的框架。框架的大小没有限制。所有这些细节都可以根据用户的要求进行更改。

使用的函数:

  • imread( ) 内置函数用于读取图像。
  • imtool( ) 内置函数用于显示图像。
  • zeros( ) 内置函数用于创建带有 0 的矩阵。
  • rgb2gray( ) 内置函数用于将 RGB 图像转换为灰度图像。
  • pause( ) 内置函数用于停止执行指定的秒数。

Frame_Image()函数的作用:这是实用函数。所有可执行语句都组合到这个函数中,因此我们只需调用一个函数即可将帧添加到任何输入图像。此函数采用单个参数,即输入图像。如果图像是彩色的,则它首先将其转换为灰度,然后将帧添加到其中。

例子:

Matlab
% MATLAB CODE ADDING FRAME TO THE IMAGE
% This function is only for Grayscale images.
function Frame_Image(img)
  
% Convert into grayscale if not.
[x,y,z]=size(img);
if(z==3)
    img=rgb2gray(img);
end
  
% Create frame.
frame=zeros(x+30,y+30);
  
% imtool(frame,[]);
frame(6:10,11:y+20)=255;
frame(x+21:x+25,11:y+20)=255;
frame(6:x+25,6:10)=255;
frame(6:x+25,y+21:y+25)=255;
  
%frame(6:10,11:y+20)=255;
%frame(x+21:x+25,11:y+20)=255;
%frame(6:x+25,6:10)=255;
%frame(6:x+25,y+21:y+25)=255;
%frame(6:10,:)=255;
%frame(x+21:x+25,:)=255;
%frame(:,6:10)=255;
%frame(:,y+21:y+25)=255;
%add image in middle.
frame(16:x+15,16:y+15)=img(:,:);
original_input_image=img;
imtool(original_input_image,[]);
framed_image=frame;
imtool(framed_image,[]);
  
pause(10);
imtool close all;
end
  
% UTILITY CODE
k=imread("cameraman.png");
Frame_Image(k);


Matlab
% MATLAB CODE Add Coloured Frame to the RGB image.
function AddColouredFrame(img)
  
% This function takes colour image only.
[x,y,~]=size(img);
framed_img(x+20,y+20,3)=0;
framed_img=uint8(framed_img);
framed_img(11:x+10,11:y+10,:)=img(:,:,:);
  
% Horizontal Yellow lines on Top and bottom of 3 px.
framed_img(1:3,:,1:2)=255;
framed_img(x+18:x+20,:,1:2)=255;
  
% Vertical Yellow lines on Left and Right of 3 px.
framed_img(:,1:3,1:2)=255;
framed_img(:,y+18:y+20,1:2)=255;
  
% Horizontal Cyan lines on Top and bottom of 4 px.
framed_img(4:7,4:y+17,2:3)=255;
framed_img(x+14:x+17,4:y+17,2:3)=255;
  
% Vertical Cyan lines on Left and Right of 4 px.
framed_img(4:x+17,4:7,2:3)=255;
framed_img(4:x+17,y+14:y+17,2:3)=255;
  
% Horizontal Red lines on Top and bottom of 3 px.
framed_img(8:10,8:y+13,1)=200;
framed_img(x+11:x+13,8:y+13,1)=200;
  
% Vertical Red lines on Left and Right of 3 px.
framed_img(8:x+13,8:10,1)=200;
framed_img(8:x+13,y+11:y+13,1)=200;
imtool(framed_img,[]);
end
  
% Utility code%
k=imread("logo.png");
AddColouredFrame(k);


输出:

代码说明:

  • 帧=零(x+30,y+30);此行创建具有黑色背景的新图像。
  • 帧(6:10,11:y+20)=255;这条线添加了顶部水平框架线。
  • 帧(x+21:x+25,11:y+20)=255;这条线添加了底部水平框架线。
  • 帧(6:x+25,6:10)=255;这条线添加了左垂直框架线。
  • 帧(6:x+25,y+21:y+25)=255;这条线添加了右垂直框架线。
  • 帧(16:x+15,16:y+15)=img(:,:);此行在中间添加图像。
  • imtool(framed_image,[]);此行显示带框的图像。

例子:

MATLAB

% MATLAB CODE Add Coloured Frame to the RGB image.
function AddColouredFrame(img)
  
% This function takes colour image only.
[x,y,~]=size(img);
framed_img(x+20,y+20,3)=0;
framed_img=uint8(framed_img);
framed_img(11:x+10,11:y+10,:)=img(:,:,:);
  
% Horizontal Yellow lines on Top and bottom of 3 px.
framed_img(1:3,:,1:2)=255;
framed_img(x+18:x+20,:,1:2)=255;
  
% Vertical Yellow lines on Left and Right of 3 px.
framed_img(:,1:3,1:2)=255;
framed_img(:,y+18:y+20,1:2)=255;
  
% Horizontal Cyan lines on Top and bottom of 4 px.
framed_img(4:7,4:y+17,2:3)=255;
framed_img(x+14:x+17,4:y+17,2:3)=255;
  
% Vertical Cyan lines on Left and Right of 4 px.
framed_img(4:x+17,4:7,2:3)=255;
framed_img(4:x+17,y+14:y+17,2:3)=255;
  
% Horizontal Red lines on Top and bottom of 3 px.
framed_img(8:10,8:y+13,1)=200;
framed_img(x+11:x+13,8:y+13,1)=200;
  
% Vertical Red lines on Left and Right of 3 px.
framed_img(8:x+13,8:10,1)=200;
framed_img(8:x+13,y+11:y+13,1)=200;
imtool(framed_img,[]);
end
  
% Utility code%
k=imread("logo.png");
AddColouredFrame(k);

输出:

代码说明:

  • framed_img(x+20,y+20,3)=0;此行创建具有黑色背景的空图像。
  • framed_img(11:x+10,11:y+10,:)=img(:,:,:);此行将输入图像添加到帧的中间。
  • framed_img(1:3,:,1:2)=255;这条线在顶部添加了 3 像素粗的水平黄线。
  • framed_img(x+18:x+20,:,1:2)=255;这条线在底部添加了 3 像素粗的水平黄线。
  • framed_img(:,1:3,1:2)=255;这条线在左侧添加了 3 像素粗的垂直黄线。
  • framed_img(:,y+18:y+20,1:2)=255;这条线在右侧添加了 3 像素粗的垂直黄线。
  • imtool(framed_img,[]);这将显示带框的图像。
  • k=imread(“logo.png”);此行读取输入图像。
  • AddColoredFrame(k);此行通过传递输入图像来调用 frame函数。