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));

怎么在matlab中图像中外接矩形,Matlab 最小外接矩形相关推荐

  1. opencv 图像轮廓特征 图像面积,轮廓周长,外接矩形、最小外接矩形、最小外接圆、拟合椭圆

    找出图像轮廓 contours, hierarchy = cv.findContours(thresh, 3, 2) 画出图像轮廓 cnt = contours[1] cv.drawContours( ...

  2. 【千律】OpenCV基础:图像外接矩形、最小外接矩形、凸包、外接圆、拟合椭圆的绘制

    环境:Python3.8 和 OpenCV 内容:图像外接矩形.最小外接矩形.凸包.外接圆.拟合椭圆的绘制 import cv2 as cv import numpy as np import mat ...

  3. 【图像修复】基于matlab损坏图像修复【含Matlab源码 731期】

    一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[图像修复]基于matlab损坏图像修复[含Matlab源码 731期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: 付费专栏 ...

  4. 【OpenCV】 外接矩形、最小外接矩形、多边形拟合、外接圆

    任务:给定这样一张图片求图片中白色区域的外接矩形.最小外接矩形.拟合多边形以及外接圆 1.外接矩形 x, y, w, h = cv2.boundingRect(points) 输入:点集 返回值:左上 ...

  5. OpenCV中图像Mat存储格式和MATLAB中图像Mat存储格式的区别

    首先,看一下图像中的宽高与笛卡尔坐标系之间的关系如下图所示,即x与width(cols)对应,y与height(rows)对应,x是按列来进行变化,y按行变化. OpenCV读入图像以Mat形式存储时 ...

  6. matlab中删除照片_如何使用matlab从图像中删除划痕

    如果您知道划痕的位置,则此问题称为 inpainting,并且存在非常复杂的算法.因此,一种方法是尽可能好地检测划痕,然后在其上使用标准的修复算法.我在Mathematica中玩了一下你的形象: 首先 ...

  7. 设置matlab图像线框,matlab在图像中画长方形(框)

    function [state,result]=draw_rect(data,pointAll,windSize,showOrNot) % 函数调用:[state,result]=draw_rect( ...

  8. matlab——识别图像中的圆形目标

    文章目录 说明 Figure 1 imread函数 imshow函数 Figure 2 rgb2gray函数 graythresh函数 im2bw函数 figure函数 Figure 3 bwarea ...

  9. matlab中图像映射实例,[转载]Matlab实现多种图像配准(转)

    本文讲述如何利用Matlab Image Processing Toolbox中的图像配准工具实现线性正投影.仿射.投影.多项式.分段线性.局部加权平均配准的过程. 实验平台 X86 PC,Windo ...

最新文章

  1. 实用工具类库java.util
  2. Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框
  3. Javascript 深入学习循环
  4. Linux格式化异常,Linux下DateFormat的parse方法出现”ParseException”异常
  5. python操作hive表_python处理数据,存进hive表的方法
  6. Tomcat运行时报内存溢出
  7. leetcode container-with-most-water(medium) /java
  8. Docker1.12.6+CentOS7.3 的安装
  9. 设计模式:JavaScript
  10. 含蓄的告别,google今日LOG
  11. pdf文件展示盖章及下载
  12. linux添加jetdirect协议,如何设置 HP JetDirect 设备的网络安全性?
  13. QNX Hypervisor —— 物理设备
  14. 《繁荣的真相》读书笔记
  15. 设计点类 Point,能够表示平面当中的任意点
  16. R安装并行计算工具包snowfall实现并行运算资源
  17. sqlserver数据库错误码
  18. win10安装docker导致virtualbox无法启动问题解决
  19. 集线器、中继器、网桥、交换机、网关、路由器——今天必把你们区分开
  20. 王牌竞速安装后显示服务器维护,王牌竞速服务器进不了 服务器登录问题详解...

热门文章

  1. 获取GridView中RowCommand的当前选中行的索引或主键Id
  2. Linux RTC 驱动实验
  3. sql 2008找不到服务器,sql server 2005 数据库迁移问题总结——错误 ‘80004005’ 在 sys.servers 中找不到服务器 ‘XXX’...
  4. oracle00109,ORA-01034: 、ORA-01078: 和 LRM-00109: 的解决方法,ora-01034ora-01078
  5. Java DO到DTO转换利用spring 的BeanUtils.copyProperties
  6. Idea运行项目报错:java.lang.OutOfMemoryError: Java heap space/ java.lang.OutOfMemoryError: GC overhead 解决方法
  7. Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
  8. APP技巧:盘点微信去年更新的9个更新功能,你都知道吗?
  9. 电脑知识:新电脑数据迁移解决方案,看完你就会了!
  10. 前端:前端安全编码规范