Ask Your Question
0

How to sample image into 10*10 grid and select every center pixel and store in a vector for further processing?

asked 2018-01-16 11:43:04 -0600

arti gravatar image

updated 2018-01-16 11:47:23 -0600

berak gravatar image

I want to take center pixel of 10 10 grid for finding corresponding locations of pixels between two consecutive frames. I have written code but getting error. here is the snap of selecting center pixel of grid 1010:

int width = img.cols;
int height = img.rows;
int g_size = 10;
Point2f points;
for(int i =0; i< width/g_size-1;++i){
    for(int j =0; j< width/g_size-1;++j){
        points.x = i* g_size + g_size/2;
        points.y = j* g_size + g_size/2;
    }
    img_corners.push_back(points);
}
img_corners.push_back(points);
vector<uchar> features_found;
features_found.reserve(maxCorners);
vector<float> feature_errors;
feature_errors.reserve(maxCorners);
calcOpticalFlowPyrLK( img, prevImg, img_corners, prevImg_corners, 
features_found, feature_errors ,Size( win_size, win_size ), 3, 
cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.3 ), 0, k);

Can any one help please. Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-16 12:07:28 -0600

berak gravatar image

updated 2018-01-16 13:01:27 -0600

your grid calculation is broken, you only save the last horizontal points (only 10 points left in the array, not 100) rather try like this:

for(int i=0; i<10; ++i){
    for(int j=0; j<10; ++j){
        float y = float(i * (height/10) + 5);
        float x = float(j * (width/10) + 5);
        img_corners.push_back(Point2f(x,y));
    }
}

then, a grid like that is usually a bad seed for optical flow, rather use goodfeaturestotrack, or ORB/SIFT for this task, not a grid (which will have points "in nowwhere" far too often !)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-16 11:43:04 -0600

Seen: 683 times

Last updated: Jan 16 '18