全部來自ilovematlab中文論壇,其中matlab較新的版本不再支持其中的有些函數,或者說有些函數的參數做了改變,因此適當修改后貼出來。注:無意侵權,記錄在此自我學習,順帶服務群眾。

首先是尋找最小外接矩形的代碼:

function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)

% minboundrect: Compute the minimal bounding rectangle of points in the plane

% usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric)

%

% arguments: (input)

% x,y - vectors of points, describing points in the plane as

% (x,y) pairs. x and y must be the same lengths.

%

% metric - (OPTIONAL) - single letter character flag which

% denotes the use of minimal area or perimeter as the

% metric to be minimized. metric may be either 'a' or 'p',

% capitalization is ignored. Any other contraction of 'area'

% or 'perimeter' is also accepted.

%

% DEFAULT: 'a' ('area')

%

% arguments: (output)

% rectx,recty - 5x1 vectors of points that define the minimal

% bounding rectangle.

%

% area - (scalar) area of the minimal rect itself.

%

% perimeter - (scalar) perimeter of the minimal rect as found

%

%

% Note: For those individuals who would prefer the rect with minimum

% perimeter or area, careful testing convinces me that the minimum area

% rect was generally also the minimum perimeter rect on most problems

% (with one class of exceptions). This same testing appeared to verify my

% assumption that the minimum area rect must always contain at least

% one edge of the convex hull. The exception I refer to above is for

% problems when the convex hull is composed of only a few points,

% most likely exactly 3. Here one may see differences between the

% two metrics. My thanks to Roger Stafford for pointing out this

% class of counter-examples.

%

% Thanks are also due to Roger for pointing out a proof that the

% bounding rect must always contain an edge of the convex hull, in

% both the minimal perimeter and area cases.

%

%

% Example usage:

% x = rand(50000,1);

% y = rand(50000,1);

% tic,[rx,ry,area] = minboundrect(x,y);toc

%

% Elapsed time is 0.105754 seconds.

%

% [rx,ry]

% ans =

% 0.99994 -4.2515e-06

% 0.99998 0.99999

% 2.6441e-05 1

% -5.1673e-06 2.7356e-05

% 0.99994 -4.2515e-06

%

% area

% area =

% 0.99994

%

%

% See also: minboundcircle, minboundtri, minboundsphere

%

%

% Author: John D'Errico

% E-mail: woodchips@rochester.rr.com

% Release: 3.0

% Release date: 3/7/07

% default for metric

if (nargin<3) || isempty(metric)

metric = 'a';

elseif ~ischar(metric)

error 'metric must be a character flag if it is supplied.'

else

% check for 'a' or 'p'

