opencv BGR 图像与其反向版本 RGB 图像 [:,:,::-1] 有什么区别? [英] What is the difference between an opencv BGR image and its reverse version RGB image[:,:,::-1]?

查看:34
本文介绍了opencv BGR 图像与其反向版本 RGB 图像 [:,:,::-1] 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示带有 QLabel 的 opencv 图像.我得到了两个不同版本的图像,第一个是 opencv BGR 图像,第二个是使用 image[:,:,::-1] 的 RGB 图像,BGR 版本工作正常但 RGB 版本不起作用.

以下代码工作正常

I'm trying to show an opencv image with a QLabel. I got two different versions of the image, first one is the opencv BGR image, the second one is the RGB image using image[:,:,::-1], the BGR version works fine but the RGB version doesn't work.

The following code works fine

src = cv.imread('image.jpg')
h,w,ch = src.shape
bytesPerLine = ch * w
qImg = QImage(src.data, w, h, bytesPerLine, QImage.Format_RGB888)
qImg = qImg.rgbSwapped()
self.ui.label.setPixmap(QPixmap.fromImage(qImg))


这些代码不起作用:

src = cv.imread('image.jpg')
src = src[:,:,::-1]
h,w,ch = src.shape
bytesPerLine = ch * w
qImg = QImage(src.data, w, h, bytesPerLine, QImage.Format_RGB888)
self.ui.label.setPixmap(QPixmap.fromImage(qImg))

推荐答案

正如您已经注意到 opencv 读取 BGR 格式的图像但 QImage 读取 RGB 格式的图像,在您的第一种方法中,您无需进行转换即可转换为 QImage,然后使用 rgbSwapped() 方法进行转换.

As you have noticed opencv reads the image in BGR format but QImage in RGB, in your first method you convert to QImage without doing the conversion and then you use rgbSwapped() method to do the conversion.

通过测试我得到的第一种方法:

By testing the first method I get:

1000 loops, best of 5: 291 usec per loop

<小时>

在第二种方法中,您尝试在将其转换为 QImage 之前执行此操作,但是当我执行它时,我收到以下错误,假设您也获得了它.


In the second method you try to do it before converting it to QImage but when I execute it I get the following error presuming that you also get it.

Traceback (most recent call last):
  File "xxxx.py", line 18, in <module>
    qImg = QtGui.QImage(src.data, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
TypeError: arguments did not match any overloaded call:
  QImage(): too many arguments
  QImage(QSize, QImage.Format): argument 1 has unexpected type 'memoryview'
  QImage(int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
  QImage(bytes, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
  QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
  QImage(bytes, int, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
  QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
  QImage(List[str]): argument 1 has unexpected type 'memoryview'
  QImage(str, format: str = None): argument 1 has unexpected type 'memoryview'
  QImage(QImage): argument 1 has unexpected type 'memoryview'
  QImage(Any): too many arguments

这是因为 numpy 使用 memoryview 来优化某些任务.在这种情况下,在执行 src[:,:,::-1] 时,一种优化方法不是修改数据而是访问数据的方式,这是通过 缓冲协议.

And this is because numpy uses memoryview to optimize certain tasks. And in this case when doing src[:,:,::-1] one way to optimize is not to modify the data but the way to access the data, this is done through the Buffer Protocol.

并且在这种情况下 QImage 不支持这种类型的数据,因此解决方案是使用 tobytes()bytes():

And in this case QImage does not support this type of data, so the solution is to access the bytes using the tobytes() or bytes():

import cv2
from PyQt5 import QtGui, QtWidgets

if __name__ == '__main__':
    import sys
    src = cv2.imread('image.jpg')
    src = src[:,:,::-1]
    h, w, ch = src.shape
    bytesPerLine = ch * w
    qImg = QtGui.QImage(src.data.tobytes(), w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
    # Or
    # qImg = QtGui.QImage(bytes(src.data), w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QLabel()
    w.setPixmap(QtGui.QPixmap.fromImage(qImg))
    w.show()
    sys.exit(app.exec_())

时间:

500 loops, best of 5: 523 usec per loop

<小时>

另一种解决方法是使用opencv的cvtColor()函数,如果修改数据:

import cv2
from PyQt5 import QtGui, QtWidgets

if __name__ == '__main__':
    import sys
    src = cv2.imread('image.jpg')
    src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
    h, w, ch = src.shape
    bytesPerLine = ch * w
    qImg = QtGui.QImage(src.data, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QLabel()
    w.setPixmap(QtGui.QPixmap.fromImage(qImg))
    w.show()
    sys.exit(app.exec_())

时间:

1000 loops, best of 5: 263 usec per loop

这篇关于opencv BGR 图像与其反向版本 RGB 图像 [:,:,::-1] 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