天天看点

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;

    case CV_16UC3:

      type = 2;

    case CV_16SC3:

      type = 3;

    case CV_32SC3:

      type = 4;

    case CV_32FC3:

      type = 5;

    case CV_64FC3:

      type = 6;

参考

http://www.cnblogs.com/wangguchangqing/p/4016179.html

    本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1946642,如需转载请自行联系原作者

继续阅读