0
votes
    //Open the image 
    Mat img_rgb = imread("sudoku2.png", CV_LOAD_IMAGE_GRAYSCALE); 
    if (img_rgb.empty()) 
    { 
            cout<<"Cannot open the image"<<endl; 
            return; 
    }
    Mat img_bw = img_rgb > 128;
    imwrite("image_bw.jpg", img_bw);

Now, I want to get all pixels of img_bw and save it into a matrix M (int[img_bw.rows][img_bw.cols]). How to do it in C++.

1

1 Answers

0
votes

What format ?

The raw byte data in cv::Mat is available from the .ptr() function, ie img_bw.ptr().

Opencv also has an xml and json read and write functions for matrices, just by using the << operator - see opencv tutorial on xml and yaml i/o

EDIT: In c++ you can access pixels with the .at operator.

Use img_data.at<uchar>(x,y) for an unsigned char (CV_8U) pixel and img_data.at<float>(x,y) for a CV_32F image.