Navdeep
Navdeep

Reputation: 863

calculating number of bytes of each row in an image

How can we calculate the size of each row in bytes of a 3 channel image. I know it is no. of columns * element size. But I have seen in some books and on internet that sometimes the rows are padded with extra pixels for efficiency. So in those cases the above formula won't work because the number of bytes in this case will increase. Am I right ?? Please explain. Also I was able to find an attribute "step" to find the size of first row in bytes .

I used it as img.step[0] // It Gave no. of bytes in first row

But when I tried to find the size of second row using img.step[1], it gave some garbage value. Please explain what is happening.

Upvotes: 2

Views: 2700

Answers (2)

bkausbk
bkausbk

Reputation: 2790

What you are looking for is the field IplImage.widthStep

Size of aligned image row in bytes

Edit: You said you are using OpenCv 2. Im assuming you are using cv::Mat.

In this case cv::Mat.step should give you the width of the row.

a distance between successive rows in bytes; includes the gap if any

However I would suggest to use method cv::Mat::ptr(row) to accces each row. This will return an uchar* to raw row data.

Upvotes: 3

Mike
Mike

Reputation: 7203

Usually a step is applied to all rows of an image. The size of all rows will be the same as the size of the first row.

Upvotes: 1

Related Questions