以下是您访问每个频道的方式:
blue = frame.at<cv::Vec3b>(i,j)[0];
green = frame.at<cv::Vec3b>(i,j)[1];
red = frame.at<cv::Vec3b>(i,j)[2];
上面的代码假设您有一个 3 通道图像,其中每个值都是一个8 位无符号字符 ( CV_8UC3
)。这种类型用于许多常见的图像格式。但是,如果您有不同类型的 3 通道图像,请执行以下操作:
- 如果图像类型是 3 通道浮点 (
CV_32FC3
),则替换cv::Vec3b
为cv::Vec3f
- 如果图像类型是 3 通道双精度 (
CV_64FC3
),则替换cv::Vec3b
为cv::Vec3d
- 如果图像类型是 3-channel int (
CV_32SC3
),则替换cv::Vec3b
为cv::Vec3i
- 如果图像类型是 3-channel short int (
CV_16SC3
) 或 16-bit uchar ( CV_16UC3
),则替换cv::Vec3b
为cv::Vec3s
不确定您使用的是什么图像格式?尝试调用getImgType(frame)
(参见下面的代码)。
string getImgType(cv::Mat frame)
{
int imgTypeInt = frame.type();
int numImgTypes = 28; // 7 base types, with 4 channel options each (C1, ..., C4)
int enum_ints[] = {CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4, CV_8SC1, CV_8SC2, CV_8SC3, CV_8SC4, CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4, CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4, CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4, CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4, CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4};
string enum_strings[] = {"CV_8U", "CV_8UC1", "CV_8UC2", "CV_8UC3", "CV_8UC4", "CV_8SC1", "CV_8SC2", "CV_8SC3", "CV_8SC4", "CV_16UC1", "CV_16UC2", "CV_16UC3", "CV_16UC4", "CV_16SC1", "CV_16SC2", "CV_16SC3", "CV_16SC4", "CV_32SC1", "CV_32SC2", "CV_32SC3", "CV_32SC4", "CV_32FC1", "CV_32FC2", "CV_32FC3", "CV_32FC4", "CV_64FC1", "CV_64FC2", "CV_64FC3", "CV_64FC4"};
for(int i=0; i<numImgTypes; i++)
{
if(imgTypeInt == enum_ints[i]) return enum_strings[i];
}
return "unknown image type";
}