怎么在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.pswp.cn/news/275588.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275588.shtml
英文地址,請注明出處:http://en.pswp.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()是查找已…