OpenCV 获取像素值的几个方法

获取单通道矩阵的值

  Mat tempMask(g_sourceImg.rows + 2, g_sourceImg.cols + 2, CV_8UC1,Scalar::all(1));
//    g_maskImage(g_sourceImg.rows + 2, g_sourceImg.cols + 2, CV_8UC1,Scalar::all(1));
    tempMask.copyTo(g_maskImage);
    for (int i = 0 ; i < g_maskImage.rows; i++) {
        for (int j = 0; j < g_maskImage.cols; j++) {
          cout << (int)((*(g_maskImage.data + g_maskImage.step[0] * i + g_maskImage.step[1] * j)) * 1) << endl;
        }
    }

对单通道矩阵的进行赋值

// 随机赋值
Mat dist(10, 10, CV_8UC1);
for (int i = 0; i<dist.rows; ++i)
{
for (int j = 0; j<dist.cols; ++j)
{
uchar& val = dist.at<uchar>(i, j);
val = (unsigned)theRNG() & 255;
}
}
//自定义赋值
   for (int i = 0; i < g_maskImg.rows; i++) {
                for (int j = 0; j < g_maskImg.cols; j++) {
                     uchar val = g_maskImg.at<uchar>(i,j);
                    int intVal = val;
                    if(intVal  > 1){
                        g_maskImg.at<uchar>(i,j) = (char)0;
                    } else {
                        g_maskImg.at<uchar>(i,j) = (char)255;
                    }
                }
            }

获取多通道矩阵的值

+ (Mat)translation1:(Mat)srcImg desImg:(Mat)desImg{
    int rows = srcImg.rows;
    int cols = srcImg.cols;
    for (int i = 0;  i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int x = j - 200;
            int y = i - 200;
            /**
             *  uchar * data01 = image.ptr<uchar>(0)[1];
                data01是指向image第一行第二个元素的指针   ,感觉和 at的使用是一样的 ?????后续
             比如Vec<uchar, 3>:
             其实这句就是定义一个uchar类型的数组,长度为3而已
             */
            if (x >=0 && y >=00 && x <cols && y < rows) {
                desImg.at<Vec3b>(i,j) = srcImg.ptr<Vec3b>(y)[x];
            }
        }
    }
    return desImg;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容