cvtColor用于各种图像之间的转换,比如将OpenCV中的BGR图像转换为常用的RGB、YUV等图像,也能用于将彩色图像转换为灰度图像(Grayscale)。
将GBR图像转换为灰度图像时通常使用如下语句:
q_image = cv.cvtColor(self.original_image, cv.COLOR_BGR2GRAY)
其中self.original_image是通过OpenCV的imread函数读取的RGB彩色图像。
然而将所得结果q_image转换为QImage对象时,由于此时的q_image为二维数组,没有深度,故获取其高、宽、深度时只能获取高度和宽度:
image_height, image_width = q_image.shape
继而才能用QImage的构造函数构造Qt中的QImage对象:
q_image = QImage(q_image.data, image_width, image_height,
image_width,