MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何使用MATLAB自动处理人为图像的填充? (https://www.labfans.com/bbs/showthread.php?t=23289)

poster 2019-12-10 20:30

如何使用MATLAB自动处理人为图像的填充?
 
这是基于此答案的另一个问题:

[URL="https://stackoverflow.com/questions/2589851/how-can-i-implement-this-visual-effect-in-matlab/2589957#2589957"]如何在MATLAB中实现鱼眼镜头效果(镜筒变换)?[/URL]

通用解决方案应适用于所有背景颜色和长/宽比。



[B]回答:[/B]

通常,在MATLAB中有许多不同的方法可以做到这一点。我将列出一些用于填充[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-20224"]RGB图像的[/URL]示例...

[B]解决方案#1:使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cat.html"]CAT[/URL]添加填充以制作正方形图像[/B]

这种解决方案需要一个给定的颜色padColor以及使用该功能复制它[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"]REPMAT[/URL]创造合适的大小,形状和颜色的填充。然后使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cat.html"]CAT[/URL]函数将填充添加到图像的侧面:

[r,c,d] = size(rgbImage); %# Get the image dimensions nPad = abs(cr)/2; %# The padding size padColor = [1 1 1]; %# RGB triple for pad color (white) padColor = reshape(padColor,1,1,3); %# Reshape pad color to 1-by-1-by-3 if c > r %# Pad rows newImage = cat(1,repmat(padColor,floor(nPad),c),... %# Top padding rgbImage,... %# Image repmat(padColor,ceil(nPad),c)); %# Bottom padding elseif r > c %# Pad columns newImage = cat(2,repmat(padColor,r,floor(nPad)),... %# Left padding rgbImage,... %# Image repmat(padColor,r,ceil(nPad))); %# Right padding end 您可以通过将定义padColor的两行替换为以下其中之一来修改上述解决方案,以使其适用于[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-17587"]索引[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]图像[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-13941"]灰度[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]图像[/URL]或[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]二进制图像[/URL] :

padColor = uint8(1); %# For an indexed image (index of color to use) padColor = uint8(255); %# For a grayscale image (white) padColor = true; %# For a binary image (white)


[B]解决方案2:制作空白方形图像并插入原始图像[/B]

该解决方案采用给定的颜色padColor和使用功能复制它[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"]REPMAT[/URL]创建颜色的空白正方形图像。然后将原始图像以居中位置插入到该空白图像中:

[r,c,d] = size(rgbImage); %# Get the image dimensions padColor = [1 1 1]; %# RGB triple for pad color (white) padColor = reshape(padColor,1,1,3); %# Reshape pad color to 1-by-1-by-3 if c > r %# Pad rows newImage = repmat(padColor,c); %# Make c-by-c-by-3 matrix of given color rowIndex = floor((cr)/2); %# Row index for inserting image newImage(rowIndex+(1:r),:,:) = rgbImage; %# Insert the image elseif r > c %# Pad columns newImage = repmat(padColor,r); %# Make r-by-r-by-3 matrix of given color columnIndex = floor((rc)/2); %# Column index for inserting image newImage(:,columnIndex+(1:c),:) = rgbImage; %# Insert the image end 您可以通过将定义padColor的两行替换为以下其中之一来修改上述解决方案,以使其适用于[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-17587"]索引[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]图像[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-13941"]灰度[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]图像[/URL]或[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]二进制图像[/URL] :

padColor = uint8(1); %# For an indexed image (index of color to use) padColor = uint8(255); %# For a grayscale image (white) padColor = true; %# For a binary image (white)


[B]解决方案3:使用[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/padarray.html"]PADARRAY[/URL] [/B]

该解决方案使用函数[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/padarray.html"]PADARRAY[/URL]创建填充以使图像方形。不幸的是,使用此解决方案时,没有简单的方法为[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-20224"]RGB图像[/URL]指定所需的填充颜色(请参见下文)。但是,您可以使用'replicate'参数使[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/padarray.html"]PADARRAY[/URL]简单地在要添加填充的图像边缘复制颜色:

[r,c,d] = size(rgbImage); %# Get the image dimensions nPad = abs(cr)/2; %# The padding size if c > r %# Pad rows newImage = padarray(rgbImage,[floor(nPad) 0],... %# Pad top 'replicate','pre'); newImage = padarray(newImage,[ceil(nPad) 0],... %# Pad bottom 'replicate','post'); elseif r > c %# Pad columns newImage = padarray(rgbImage,[0 floor(nPad)],... %# Pad left 'replicate','pre'); newImage = padarray(newImage,[0 ceil(nPad)],... %# Pad right 'replicate','post'); end 该解决方案适用于[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-17587"]索引[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]图像[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-13941"]灰度[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]图像[/URL]或[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-33397"]二进制图像[/URL] 。对于这三种图像类型,您可以选择将'replicate'参数替换为要用于填充的标量值(即uint8(255)用于灰度图像中的白色填充)。对于[URL="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f14-13543.html#f14-20224"]RGB图像[/URL] ,将'replicate'参数替换为单个值将仅允许您创建从白色到黑色的灰色阴影填充颜色(即1会创建白色填充)。



[url=https://stackoverflow.com/questions/2605202]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 03:00

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.