问题描述
我有一个彩色图像,表示为OpenCV Mat对象(C ++,图像类型CV_32FC3)。我有一个颜色校正矩阵,我想应用于RGB彩色图像的每个像素(或使用OpenCV约定的BGR,这里无关紧要)。色彩校正矩阵为3x3。
I have a color image represented as an OpenCV Mat object (C++, image type CV_32FC3). I have a color correction matrix that I want to apply to each pixel of the RGB color image (or BGR using OpenCV convention, doesn't matter here). The color correction matrix is 3x3.
我可以轻松迭代像素并创建一个代表RGB的矢量v(3x1),然后计算M * v,但这对我的实际来说太慢了时间视频应用程序。
I could easily iterate over the pixels and create a vector v (3x1) representing RGB, and then compute M*v, but this would be too slow for my real-time video application.
cv :: cvtColor函数很快,但似乎不允许自定义颜色转换。
The cv::cvtColor function is fast, but does not seem to allow for custom color transformations. http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor
类似于以下内容,但我使用的是OpenCV for C ++,而不是Python。
Similar to the following, but I am using OpenCV for C++, not Python.Apply transformation matrix to pixels in OpenCV image
推荐答案
基本上,链接的答案使用重塑
来转换你的 CV_32FC3
垫尺寸 mxn
到 CV_32F
垫尺寸(mn)x 3
。之后,矩阵的每一行都包含一个像素的颜色通道。然后你可以应用通常的矩阵乘法来获得一个新的垫子并且重塑
它可以回到具有三个通道的原始形状。
Basically the linked answer uses reshape
to convert your CV_32FC3
mat of size m x n
to a CV_32F
mat of size (mn) x 3
. After that, each row of the matrix contains exactly color channels of one pixel. You can then apply usual matrix multiplication to obtain a new mat and reshape
it back to the original shape with three channels.
注意:值得注意的是opencv的默认颜色空间是BGR,而不是RGB。
Note: It may be worth noticing that the default color space of opencv is BGR, not RGB.
这篇关于使用OpenCV 3.0将色彩矩阵应用于RGB图像的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!