
整张图都分析有时候只需要看特定区域。矩形、圆形、多边形——三种AOI覆盖大部分场景。一、AOI的作用用途说明排除干扰边缘暗角、划痕、污染物聚焦关键只分析样品特定区域加速分析区域小计算快分区统计不同区域分别分析二、三种AOI模式模式操作方式数据结构矩形拖拽{type:rectangle, x, y, w, h}圆形圆心→拖拽半径{type:circle, cx, cy, r}多边形点击顶点→双击闭合{type:polygon, points:[(x,y),...]}三、鼠标事件处理pythondef mousePressEvent(self, event): if self.calibration_mode: self._calibration_mouse_press(event) return pos self.mapToScene(event.pos()) x, y pos.x(), pos.y() if self.aoi_mode rectangle: if event.button() Qt.LeftButton: self.aoi_drawing True self.aoi_points [(x, y)] self._clear_aoi_items() pen QPen(Qt.red, 2) self.aoi_rect_item self.scene.addRect(x, y, 0, 0, pen) elif self.aoi_mode circle: if event.button() Qt.LeftButton: self.aoi_drawing True self.aoi_points [(x, y)] self._clear_aoi_items() pen QPen(Qt.red, 2) self.aoi_ellipse_item self.scene.addEllipse(x, y, 0, 0, pen) elif self.aoi_mode polygon: if event.button() Qt.LeftButton: self.aoi_points.append((x, y)) self._update_polygon_item() def mouseMoveEvent(self, event): if not self.aoi_drawing: return pos self.mapToScene(event.pos()) x, y pos.x(), pos.y() if self.aoi_mode rectangle and self.aoi_rect_item: x0, y0 self.aoi_points[0] self.aoi_rect_item.setRect( min(x0, x), min(y0, y), abs(x - x0), abs(y - y0) ) elif self.aoi_mode circle and self.aoi_ellipse_item: cx, cy self.aoi_points[0] r int(((x - cx) ** 2 (y - cy) ** 2) ** 0.5) self.aoi_ellipse_item.setRect(cx - r, cy - r, 2 * r, 2 * r) def mouseDoubleClickEvent(self, event): if self.aoi_mode polygon and len(self.aoi_points) 3: self.aoi_shape { type: polygon, points: self.aoi_points.copy() } self.aoi_updated.emit(self.aoi_shape)四、AOI掩码生成pythondef get_aoi_mask(self, img_shape): if not self.aoi_shape: return None h, w img_shape[:2] mask np.zeros((h, w), dtypenp.uint8) if self.aoi_shape[type] rectangle: x, y int(self.aoi_shape[x]), int(self.aoi_shape[y]) w, h int(self.aoi_shape[w]), int(self.aoi_shape[h]) mask[y:yh, x:xw] 255 elif self.aoi_shape[type] circle: cx, cy, r int(self.aoi_shape[cx]), int(self.aoi_shape[cy]), int(self.aoi_shape[r]) y, x np.ogrid[:h, :w] mask[(x - cx) ** 2 (y - cy) ** 2 r ** 2] 255 elif self.aoi_shape[type] polygon: pts np.array([[int(p[0]), int(p[1])] for p in self.aoi_shape[points]], np.int32) cv2.fillPoly(mask, [pts], 255) return mask五、模式互斥优先级标定模式 AOI模式 默认视图操作标定完成后必须调用end_calibration()否则AOI被拦截。下篇预告下一篇写识别结果叠加显示技术多Tab展示、颜色方案、图像保存。