OpenCV learning notes (7) -- OpenCV for Android real-time image processing

Source: Internet
Author: User
Tags scalar

OpenCV learning notes (7) -- OpenCV for Android real-time image processing

In the previous article, we have enabled the camera and acquired real-time image information. Then we can try to process the acquired image information and display it in real time, here we need to complete several processes:

Grayed out, Canny edge detection, Hist histogram calculation, Sobel edge detection, SEPIA (color tone conversion), ZOOM magnifier, pixw.e Pixel

 

1. Modify the layout interface:

Because we need to switch between different image processing modes, we need to place a button on the interface. We can place multiple buttons, each of which corresponds to a processing mode, however, here we can also place only one button. Each time we click the button, the switch will be performed once. The switch mode will be cyclic:

Activity_main.xml file:

 

<Framelayout android: layout_height = "match_parent" android: layout_width = "match_parent" xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: opencv = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools">
         
  
   
  
 </Framelayout>
View the preview image:

 

2. Get the button component and listen to the button and click:

1. Declare a Button object to bind the preceding Button component and a status flag to store the current status:

 

// Button component private Button mButton; // The current processing status is private static int Cur_State = 0;

 

 

2. Bind the button and click listener in OnCreate:

 

MButton = (Button) findViewById (R. id. deal_btn); mButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (Cur_State <8) {// Switch Status Cur_State ++ ;} else {// restore the initial state Cur_State = 0 ;}}});
Here, the state flag Cur_State corresponds to each type of image processing, and 0 corresponds to the default state, that is, the display source image, which corresponds to 1-7 respectively: grayed out, Canny edge detection, Hist histogram calculation, Sobel edge detection, SEPIA (color tone conversion), ZOOM magnifier, pixw.e Pixel

3. image information acquisition, storage, processing, and display:

 

1. In OpenCV, the Mat type is generally used to store matrix information such as images. Therefore, we can declare a Mat object as a cache object for real-time frame images:

 

// Cache the data input by the camera per frame private Mat mRgba;

 

 

2. Object Instantiation and basic attribute settings, including length, width, and image type flag:
public void onCameraViewStarted(int width, int height) {// TODO Auto-generated method stubmRgba = new Mat(height, width, CvType.CV_8UC4);}

 

3. assign values to objects. Here, only the source image and the gray image are processed, and other operations will be added later:

 

/*** Image processing is written here */@ Overridepublic Mat onCameraFrame (CvCameraViewFrame inputFrame) {switch (Cur_State) {case 1: // The Imgproc for gray processing. cvtColor (inputFrame. gray (), mRgba, Imgproc. COLOR_GRAY2RGBA, 4); break; default: // display the original image mRgba = inputFrame. rgba (); break;} // return the processed result data return mRgba ;}

 


4. If you use an object to store image data, the data will be stored in the memory. Therefore, you need to release the data at the end of the process. Otherwise, the data may crash:

 

        @Overridepublic void onCameraViewStopped() {// TODO Auto-generated method stubmRgba.release();}

5. view the running results:

 

Normal Mode:


Gray map:

 

4. Other processing and results:

In the above example, we have completed the gray processing of the preview image, so we will add other processing to the Code to view the effect. Because in 2. some methods used in Version x have changed, for example, org. opencv. core. methods line and rectangle in the Core class are invalid. You can use org. opencv. imgproc. line and rectangle in Imgproc are replaced:

1. MainActivity. java source code:

 

Package com. linsh. opencv_test; import java. util. arrays; import org. opencv. android. baseLoaderCallback; import org. opencv. android. cameraBridgeViewBase; import org. opencv. android. openCVLoader; import org. opencv. android. cameraBridgeViewBase. cvCameraViewFrame; import org. opencv. android. cameraBridgeViewBase. cvCameraViewListener2; import org. opencv. android. loaderCallbackInterface; import org. opencv. core. core; impo Rt org. opencv. core. cvType; import org. opencv. core. mat; import org. opencv. core. matOfFloat; import org. opencv. core. matOfInt; import org. opencv. core. point; import org. opencv. core. scalar; import org. opencv. core. size; import org. opencv. imgproc. imgproc; import android. r. string; import android. app. activity; import android. OS. bundle; import android. util. log; import android. widget. button; import android. view. view; impor T android. view. view. onClickListener; public class MainActivity extends Activity implements CvCameraViewListener2 {private String TAG = "OpenCV_Test"; // The camera interface of OpenCV private CameraBridgeViewBase mCVCamera; // cache the data input by the camera per frame. private Mat mRgba and mTmp; // Button component private Button mButton; // The current processing status is private static int Cur_State = 0; private Size mSize0; private Mat mIntermediateMat; private MatOfInt mChannels []; private MatOfI Nt mHistSize; private int mHistSizeNum = 25; private Mat mMat0; private float [] mBuff; private MatOfFloat mRanges; private Point mP1; private Point mP2; private Scalar mColorsRGB []; private Scalar mColorsHue []; private Scalar mWhilte; private Mat mSepiaKernel;/*** use OpenCV to manage Android services and asynchronously initialize OpenCV */BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback (this) {@ Overridepublic void onManagerCon Nected (int status) {switch (status) {case LoaderCallbackInterface. SUCCESS: Log. I (TAG, "OpenCV loaded successfully"); mCVCamera. enableView (); break; default: break ;}};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mCVCamera = (CameraBridgeViewBase) findViewById (R. id. camera_view); mCVCamera. setCvCameraViewListener (this); MButton = (Button) findViewById (R. id. deal_btn); mButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (Cur_State <8) {// Switch Status Cur_State ++ ;} else {// restore initial state Cur_State = 0 ;}}) ;}@ Overridepublic void onResume () {super. onResume (); if (! OpenCVLoader. initDebug () {Log. d (TAG, "OpenCV library not found! ");} Else {Log. d (TAG," OpenCV library found inside package. Using it! "); MLoaderCallback. onManagerConnected (LoaderCallbackInterface. SUCCESS) ;};@ Overridepublic void onDestroy () {if (mCVCamera! = Null) {mCVCamera. disableView () ;}}; @ Overridepublic void onCameraViewStarted (int width, int height) {// TODO Auto-generated method stubmRgba = new Mat (height, width, CvType. CV_8UC4); mTmp = new Mat (height, width, CvType. CV_8UC4); mIntermediateMat = new Mat (); mSize0 = new Size (); mChannels = new MatOfInt [] {new MatOfInt (0), new MatOfInt (1 ), new MatOfInt (2)}; mBuff = new float [mHistSizeNum]; mHistSize = new MatOfInt (mHistSizeNum); mRanges = new MatOfFloat (0f, 256f); mMat0 = new Mat (); mColorsRGB = new Scalar [] {new Scalar (200, 0, 0,255), new Scalar (0,200, 0,255), new Scalar (0, 0,200,255 )}; mColorsHue = new Scalar [] {new Scalar (255, 0, 0,255), new Scalar (255, 60, 0,255), new Scalar (255,120, 0,255), new Scalar (255,180, 0,255), new Scalar (255,240, 0,255), new Scalar (215,213, 0,255), new Scalar (150,255, 0,255), new Scalar (85,255, 0,255), new Scalar (20,255, 0,255), new Scalar (0,255, 30,255), new Scalar (0,255, 85,255), new Scalar (0,255,150,255), new Scalar (0,255,215,255), new Scalar (0,234,255,255 ), new Scalar (0,170,255,255), new Scalar (0,120,255,255), new Scalar (0, 60,255,255), new Scalar (0, 0,255,255), new Scalar (64, 0,255,255 ), new Scalar (120, 0,255,255), new Scalar (180, 0,255,255), new Scalar (255, 0,255,255), new Scalar (255, 0,215,255), new Scalar (255, 0, 85,255), new Scalar (255, 0, 0,255)}; mWhilte = Scalar. all (255); mP1 = new Point (); mP2 = new Point (); // Fill sepia kernel mSepiaKernel = new Mat (4, 4, CvType. CV_32F); mSepiaKernel. put (0, 0,/* R */0.189f, 0.769f, 0.393f, 0f); mSepiaKernel. put (1, 0,/* G */0.168f, 0.686f, 0.349f, 0f); mSepiaKernel. put (2, 0,/* B */0.20.f, 0.534f, 0.272f, 0f); mSepiaKernel. put (3, 0,/* A */0.000f, 0.000f, 0.000f, 1f);} @ Overridepublic void onCameraViewStopped () {// TODO Auto-generated method stubmRgba. release (); mTmp. release ();}/*** image processing are all written here */@ Overridepublic Mat onCameraFrame (CvCameraViewFrame inputFrame) {mRgba = inputFrame. rgba (); Size sizeRgba = mRgba. size (); int rows = (int) sizeRgba. height; int cols = (int) sizeRgba. width; Mat rgbaInnerWindow; int left = cols/8; int top = rows/8; int width = cols * 3/4; int height = rows * 3/4; switch (Cur_State) {case 1: // grey processing Imgproc. cvtColor (inputFrame. gray (), mRgba, Imgproc. COLOR_GRAY2RGBA, 4); break; case 2: // mRgba = inputFrame. rgba (); Imgproc. canny (inputFrame. gray (), mTmp, 80,100); Imgproc. cvtColor (mTmp, mRgba, Imgproc. COLOR_GRAY2RGBA, 4); break; case 3: // Hist histogram calculation Mat hist = new Mat (); int thikness = (int) (sizeRgba. width/(mHistSizeNum + 10)/5); if (thikness> 5) thikness = 5; int offset = (int) (sizeRgba. width-(5 * mHistSizeNum + 4*10) * thikness)/2); // RGB for (int c = 0; c <3; c ++) {Imgproc. calcHist (Arrays. asList (mRgba), mChannels [c], mMat0, hist, mHistSize, mRanges); Core. normalize (hist, hist, sizeRgba. height/2, 0, Core. NORM_INF); hist. get (0, 0, mBuff); for (int h = 0; h
 
  

 

 

2 .:

Well-known Edge Detection:

Hist histogram calculation:

Sobel Edge Detection:

SEPIA ):

ZOOM magnifiers:

Pixstme pixel:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.