metric = lower(metric(:)');

ind = strmatch(metric,{'area','perimeter'});

if isempty(ind)

error 'metric does not match either ''area'' or ''perimeter'''

end

% just keep the first letter.

metric = metric(1);

end

% preprocess data

x=x(:);

y=y(:);

% not many error checks to worry about

n = length(x);

if n~=length(y)

error 'x and y must be the same sizes'

end

% start out with the convex hull of the points to

% reduce the problem dramatically. Note that any

% points in the interior of the convex hull are

% never needed, so we drop them.

if n>3

edges = convhull(x,y);

%edges = convhull(x,y,{'Qt'}); % 'Pp' will silence the warnings

% exclude those points inside the hull as not relevant

% also sorts the points into their convex hull as a

% closed polygon

x = x(edges);

y = y(edges);

% probably fewer points now, unless the points are fully convex

nedges = length(x) - 1;

elseif n>1

% n must be 2 or 3

nedges = n;

x(end+1) = x(1);

y(end+1) = y(1);

else

% n must be 0 or 1

nedges = n;

end

% now we must find the bounding rectangle of those

% that remain.

% special case small numbers of points. If we trip any

% of these cases, then we are done, so return.

switch nedges

case 0

% empty begets empty

rectx = [];

recty = [];

area = [];

perimeter = [];

return

case 1

% with one point, the rect is simple.

rectx = repmat(x,1,5);

recty = repmat(y,1,5);

area = 0;

perimeter = 0;

return

case 2

% only two points. also simple.

rectx = x([1 2 2 1 1]);

recty = y([1 2 2 1 1]);

area = 0;

perimeter = 2*sqrt(diff(x).^2 + diff(y).^2);

return

end

% 3 or more points.

% will need a 2x2 rotation matrix through an angle theta

Rmat = @(theta) [cos(theta) sin(theta);-sin(theta) cos(theta)];

% get the angle of each edge of the hull polygon.

ind = 1:(length(x)-1);

edgeangles = atan2(y(ind+1) - y(ind),x(ind+1) - x(ind));

% move the angle into the first quadrant.

edgeangles = unique(mod(edgeangles,pi/2));

% now just check each edge of the hull

nang = length(edgeangles);

area = inf;

perimeter = inf;

met = inf;

xy = [x,y];

for i = 1:nang

% rotate the data through -theta

rot = Rmat(-edgeangles(i));

xyr = xy*rot;

xymin = min(xyr,[],1);

xymax = max(xyr,[],1);

% The area is simple, as is the perimeter

A_i = prod(xymax - xymin);

P_i = 2*sum(xymax-xymin);

if metric=='a'

M_i = A_i;

else

M_i = P_i;

end

% new metric value for the current interval. Is it better?

if M_i

% keep this one

met = M_i;

area = A_i;

perimeter = P_i;

rect = [xymin;[xymax(1),xymin(2)];xymax;[xymin(1),xymax(2)];xymin];

rect = rect*rot';

rectx = rect(:,1);

recty = rect(:,2);

end

end

% get the final rect

% all done

end % mainline end

上面我就修改了一個地方convhull函數的參數,使之能在matlab R2013b上順利運行。下面是一個測試樣例:

I=imread('3.jpg');

bw=im2bw(I);

[r c]=find(bw==1);

% 'a'是按面積算的最小矩形,如果按邊長用'p'

[rectx,recty,area,perimeter] = minboundrect(c,r,'p');

imshow(bw);hold on

line(rectx,recty);

就這樣吧!

matlab外接矩形,matlab求二值圖像最小外接矩形相关推荐

  1. 求取SHP文件的最小外接矩形并裁剪图像

    目的: 求取shp文件中每一个形状的最小外接矩形. 根据每一个形状的最小外接矩形裁剪图像. 已知数据: 一个shp文件,包含若干个形状. 2.shp文件对应的影像. 工具 ARCGIS10.4 pyt ...

  2. otus阈值分割matlab,OSTU最佳阈值法二值化原理-matlab和C | 学步园

    觉得这篇介绍OTSU方法挺清楚的.自己又加了一些,希望对初学者有帮助哦~ OTSU 1. OTSU算法原理简介 对于一幅图像,设当前景与背景的分割阈值为t时,前景点占图像比例为w0,均值为u0,背景点 ...

  3. Matlab:干涉条纹骨化算法|二值化|滤波|去毛刺

    干涉条纹原图(像素1024×1024,点击放大-右键"图片另存为"到桌面-文件名存为"1.bmp"):  干涉条纹骨化的结果.依次经过:二值化处理→滤波去噪→骨 ...

  4. matlab已知点求二维和三维中两直线夹角

    链接:http://blog.sina.com.cn/s/blog_994de1530101em5u.html (1)二维:theta=acosd(dot([x1-x2,y1-y2],[x3-x4,y ...

  5. 遍历图像 找最小外接矩形 matlab,2018a版本MatLab利用regionprops函数获取图片中物体轮廓最小外接矩形...

    2018a版本MatLab利用regionprops函数获取图片中物体最小外接矩形 本次内容,用于介绍利用matlab中的regionprops函数来获取图像区域中的物体的最小外接矩形信息(位置(x, ...

  6. 二值图像最小外接矩形(正)

    实现二值图像最小外接矩形(正) 用于目标识别 散点图画框 % 原文链接:https://blog.csdn.net/rosfreshman/article/details/116380981 原文效果 ...

  7. MATLAB二值图求figure图中任意图形面积

    MATLAB二值图求figure图中任意图形面积 问题描述 思路 代码 效果 问题描述 MATLAB的figure图遇到曲线是由离散点组成,没有解析表达式.想求曲线包围面积时无法直接编程求解.如下图 ...

  8. N圆最密堆积、最小外接正方形的matlab求解(二维、三维等圆Packing 问题)

    圆形最密堆积.最小外接正方形的matlab求解(二维.三维等圆Packing 问题) 0 前言 1 N个圆的最小外接正方形求解 2 N个球的最小外接立方体求解 惯例声明:本人没有相关的工程应用经验,只 ...

  9. 二值图填充原理 matlab,图像Ostu二值化原理及matlab实现代码

    Ostu假设图像是由前景区域和背景区域两部分组成的,通过遍历计算不同阈值(通常为[0 255]区间范围内)下分割结果中前景区域和背景区域的灰度直方图,然后比较两者之间的方差,使得方差最大化的那个灰度阈 ...

最新文章

  1. 使用 AngularJS 和 Electron 构建桌面应用
  2. 四管前级怎么去掉高低音音调_TDG Audio达芬奇:什么是前级,后极?
  3. 自己整理的排序算法之(1) 选择排序
  4. 配置VRRP(虚拟路由器冗余协议)
  5. 仿抖音视频自动播放html,vue 仿抖音视频播放切换
  6. Oracle 抢人了!近 4000 万年薪只为一个 AI 专家
  7. BootStrap的介绍与案例使用
  8. php分页3 1,经典php分页代码与分页原理(1/3)
  9. 第三次PR培训(添加常用效果和转场)
  10. 以下不是python3保留字的是_python 保留字
  11. 5大关键,让你二十年后依然是人才
  12. 手绘漫画学习 素描自学视频
  13. db2 日期英式写法_英式与美式日期写法 基数与序数词辨析
  14. 自学Python3脚本100例(1-10)
  15. 树的先序遍历(双亲表示法)
  16. 从零开始学架构 01-架构基础【笔记】
  17. 赛扬处理器_神舟推出优雅X4D2轻薄本,搭载赛扬处理器
  18. make编译打印详细日志
  19. 特殊人工智能英语教育
  20. 【python教程入门学习】线性回归算法详解

热门文章

  1. sql脚本过大,无法打开的解决方法
  2. vss登录invalid handle问题的解决办法
  3. 2021高值人才职业发展洞察:连接、信任与赋能
  4. 【报告分享】2020年抖音商业产品手册.pdf(附下载链接)
  5. 程序员,你得选准跑路的时间!
  6. 深度学习福利入门到精通第一讲——LeNet模型
  7. java基于ssm的个人博客系统_一个基于 Spring Boot 的开源免费博客系统
  8. Tiktok现阶段最简单的三种变现模式,小白也可轻松上手
  9. 外贸电商ERP都有哪些值得用?
  10. java service 初始化_【Java】Nacos – NacosNamingService初始化