摘要:最小外接矩形外接矩形計(jì)算對(duì)一個(gè)凸多邊形進(jìn)行外接矩形計(jì)算,需要知道當(dāng)前面的最大和最小值,即可獲得外接矩形最小外接矩形計(jì)算對(duì)凸多邊形的每一條邊都繪制一個(gè)外接矩形求最小面積。
最小外接矩形 外接矩形計(jì)算
對(duì)一個(gè)凸多邊形進(jìn)行外接矩形計(jì)算,需要知道當(dāng)前面的最大xy 和最小xy值,即可獲得外接矩形
最小外接矩形計(jì)算對(duì)凸多邊形的每一條邊都繪制一個(gè)外接矩形求最小面積。下圖展示了計(jì)算流程
計(jì)算流程
旋轉(zhuǎn)基礎(chǔ)算法實(shí)現(xiàn)
旋轉(zhuǎn)點(diǎn)基礎(chǔ)
/** * 旋轉(zhuǎn)點(diǎn) * * @param point 被旋轉(zhuǎn)的點(diǎn) * @param center 旋轉(zhuǎn)中心 * @param angle 角度 * @return 旋轉(zhuǎn)后坐標(biāo) */ public static Coordinate get(Coordinate point, Coordinate center, double angle) { double cos = Math.cos(angle); double sin = Math.sin(angle); double x = point.x; double y = point.y; double centerX = center.x; double centerY = center.y; return new Coordinate(centerX + cos * (x - centerX) - sin * (y - centerY), centerY + sin * (x - centerX) + cos * (y - centerY)); }
凸包算法實(shí)現(xiàn)
Geometry hull = (new ConvexHull(geom)).getConvexHull();
獲得結(jié)果
public static Polygon get(Geometry geom, GeometryFactory gf) { Geometry hull = (new ConvexHull(geom)).getConvexHull(); if (!(hull instanceof Polygon)) { return null; } Polygon convexHull = (Polygon) hull; System.out.println(convexHull); // 直接使用中心值 Coordinate c = geom.getCentroid().getCoordinate(); System.out.println("==============旋轉(zhuǎn)基點(diǎn)=============="); System.out.println(new GeometryFactory().createPoint(c)); System.out.println("==============旋轉(zhuǎn)基點(diǎn)=============="); Coordinate[] coords = convexHull.getExteriorRing().getCoordinates(); double minArea = Double.MAX_VALUE; double minAngle = 0; Polygon ssr = null; Coordinate ci = coords[0]; Coordinate cii; for (int i = 0; i < coords.length - 1; i++) { cii = coords[i + 1]; double angle = Math.atan2(cii.y - ci.y, cii.x - ci.x); Polygon rect = (Polygon) Rotation.get(convexHull, c, -1 * angle, gf).getEnvelope(); double area = rect.getArea(); // 此處可以將 rotationPolygon 放到list中求最小值 // Polygon rotationPolygon = Rotation.get(rect, c, angle, gf); // System.out.println(rotationPolygon); if (area < minArea) { minArea = area; ssr = rect; minAngle = angle; } ci = cii; } return Rotation.get(ssr, c, minAngle, gf); }
測(cè)試類(lèi)
@Test public void test() throws Exception{ GeometryFactory gf = new GeometryFactory(); String wkt = "POLYGON ((87623.0828822501 73753.4143904365,87620.1073981973 73739.213216548,87629.1690996309 73730.4220136646,87641.882531493 73727.3112803367,87643.0997749692 73714.8683470248,87662.0346734872 73725.0120426595,87669.0676357939 73735.1557382941,87655.9484561064 73735.9672339449,87676.9120937514 73747.4634223308,87651.8909778525 73740.8362078495,87659.4649372597 73755.4431295634,87644.4522677204 73748.680665807,87645.5342619215 73760.7178512935,87635.2553170117 73750.9799034842,87630.5215923822 73760.3121034681,87623.0828822501 73753.4143904365))"; Polygon read = (Polygon) new WKTReader().read(wkt); Polygon polygon = MinimumBoundingRectangle.get(read, gf); // System.out.println(polygon); // System.out.println(polygon.getArea()); }注
本文代碼及可視化代碼均放在 gitee 碼云上 歡迎star & fork
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/73422.html
摘要:因此,邊界矩形的面積不會(huì)最小設(shè),為矩形的左上角坐標(biāo),,為寬度和高度代碼最小外接矩形返回一個(gè)結(jié)構(gòu),其中包含以下,,,,畫(huà)上述矩形代碼最小封閉圈擬合橢圓擬合直線(xiàn) Contour Features 1 圖像的矩 cv2.moments()圖像的矩可以幫助計(jì)算物體的某些特征,如對(duì)象的質(zhì)心,對(duì)象的區(qū)域等. 代碼: import cv2 import numpy as np img = cv2...
摘要:請(qǐng)注意,和最大值和最小值及它們的位置我們可以使用掩模圖像得到這些參數(shù)平均顏色或平均強(qiáng)度在這里,我們可以找到對(duì)象的平均顏色。我們?cè)俅问褂醚谀M瓿伤鼧O點(diǎn)目標(biāo)最上面,最下面,最左邊,最右邊的點(diǎn) Contour Properties 1 縱橫比 它是對(duì)象的邊界矩形的寬度與高度的比率. $$ Rspect Ratio = frac{Width}{Height} $$ x,y,w,h = cv2...
摘要:之前項(xiàng)目剛寫(xiě)了個(gè)判斷觸底自動(dòng)加載更多的功能,發(fā)現(xiàn)自己對(duì)各種寬高的定義還是很模糊。嗯,就這樣過(guò)程中還是有很大收獲的 之前項(xiàng)目剛寫(xiě)了個(gè)判斷觸底自動(dòng)加載更多的功能,發(fā)現(xiàn)自己對(duì)各種寬、高的定義還是很模糊。終于沒(méi)有偷懶,寫(xiě)了個(gè)demo理解了一下。網(wǎng)上也有很多整理好的文章,為了加強(qiáng)下自身記憶,順便也許能給些建議。 element的一些寬、高屬性注:盒子模型: content+padding+mar...
閱讀 590·2023-04-25 21:29
閱讀 1122·2023-04-25 21:27
閱讀 1062·2021-11-25 09:43
閱讀 1098·2021-09-29 09:43
閱讀 3631·2021-09-03 10:30
閱讀 2870·2019-08-29 15:26
閱讀 2815·2019-08-29 12:52
閱讀 1760·2019-08-29 11:10