java - Convert RGB to Gray with my own formula in OpenCV -


i'd convert rgb image gray function :

imgproc.cvtcolor(mrgba, mgrey, imgproc.color_rgba2gray); 

but opencv uses specific formula (0.299*r + 0.587*g + 0.114*b think ?), , i'd use min(r,g,b) instead.

is there performance-efficient way ? (this opencv android, java)

edit :

here profiling, should have done earlier :

base application capturing camera runs @ 12 fps

  • with opencv's cvtcolor() : 12 fps (speed difference insignificant)
  • with mark miller's answer : 1.5 fps
  • with mark miller's answer , miki's optimization (get data pointer once) : 7 fps

that may incredible little function costs half fps. device can yet run complicated stuff orb @ decent rates (3-4 fps), , o(n) c++ routines without fps difference. miki's "trick" makes function 10x faster.

there no pre-built way give opencv arbitrary function conversion grayscale. remaining option using rgb channels themselves.

for (int x = 0; x < mrgba.cols(); x++) {     (int y = 0; y < mrgba.rows(); y++)     {         double[] rgb = mrgba.get(x, y);         mgray.put(x, y, math.min(rgb[0], math.min(rgb[1], rgb[2])));     } } 

as have mentioned, may slow, large images , branching comes if-statements. however, there 3 rules of optimization: (1) don't it, (2) don't yet, , (3) profile first. put, try easy-to-code version first, once know fact section slow section [i.e, running , timing it], change code more efficient.


Comments