怎么在matlab中图像中外接矩形,Matlab 最小外接矩形

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.

%

%

% See also: minboundcircle, minboundtri, minboundsphere

%

%

% 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

% if var(x)==0

% 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

%%%%%%%%%%%%%%%%%%%%%%%%%

if (var(x)== 0|| var(y)==0)

if var(x)== 0

x = [x-1;x(1); x+1 ];

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

flag = 1;

else

y = [y-1;y(1); y+1 ];

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

flag = 1;

end

else

flag = 0;

%%%%%%%%%%%%%%%%%%%%%%

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

end

% exclude those points inside the hull as not relevant

% also sorts the points into their convex hull as a

% closed polygon

%%%%%%%%%%%%%%%%%%%%

if flag == 0

%%%%%%%%%%%%%%%%%%%%

x = x(edges);

y = y(edges);

%%%%%%%%%%%%%%%%%%

end

%%%%%%%%%%%%%

% 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

当然这段代码并没有获取到外接矩形的长和宽,下面我在写一个函数,就可以获得对应外接矩形的长和宽

function [ wid hei ] = minboxing( d_x , d_y )

%minboxing Summary of this function goes here

% Detailed explanation goes here

dd = [d_x, d_y];

dd1 = dd([4 1 2 3],:);

ds = sqrt(sum((dd-dd1).^2,2));

wid = min(ds(1:2));

hei = max(ds(1:2));

end

这里默认为较短的距离为宽,较长的距离为长。

调用代码如下:注(dataX, dataY为需要计算最小外接矩形的数据。)

[recty,rectx,area,perimeter] = minboundrect(dataX, dataY);

[wei hei] = minboxing(rectx(1:end-1),recty(1:end-1));

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275588.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

尤雨溪开发的 vue-devtools 如何安装,为何打开文件的功能鲜有人知?

1. 前言大家好&#xff0c;我是若川。最近组织了一次源码共读活动。每周读 200 行左右的源码。很多第一次读源码的小伙伴都感觉很有收获&#xff0c;感兴趣可以加我微信 ruochuan12&#xff0c;拉你进群学习。第一周读的是&#xff1a;据说 99% 的人不知道 vue-devtools 还能直…

sketch浮动布局_使用智能布局和调整大小在Sketch中创建更好的可重用符号

sketch浮动布局Sketch is a widely used tool for UI designs. It implemented the Sketch是用于UI设计的广泛使用的工具。 它实施了 atomic design methodology and made the workflow of UI design much more efficient. You can create a Symbol in Sketch and use it ever…

用Sql添加删除字段,判断字段是否存在的方法

增加字段alter table docdsp add dspcode char(200)删除字段ALTER TABLE table_NAME DROP COLUMN column_NAME修改字段类型ALTER TABLE table_name ALTER COLUMN column_name new_data_type改名sp_rename更改当前数据库中用户创建对象&#xff08;如表、列或用户定义数据类型…

小姐姐笔记:我是如何学习简单源码拓展视野的

大家好&#xff0c;我是若川。这是我上周组织的源码共读纪年小姐姐的笔记&#xff0c;写得很好。所以分享给大家。欢迎加我微信 ruochuan12&#xff0c;进源码共读群。其他更多人的笔记可以阅读原文查看。川哥的源码解读文章&#xff1a;据说 99% 的人不知道 vue-devtools 还能…

php表决器代码,三人表决器:VHDL源代码

描述--三人表决器(三种不同的描述方式) vhdl-- Three-input Majority Voter-- The entity declaration is followed by three alternative architectures which achieve the same functionality in different ways.ENTITY maj ISPORT(a,b,c : IN BIT; m : OUT BIT);END maj;--D…

保持危机感和紧迫感_什么是紧迫的:您需要知道的一切

保持危机感和紧迫感Putting the finishing touches on a graphic design project calls for a keen eye. But you already know this, because perfectionism is just a part of the job! You look at every nook and cranny of a project before you can consider it complete…

剑指offer java版(一)

二维数组中的查找 问题描述 在一个二维数组中&#xff08;每个一维数组的长度相同&#xff09;&#xff0c;每一行都按照从左到右递增的顺序排序&#xff0c;每一列都按照从上到下递增的顺序排序。请完成一个函数&#xff0c;输入这样的一个二维数组和一个整数&#xff0c;判断…

如何系统搭建现代 Web CI/CD

大家好&#xff0c;我是若川。今天分享一篇00后写的CI/CD直播文字稿。之前发过他的故事&#xff1a;一位00后前端2年经验的成长历程。我最近组织了源码共读活动&#xff0c;感兴趣的加我微信 ruochuan12。本次直播录播链接&#xff1a;https://live.juejin.cn/4354/595741[1]开…

