2

Is any way to convert Mat image to vector?

This doesnt work:

Mat input = imread("image.png");
vector<Point> points;

points = input;
  • It is not surprising that there's no such conversion in OpenCV, since representing an image as a vector is extremely inefficient. But if you need this anyway, what's stopping you from writing the 2-4 lines of code to perform this conversion? – Ophir Gvirtzer Aug 11 '15 at 14:17
  • An image has points and every point holds data. An 8-bit black and white image has WxH points and each point has a UCHAR value. Study [this](http://stackoverflow.com/questions/22115302/opencv-documentation-says-that-uchar-is-unsigned-integer-datatype-how). – LovaBill Aug 11 '15 at 15:45

2 Answers2

2

You could easily do it manually, creating points directly using the row/col indexing of the mat:

//Create a blank mat of size 3x3 and an empty point vector
cv::Mat img = cv::Mat::zeros(cv::Size(3, 3), CV_8UC3);
std::vector<cv::Point> points;

//Loop over each pixel and create a point
for (int x = 0; x < img.cols; x++)
    for (int y = 0; y < img.rows; y++)
        points.push_back(cv::Point(x, y));

//Print out results
for (cv::Point p : points)
    std::cout << p << "\n";

enter image description here

But I can't see why you'd need to do this, you already know the points in the matrix as you have access to its size.

user3510227
  • 576
  • 2
  • 6
  • I thought Point has member "value". –  Aug 11 '15 at 14:40
  • Do you want to access the colour channels of the pixel? – user3510227 Aug 11 '15 at 14:41
  • I have binary image and want to measure Hausdorff distance to compare two images and need black pixels in vector. I does it like this: extractChannel(input,input,0); findNonZero(input,points); But i dont know why white pixels have non zero value not black. –  Aug 11 '15 at 14:49
  • There are (generally) 8 bits per channel which give a range from 0 to 255. A value of 0 indicates a black pixel and a value of 255 indicates a white pixel. If you wanted to find black pixels, you could perform a bitwise not to flip black to white and then perform findNonZero() to find the points. Edit - For a Binary image you'll only have two possible values per pixel 0 - 1 (instead of 0 - 255) but the idea is the same. – user3510227 Aug 11 '15 at 14:57
  • How to get value of for example row:2 col:3 pixel? –  Aug 11 '15 at 15:34
  • 1
    You'll already have a binary image if there are only two possible pixel values. OpenCV must use an 8-bit channel for binary images which is why you get 0 (00000000) and 255 (11111111) as your two possible values. A 1-bit channel which would give you 0 and 1, it won't make a difference though, you still only have black and white pixels. If you still have trouble make a new question and I'll write up an example when I can. Edit - To get channel values use img.at(point) – user3510227 Aug 11 '15 at 15:47
0

Please explain how do you wish to convert the image to points. What would each point in your vector hold?

You can populate your vector by iterating through the matrix.

You can assign a 3x1,1x3 or a 1x2, 2x1 Mat array to a Point2f, Point3f structure by constructors.

This thread may be helpful, Conversion of 3x1 or 1x3 cv::Mat to cv::Point3d?

Community
  • 1
  • 1
rs_
  • 433
  • 4
  • 12
  • 1
    Point has two elements x, y or three elements x, y, z in case of Point3i, Point3f. If you need to represent color values of a pixel, you may use Scalar instead. – rs_ Aug 11 '15 at 14:55