Ask Your Question
0

Create column pointers just like TheMatrix.ptr<int>(vi);

asked 2015-11-23 07:14:34 -0500

aaarthur gravatar image

(Please forgive me for my wording since i am still a newbie)

i know that something like TheMatrix.ptr<uchar>(vi)can create a pointer with the elements of one row. I wonder if there's a way to create pointers containing the elements of one column (so that i can quickly access the elements in column sequence)?

PS: i would prefer a fast way since i would like to do some practical applications. So i would not consider method like transpose and use the ptr.<>()...

Thanks in advance!

edit retag flag offensive close merge delete

Comments

using the TheMatrix.col(i) is returning a Mat (its header more exactly) to the column i. So using the ptr<> on this Mat, you have a pointer to each element. Does it help?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-25 09:36:54 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-11-25 08:33:20 -0500

kbarni gravatar image

updated 2015-11-25 09:19:03 -0500

In image processing generally you cannot directly access columns as continuous vectors. However calculating the transpose of an image should be really fast, and I recommend it if you need to access many elements.

Otherwise you have to check first if the image is continuous in the memory (TheMatrix.isContinuous()). Sometimes OpenCV doesn't keep all the image data in the same place.

Now, if the image is continuous, you can calculate directly the pixel position in the memory:

p=TheMatrix.ptr<uchar>();
for(y=0;y<TheMatrix.rows();y++)
     uchar a=*(p+y*TheMatrix.step[1]*y+col);  //get quickly the column number col

Otherwise you need to use the TheMatrix.at<uchar>(y,x) function to get an element.

Anyway, as a general rule I recommend to write your algorithm, and optimize it at the end, if it's really needed. Today's computers are quite fast for image processing. Sometimes parallelization (or using the GPU) gives you much better performance than a heavy optimization (e.g. in this case, you can transpose one image while processing another one).

edit flag offensive delete link more

Comments

Thanks so much for the answer! I would take some time to test them out. I am using Visual Studio for the coding in c++. May I ask what kind of process could be done in parallelization? Is it as long as those tasks don't rely on each other?

aaarthur gravatar imageaaarthur ( 2015-11-25 10:36:57 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-11-23 07:14:34 -0500

Seen: 357 times

Last updated: Nov 25 '15