opencv 读取链接地址图片
时间: 2023-09-21 16:12:49 浏览: 58
你可以使用OpenCV中的`imread()`函数来读取链接地址图片。具体步骤如下:
1. 导入OpenCV库:
```python
import cv2
```
2. 从链接地址中读取图片:
```python
import urllib.request
url = 'https://example.com/image.jpg' # 替换成你的图片链接地址
resp = urllib.request.urlopen(url)
img = np.asarray(bytearray(resp.read()), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
```
这里使用了Python的`urllib.request`库从链接地址中读取图片数据,并使用`imdecode()`函数将图片数据解码为OpenCV中的`Mat`对象。
3. 显示图片:
```python
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这里使用了`imshow()`函数显示图片,并使用`waitKey()`函数等待用户按下任意键后关闭窗口。`destroyAllWindows()`函数可关闭所有OpenCV窗口。
完整代码示例:
```python
import cv2
import urllib.request
url = 'https://example.com/image.jpg' # 替换成你的图片链接地址
resp = urllib.request.urlopen(url)
img = np.asarray(bytearray(resp.read()), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)