Monday, 15 July 2013

c++ - manual grayscale in opencv is too slow -



c++ - manual grayscale in opencv is too slow -

note: have manually don't suggest me utilize library function cvtcolor().

i'm new opencv , trying grayscale color image formula

(r,g,b) = (r,g,b)/((r+g+b)/3)

here method(c++) converting grayscale:

mat dst = src.clone(); (int i= 0; i<src.rows; ++i) { (int j = 0 ; j < src.cols; ++j) { vec3b myvec = dst.at<vec3b>(i,j); uchar temp = (myvec[0]+myvec[1]+myvec[2])/3; vec3b newpoint(temp,temp,temp); dst.at<vec3b>(i,j) = newpoint ; } }

because want grayscale video utilize method grayscale each of frame. slow in comparing using cvtcolor(src,dst,cv_rgb2gray). (i waitkey(1) not problem of waitkey)

i have 2 questions

i wonder if there way manually grayscale image fast cvtcolor. if not guy know how optimize above code grayscale video appears smoother. the above 1 approach grayscale. (r,g,b) = (r,g,b)/((r+g+b)/3) can guy tell me other approaches grayscale color image?

any reply appreciated. in advance.

i can tell ::at function slow.

i utilize struct , pointer here rgb example:

#pragma pack(push, 2) struct rgb { //members in "bgr" order! uchar blue; uchar green; uchar red; };

and acces pixels of image this:

rgb& rgb = image.ptr<rgb>(y)[x]; //y = row, x = col

change pixel values (for rgb image) this:

image.ptr<rgb>(y)[x].value[0] = 142; image.ptr<rgb>(y)[x].value[1] = 255; image.ptr<rgb>(y)[x].value[2] = 90;

you can translate grayscale problem pretty easy. thing is, fast because scanline of cv::mat image not split in memory pixels of 1 scanline next each other in memory.

c++ opencv optimization grayscale

No comments:

Post a Comment