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:
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
ClassShapeClass:List<Point> contourPoints: Store the full set of contour points fromfindContours()(you can convert OpenCV’sMatOfPointto aList<Point>easily withmatOfPoint.toList())Rect boundingRect: Directly use OpenCV’s built-inRectclass to hold the bounding rectangle’s x/y coordinates, width, and height (this is exactly whatboundingRect()returns)String className(optional): Reserve a field for the class name if you plan to add OCR text recognition laterList<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.
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
RelationshipLineClass:Point startPoint: The starting coordinate of the line (typically on the edge of a sourceClassShape)Point endPoint: The ending coordinate of the line (on the edge of a targetClassShape)List<Point> lineSegments: For polyline relationships, store all intermediate points that make up the lineenum ArrowType: Define an enum likeINHERITANCE,ASSOCIATION,DEPENDENCYto 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
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 classesMap<ClassShape, List<RelationshipLine>> classRelationshipMap: A map that links eachClassShapeto all theRelationshipLineinstances 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
ClassShapeinstances (check contour area withImgproc.contourArea()) to avoid treating noise as class shapes - Override
equals()andhashCode()in yourClassShapeandRelationshipLineclasses if you need to do set operations or lookups in collections - If you’re dealing with complex diagrams, consider adding a
Diagramclass to wrap all theallClassShapesandclassRelationshipMap—this keeps your code clean and modular
内容的提问来源于stack exchange,提问作者Nicholas Milner

