在学习AI视觉算法入门与调优中,记录一下常用函数和思路。
将这个图片转为YUV通道
python代码如下:
import cv2
import matplotlib.pyplot as plt
# 读取 RGB 图像
rgb_image = cv2.imread("./cats.png")
# 将 RGB 图像转换为 YUV 格式
yuv_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2YUV)
# 分离 Y、U、V 通道
y_channel, u_channel, v_channel = cv2.split(yuv_image)
# 显示原始 RGB 图像和 Y、U、V 通道
plt.subplot(221)
plt.imshow(cv2.cvtColor(rgb_image, cv2.COLOR_BGR2RGB))
plt.title("Original RGB Image")
plt.subplot(222)
plt.imshow(y_channel, cmap="gray")
plt.title("Y Channel")
plt.subplot(223)
plt.imshow(u_channel, cmap="gray")
plt.title("U Channel")
plt.subplot(224)
plt.imshow(v_channel, cmap="gray")
plt.title("V Channel")
plt.show()
结果如下:
遇到的问题
当使用了一个安装了PyQt的python虚拟环境时,可能会报与PYQT相关的错误, 如下:
qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "/path/to/some/dir/lib/python3.11/site-packages/cv2/qt/plugins"
QObject::moveToThread: Current thread (0x245a670) is not the object's thread (0x2465c60).
Cannot move to target thread (0x245a670)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/path/to/some/dir/lib/python3.11/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb, eglfs, minimal, minimalegl, offscreen, vnc, webgl.
[1] 578474 IOT instruction (core dumped) python rgb2yuv.py
解决方案
新建一个虚拟环境,只安装 opencv-python 和 matplotlib 这两个包就行!
最后
Great! 又掌握一个知识点!