OpenCV-Mat方式获取图片的像素(二)

来源:互联网 发布:百分百圣洁知乎 编辑:程序博客网 时间:2024/05/29 03:24

注意step是一个数组,随着维度会进行扩展,定义了矩阵的布局。

#include <iostream>#include <opencv2/opencv.hpp>#include <opencv2/highgui.hpp>using namespace std;using namespace cv;int main(int argc, const char * argv[]) {    // insert code here...    const char filename[] = "/Users/linwang/Downloads/Lena.jpg";    Mat Img = imread(filename);        cout<<"三维图像的维度:"<<Img.dims<<endl;    cout<<"三维图像的通道数:"<<Img.channels()<<endl;        cout<<"三维图像的宽:"<<Img.rows<<endl;    cout<<"三维图像的高:"<<Img.cols<<endl;        cout<<"数据类型的大小 "<<Img.elemSize1()<<endl;    cout<<"存放一个列元素的大小:"<<Img.elemSize()<<endl;        cout<<" Img.step[0] = "<<Img.step[0]<<endl;    cout<<" Img.step[1] = "<<Img.step[1]<<endl;        Vec3i color;    for (int row = 2;row<20;row++)    {        for(int col = 20;col<40;col++)        {            color[0] = int(*(Img.data + Img.step[0] * row + Img.step[1]* col));            color[1] = int(*(Img.data + Img.step[0] * row + Img.step[1]* col + Img.elemSize1()*1));            color[2] = int(*(Img.data + Img.step[0] * row + Img.step[1]* col + Img.elemSize1()*2));            cout<<color[0]<<"->"<<color[1]<<"->"<<color[2]<<endl;            color[0] = 255;            color[1] = 0;            color[2] = 0;            *(Img.data + Img.step[0] * row + Img.step[1]* col) = color[0];            *(Img.data + Img.step[0] * row + Img.step[1]* col + Img.elemSize1()*1) = color[1];            *(Img.data + Img.step[0] * row + Img.step[1]* col + Img.elemSize1()*2) = color[2];        }    }    imshow("test", Img);    cvWaitKey(0);    return 1;}




原创粉丝点击