黑白反转opencv
时间: 2023-08-31 12:08:18 浏览: 316
OpenCV图像颜色反转算法详解
要实现黑白反转,可以使用OpenCV库。以下是一个使用OpenCV实现黑白反转的示例代码:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg', 1)
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 反转图像
dst = 255 - gray
# 显示反转后的图像
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码读取名为`image.jpg`的图像,将其转换为灰度图像,并通过计算最大灰度值减去原图像的灰度值,得到反转后的图像。最后使用`cv2.imshow`函数显示反转后的图像。按下任意键关闭图像窗口。<em>1</em><em>2</em>
#### 引用[.reference_title]
- *1* [黑白图片颜色反转并保存](https://blog.csdn.net/qq_41264055/article/details/121504639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
- *2* [利用opencv将图片黑白色反转并显示、保存](https://blog.csdn.net/m0_64170094/article/details/125474142)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文