OpenCV:如何计算max(R,G,B) [英] OpenCV: How to calculate max(R,G,B)
本文介绍了OpenCV:如何计算max(R,G,B)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可悲的是,OpenCV文档仅为数学家或已经知道如何使用OpenCV的人编写。
Sadly the OpenCV documentation has been written for mathematicians only or for those who already know how to use OpenCV.
我想做一个简单的事情,比如获取每个像素的R,G,B的最高值,并将其写入新的灰度图像。我尝试了 merge()
, split()
和其他人但没有成功。
I want to do such a simple thing as getting the highest value of R,G,B for each pixel and write it to a new gray scale image. I tried merge()
, split()
and others but without success.
以下函数完全符合我的要求,但我想知道OpenCV是否可以做得更简单。
The following function does exactly what I want, but I wonder if OpenCV could do that simpler.
Mat CalcRGBmax(Mat i_RGB)
{
if (i_RGB.channels() != 3)
throw "24 bit color image expected.";
Mat i_Gray(i_RGB.rows, i_RGB.cols, CV_8UC1);
for (int Y=0; Y<i_RGB.rows; Y++)
{
BYTE* pu8_Src = i_RGB. ptr<BYTE>(Y);
BYTE* pu8_Dst = i_Gray.ptr<BYTE>(Y);
int P = 0;
for (int X=0; X<i_RGB.cols; X++)
{
BYTE B = pu8_Src[P++];
BYTE G = pu8_Src[P++];
BYTE R = pu8_Src[P++];
pu8_Dst[X] = max(R, max(G,B));
}
}
return i_Gray;
}
推荐答案
你的代码会快得多比这更好,但这是如何使用 split()和 max():
Your code will be much faster than this, but this is how to do it with split() and max():
Mat CalcRGBmax(Mat i_RGB)
{
std::vector<cv::Mat> planes(3);
cv::split(i_RGB, planes);
return cv::Mat(cv::max(planes[2], cv::max(planes[1], planes[0])));
}
这篇关于OpenCV:如何计算max(R,G,B)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文