2

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;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
j.doe
  • 327
  • 7
  • 22

1 Answers1

4

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;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
  • Thank you Can I ask queastion What is the difference deep copy and I try seeing second line but it doesn't work – j.doe Mar 09 '16 at 23:27
  • with _deep_ copy (i.e. `clone()`) you're are effectively copying the data inside the matrix. Regarding this example, if you modify `line` after deep copy, `img` is not altered. If you don't use `clone`, any modification to `line` will be reflected in `img`. Try adding `line.setTo(Scalar(0,0,0));` before `imshow("line", line);`, and adding `imshow("original", img); waitKey();` at the end. With deep copy `img` is not changed, otherwise it becomes black. – Miki Mar 09 '16 at 23:31
  • what doesn't work? (there was a small typo, corrected) – Miki Mar 09 '16 at 23:34
  • Its Ok! :) Thank you so much But I cant see second line yet I try this :img.row[i][1]; – j.doe Mar 09 '16 at 23:41
  • second line (i.e. with index 1) is `img.row(1)`. `img.row[i][1]` is very wrong. What do you want to do exactly? – Miki Mar 09 '16 at 23:44
  • I work first line and when it finished I go second line Now I can get a first line but I can't get other lines Imean I want to get lines respectively – j.doe Mar 09 '16 at 23:47
  • Probably I didn't understand... however... after `line = img.row(i);` you can do all processing you want... the next iteration of the for loop you'll be on second line... – Miki Mar 09 '16 at 23:51
  • Im so sorry I have understood it I write incorrectly thank you so much again – j.doe Mar 09 '16 at 23:54
  • Hi @Miki Now I have a row which first row and it has black and white points How can I derivative of row's according to black points – j.doe Mar 11 '16 at 17:50
  • I don't understand. However, this is another question. So please write another question and try to explain properly what you have and what you want to achieve. – Miki Mar 11 '16 at 17:51