left_point_x = np.min(box[:, 0]) right_point_x = np.max(box[:, 0]) top_point_y = np.min(box[:, 1]) bottom_point_y = np.max(box[:, 1]) 如何绘制出轮廓
时间: 2024-03-09 20:50:28 浏览: 26
要绘制出轮廓,可以使用cv2.rectangle函数,该函数需要传入轮廓的左上角和右下角坐标,即left_point_x、top_point_y和right_point_x、bottom_point_y,以及矩形框的颜色和线宽等参数。例如,可以使用以下代码绘制出矩形轮廓:
```
cv2.rectangle(image, (left_point_x, top_point_y), (right_point_x, bottom_point_y), (0, 255, 0), 2)
```
其中,image是需要绘制轮廓的图像,(0, 255, 0)是矩形框的颜色,2是线宽。
相关问题
python opencv 帮我写一份代码 ,要求如下 假设img_test经过滤波后进行轮廓提取 假设提取后有有rect = cv2.minAreaRect(contour)、box = cv2.boxPoints(rect)、box = np.round(box).astype('int64') 通过box 有矩形A:left_point_x = np.min(box[:, 0])、right_point_x = np.max(box[:, 0])、top_point_y = np.min(box[:, 1])、bottom_point_y = np.max(box[:, 1]) 假设有点(x1、y1) 判断 x1是否在right_point_x与left_point_x之内 若不在 则将x1平移到离矩形A最近的一条边上 同理 判断y1是否在top_point_y与bottom_point_y之内 若不在 则将y1平移到x1平移之后所在的边上
好的,以下是 Python OpenCV 代码实现:
```python
import cv2
import numpy as np
# 假设img_test经过滤波后进行轮廓提取,contour为轮廓
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.round(box).astype('int64')
# 获取矩形的四个顶点坐标
left_point_x = np.min(box[:, 0])
right_point_x = np.max(box[:, 0])
top_point_y = np.min(box[:, 1])
bottom_point_y = np.max(box[:, 1])
# 假设有点(x1, y1)
x1 = 50
y1 = 100
# 判断x1是否在right_point_x与left_point_x之内
if x1 < left_point_x or x1 > right_point_x:
# 将x1平移到离矩形A最近的一条边上
if x1 < left_point_x:
x1 = left_point_x
else:
x1 = right_point_x
# 判断y1是否在top_point_y与bottom_point_y之内
if y1 < top_point_y or y1 > bottom_point_y:
# 将y1平移到x1平移之后所在的边上
if y1 < top_point_y:
y1 = top_point_y
else:
y1 = bottom_point_y - (bottom_point_y - y1)
# 打印结果
print("平移后的点坐标为:", x1, y1)
```
其中,示例中的点坐标是随便设定的,实际应用中需要根据实际情况获取。
def get_bbox(height, width, points): polygons = points mask = np.zeros([height, width], dtype=np.uint8) mask = PIL.Image.fromarray(mask) xy = list(map(tuple, polygons)) PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1) mask = np.array(mask, dtype=bool) index = np.argwhere(mask == 1) rows = index[:, 0] clos = index[:, 1] left_top_r = np.min(rows) left_top_c = np.min(clos) right_bottom_r = np.max(rows) right_bottom_c = np.max(clos) return [ left_top_c, left_top_r, right_bottom_c - left_top_c, right_bottom_r - left_top_r ]
这段代码是一个函数,用于从图像中给定的多边形 points 中提取出包含该多边形的最小矩形 bounding box 的位置和大小信息。具体来说,输入参数包括图像的高度 `height`、宽度 `width`,以及多边形的顶点坐标 `points`。函数首先将多边形 points 转换为一个二值掩模图像,其中多边形内部的像素值为 1,其余像素值为 0。然后,通过 PIL 库中的 ImageDraw.Draw.polygon 函数,将多边形的轮廓绘制在掩模图像上,生成一个包含多边形的二值掩模。接着,函数将该二值掩模转换为一个布尔类型的 numpy 数组 `mask`。然后,函数根据 `mask` 中像素值为 1 的位置,计算出多边形的最小矩形 bounding box 的位置和大小信息,即左上角坐标 `(left_top_c, left_top_r)` 和宽高 `(right_bottom_c - left_top_c, right_bottom_r - left_top_r)`,并将其打包成一个列表返回。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)