opencv输出图片像素值

需求:在控制台输出灰度图像的像素值

代码:

#include <stdio.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include<stack>
using namespace std;
using namespace cv;
#include <fstream>

#include <sys/uio.h>



int main()
{
    Mat g_srcImage;
    
    g_srcImage = imread("/Users/tanchao/Documents/c++work/myopencv5/myopencv5/1.jpg",0);//第二个参数0表示灰度图读取
    imshow("test",g_srcImage);
    for(int i=0;i<g_srcImage.rows;i++)
    {
        for(int j=0;j<g_srcImage.cols;j++)
        {

            cout<<int(g_srcImage.at<uchar>(i,j))<<" ";

        }
        cout << "\n"<<endl;
    }
    waitKey(0);
    return 0;
    
}

需要注意的是:g_srcImage.at<uchar>(i,j)时要用int进行类型转换,否则会出错误。

猜你喜欢

转载自www.cnblogs.com/shixisheng/p/9226127.html