opencv roi C++
时间: 2023-09-03 18:06:21 浏览: 120
CyyROICut.zip
要提取ROI区域,可以使用OpenCV的cv2.rectangle函数来绘制矩形框,然后使用numpy数组的切片操作来提取ROI区域。具体步骤如下:
1. 读入图像并显示
```python
import cv2
img = cv2.imread('image.jpg')
cv2.imshow('image', img)
cv2.waitKey()
```
2. 绘制矩形框
```python
x, y, w, h = 100, 100, 200, 200 # 矩形框左上角坐标和宽高
cv2.rectangle(img, (x, y), (x+w, y+h), (, 255, ), 2) # 绘制矩形框
cv2.imshow('image', img)
cv2.waitKey()
```
3. 提取ROI区域
```python
roi = img[y:y+h, x:x+w] # 提取ROI区域
cv2.imshow('ROI', roi)
cv2.waitKey()
```
完整代码如下:
```python
import cv2
img = cv2.imread('image.jpg')
cv2.imshow('image', img)
x, y, w, h = 100, 100, 200, 200 # 矩形框左上角坐标和宽高
cv2.rectangle(img, (x, y), (x+w, y+h), (, 255, ), 2) # 绘制矩形框
cv2.imshow('image', img)
roi = img[y:y+h, x:x+w] # 提取ROI区域
cv2.imshow('ROI', roi)
cv2.waitKey()
cv2.destroyAllWindows()
```
阅读全文