You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中类图形状坐标识别存储及合适数据结构选型咨询

Great question! Since you're building a parameterized image processor for UML class diagrams (handling JPG/PNG, etc.) and already have OpenCV's edge detection tools like findContours(), drawContours(), and bounding rectangles implemented in Java, here’s what I’d recommend for data structures to store shapes and lines with their coordinate details:

1. Data Structures for Class Shapes

Since class diagrams use rectangular shapes for classes, a custom POJO (Plain Old Java Object) tailored to your OpenCV workflow is perfect. It’ll play seamlessly with the data you’re already extracting:

  • Custom ClassShape Class:
    • List<Point> contourPoints: Store the full set of contour points from findContours() (you can convert OpenCV’s MatOfPoint to a List<Point> easily with matOfPoint.toList())
    • Rect boundingRect: Directly use OpenCV’s built-in Rect class to hold the bounding rectangle’s x/y coordinates, width, and height (this is exactly what boundingRect() returns)
    • String className (optional): Reserve a field for the class name if you plan to add OCR text recognition later
    • List<String> attributes (optional): Similarly, leave space for storing extracted class attributes or methods

This approach keeps your data tightly coupled with OpenCV’s API, so you won’t waste time converting between custom types and library types.

2. Data Structures for Relationship Lines

Class diagram relationships (inheritance, association, dependency, etc.) are lines (sometimes with arrows or other markers). Another custom POJO will let you capture all critical details:

  • Custom RelationshipLine Class:
    • Point startPoint: The starting coordinate of the line (typically on the edge of a source ClassShape)
    • Point endPoint: The ending coordinate of the line (on the edge of a target ClassShape)
    • List<Point> lineSegments: For polyline relationships, store all intermediate points that make up the line
    • enum ArrowType: Define an enum like INHERITANCE, ASSOCIATION, DEPENDENCY to mark the type of relationship (you can infer this from the arrow/marker shape later)
    • String relationshipLabel (optional): Store any text labels attached to the relationship line
3. Container Structures for Global Management

To keep all your parsed diagram elements organized for processing, use these standard Java collections:

  • List<ClassShape> allClassShapes: A simple list to hold every class shape you detect—great for iterating through when matching lines to classes
  • Map<ClassShape, List<RelationshipLine>> classRelationshipMap: A map that links each ClassShape to all the RelationshipLine instances connected to it. This makes it easy to quickly look up all relationships for a given class.

Quick Pro Tips

  • Filter out tiny contours before creating ClassShape instances (check contour area with Imgproc.contourArea()) to avoid treating noise as class shapes
  • Override equals() and hashCode() in your ClassShape and RelationshipLine classes if you need to do set operations or lookups in collections
  • If you’re dealing with complex diagrams, consider adding a Diagram class to wrap all the allClassShapes and classRelationshipMap—this keeps your code clean and modular

内容的提问来源于stack exchange,提问作者Nicholas Milner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:42:52