sqlserver oracle 数据类型对应关系,SQLSERVER和ORACLE数据类型对应关系详解和对应表格整理...

Oracle SQLServer 比较 SQLServer 常见的 数据 库 类型 字符 数据 类型 CHAR CHAR :都是固定长度字符资料但oracle里面最大度为2kb&#xff0c;SQLServer里面最大长度为8kb 变长字符 数据 类型 VARCHAR2 VARCHAR :racle里面最大长度为4kb&#xff0c;SQLServer里面最大长度为8k…

优化算法汇总

interior point block coordinate relaxation Boltzmann machine 求解L1范数最小化 E. Candes, M. B. Wakin, and S. P. Boyd, “Enhancing sparsity by reweighted l1 minimization,” Journalof Fourier Analysis and Applications, vol. 14, pp. 877-905, Dec. 2008.I. Daub…

对接百度地图API

一、准备工作 百度地图开发文档 注册百度账号&#xff0c;成为开发人员&#xff0c;同时获取AK实例代码&#xff1a;<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content&quo…

ui边框设计图_UI设计形状和对象基础知识:填充和边框

ui边框设计图第2部分 (Part 2) Welcome to the second part of the UI Design shapes basics. This time we’ll cover two of the most essential properties of a shape — fills and borders. This is also a part of the free chapters from Designing User Interfaces.欢迎…

如何移除项目中无用的 console.log 代码

大家好&#xff0c;我是若川。早些天时&#xff0c;我看到一个后端公众号发《辞退了一个前端》&#xff0c;当时还想着现在后端公众号都开始吊打前端了嘛。其中有个理由就是线上还一堆console.log...我猜很多人都会移除项目中无用的console.log。可以复习一下。前言说起console…

WCF - 服务实例管理模式

WCF 提供了三种实例上下文模式&#xff1a;PreCall、PreSession 以及 Single。开发人员通过 ServiceBehavior.InstanceContextMode 就可以很容易地控制服务对象的实例管理模式。而当 WCF 释放服务对象时&#xff0c;会检查该对象是否实现了 IDisposable 接口&#xff0c;并调用…

oracle io lost,磁盘IO故障

测试工作正在如火如荼的进行&#xff0c;突然数据库就连接不上了。我连接上主机发现数据库alert_sid日志中有如下信息&#xff1a;KCF: write/open error block0x9a6 online1file2 /oracle_data1/UNDOTBS3.dbferror27072 txt: Linux Error: 5: Input/output errorAdditional in…

易思汇完成近亿元B轮融资,信中利投资

3月19日消息&#xff0c;近日&#xff0c;留学生在线付费平台易思汇宣布已在3月份完成由信中利投资的近亿元B轮融资。 易思汇联合创始人高宇同表示&#xff0c;本轮融资将主要用于留学生信用卡、留学家庭金融商城等新产品布局&#xff0c;以及扩大团队和市场投入。 易思汇成立…

远程连接 错误 内部错误_关于错误的性质和原因。 了解错误因素

远程连接 错误 内部错误Back in 2012, I was a young[er] product designer working in a small tech agency in Valencia, Spain. In parallel, I worked as a freelancer on several side projects for different clients. One day I was contacted by a new health services…

得到鹅厂最新前端开发手册一份

又逢金九银十&#xff0c;拿到大厂offer一直是程序员朋友的目标&#xff0c;但是去大厂就得拿出实力来。除了需要积累技术&#xff0c;了解并掌握面试的技巧&#xff0c;熟悉大厂面试流程&#xff0c;也必不可少。这里分享一份最新入职腾讯的前端社招面经&#xff0c;来看看鹅厂…

性能测试分析之带宽瓶颈的疑惑

第一部分&#xff0c; 测试执行 先看一图&#xff0c;再看下文 这个当然就是压力过程中带宽的使用率了&#xff0c;我们的带宽是1Gbps的&#xff0c;合计传输速率为128MB/s&#xff0c;也正因为这个就让我越来越疑惑了&#xff0c;不过通过压力过程中的各项数据我又不得不相信。…

Android 中的LayoutInflater的理解

LayoutInflater与findViewById的区别&#xff1f; 对于一个已经载入的界面&#xff0c;就可以使用findViewById()方法来获得其中的界面元素。对于一个没有被载入或者想要动态载入的界面&#xff0c;就需要使用LayoutInflater对象的inflate()方法来载入。findViewById()是查找已…