j.doe
j.doe

Reputation: 327

Get a row from an image

I have an image and I want to get a first row (and then the second, and so on...)

I wrote this code, but it doesn't work as expected:

int main(int argc, char** argv) 
{
    Mat img = imread("a.jpg");
    Mat line, ROI;
    for (int i = 0; i<img.rows; i++)
    {
        for (int i = 0; i<img.cols; i++)
        {
            ROI = img.clone();
            //  ROI=img(cropRect);
            Mat line = ROI(Rect(0, i, ROI.cols, 1)).clone();
        }
    }
    imshow("line", line);
    int k = waitKey(0);
    return 0;
}

Upvotes: 2

Views: 9551

Answers (1)

Miki
Miki

Reputation: 41765

You can use row to create a matrix header for the specified matrix row. If you need a deep copy, you can then use clone.

Also, you need imshow and waitKey to be inside the loop, or you'll see only the last row.

#include <opencv2/opencv.hpp>
using namespace cv;

int main() 
{
    Mat img = imread("path_to_image");
    Mat line;
    for (int i = 0; i < img.rows; i++)
    {
        line = img.row(i);

        // Or, for a deep copy:
        //line = img.row(i).clone();

        imshow("line", line);
        waitKey(0);
    }
    return 0;
}

Upvotes: 4

Related Questions