OpenCV for Android: Convert Camera preview from YUV to RGB with Imgproc.cvtColor -
i runtime error if try convert camera preview yuv byte array rgb(a) byte array imgproc.cvtcolor( myuv_mat, mrgba_mat, imgproc.color_yuv420sp2rgba, 4 ) in onpreviewframe(byte[] data, camera camera):
preview.java:
mcamera.setpreviewcallback(new previewcallback() { public void onpreviewframe(byte[] data, camera camera) { // pass yuv data draw-on-top companion system.arraycopy(data, 0, mdrawontop.myuvdata, 0, data.length); mdrawontop.invalidate(); } }); drawontop.java:
public class drawontop extends view { bitmap mbitmap; mat myuv_mat; protected void ondraw(canvas canvas) { if (mbitmap != null) { canvaswidth = canvas.getwidth(); canvasheight = canvas.getheight(); int newimagewidth = 640; int newimageheight = 480; marginwidth = (canvaswidth - newimagewidth)/2; if( myuv_mat != null ) myuv_mat.release(); //myuv_mat = new mat( newimagewidth, newimageheight, cvtype.cv_8uc1 ); myuv_mat = new mat( newimagewidth, newimageheight, cvtype.cv_8uc4 ); myuv_mat.put( 0, 0, myuvdata ); //mat mrgba_mat = new mat(); mat mrgba_mat = new mat(newimagewidth,newimageheight,cvtype.cv_8uc4); //mat mrgba_mat = myuv_mat; //imgproc.cvtcolor( myuv_mat, mrgba_mat, imgproc.color_yuv2rgba_nv21, 4 ); //imgproc.cvtcolor( myuv_mat, mrgba_mat, imgproc.color_yuv420sp2rgb, 4 ); imgproc.cvtcolor( myuv_mat, mrgba_mat, imgproc.color_yuv420sp2rgba, 4 ); // draw bitmap new: bitmap mbitmap = bitmap.createbitmap( newimagewidth, newimageheight, bitmap.config.argb_8888 ); utils.mattobitmap( mrgba_mat, mbitmap ); mrgba_mat.release(); } } the conversion myuv_mat.put( 0, 0, myuvdata ) runs correctly. attempts convert myuv_mat mrgb_mat using imgproc.cvtcolor lead runtime errors ("source not found." eclipse).
what correct imgproc.cvtcolor command goal?
(i don't want use java yuv2rgb(a) decode method because it's slow image processing. want use opencv imgproc.cvtcolor method because can native calls)
maybe imgproc library isn't included in project, other opencv libraries are? line crashes first line use method imgproc, explain why earlier parts of code run correctly.
your code looks fine, except can use no-argument constructor mrgba_mat (since opencv4android functions, including cvtcolor, can infer required size of destination matrix), , you're allocating lot of wasted space myuv_mat. don't need full 4 channels if allocate yuv matrices 50% more space rgb counterparts:
myuv_mat = new mat( newimageheight + newimageheight / 2, newimagewidth, cvtype.cv_8uc1 );
Comments
Post a Comment