OpenCV 訪問Mat 像素
場景
在進行煙霧檢測的過程中,需要訪問Mat指定區域的像素值,然後判斷是否符合煙霧的像素特征(當然,在網上查詢到的煙霧像素特征,實際上,並沒有正確識別出煙霧),可通過如下的方式進行操作
int similarDegree = 0;
int channel = srcMat.channels();
for (int i=info.smokeLeftTopPos.y; i<maxHeight; i++)
{
for (int j=info.smokeLeftTopPos.x; j<maxWidth; j++)
{
int r = srcMat.at<uchar>(i, j*channel);
int g = srcMat.at<uchar>(i, j*channel+1);
int b = srcMat.at<uchar>(i, j*channel+2);
if ((r>=200) && (g>=200) && (b<=180))
{
similarDegree++;
}
}
}
當然在實際的應用過程中,像素的值很可能不是無符號字符型,還有可能是浮點型,短整型等,可以使用如下的方式進行判斷:
int type = 0;
switch(srcMat.type())
{
case CV_8UC3:
type = 0;
break;
case CV_8SC3:
type = 1;
break;
case CV_16UC3:
type = 2;
break;
case CV_16SC3:
type = 3;
break;
case CV_32SC3:
type = 4;
break;
case CV_32FC3:
type = 5;
break;
case CV_64FC3:
type = 6;
break;
}
參考
http://www.cnblogs.com/wangguchangqing/p/4016179.html
OpenCV 訪問Mat 像素