IplImage和Mat访问图像中每个像素

opencv1.0,IplImage类型存储图像数据,3通道

void print_img(IplImage* img)
{
    int x,y;
    FILE *file;
    file = fopen("1.txt", "w");
    uchar* data = (uchar *)img->imageData;
    int step = img->widthStep / sizeof(uchar);
    int channels = img->nChannels;
    for (int i = 0; i<img->height; i++)
        for (int j = 0; j<img->width; j++)
        {
            fprintf(file, "%d %d %d ", data[i*step + j*channels + 0], data[i*step + j*channels + 1], data[i*step + j*channels + 2]);
        }
    fclose(file);
}

opencv2.4.9,Mat类型存储图像数据,3通道

void print_img(Mat img)
{
    int x,y;
    FILE *file;
    file = fopen("1.txt", "w");
    for (int x = 0; x<img.rows; x++)
    {
        for (int y = 0; y<img.cols; y++)
        {
            fprintf(file, "%d %d %d ", img.at<Vec3b>(x, y)[0], img.at<Vec3b>(x, y)[1], img.at<Vec3b>(x, y)[2]);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/sjtuxx_lee/article/details/77075043