Android image processing software

Source: Internet
Author: User

In the machine Vision Laboratory for 1.5 of the time, but because of their own "wayward." All the time to learn the content is to arrange their own, I still insist that there is no best and simplest technology, only their favorite technology. But still feel ashamed to say, often hear seniors talk about image processing algorithms, but until this software was born before the knowledge of machine vision can be said all do not know. His own research is mainly about Android, from the upper to the lower levels are involved. Have always wanted to put their own director and the laboratory theme, so that you can communicate with the laboratory cattle, but also brush the existence of the feeling ~ from this to my brother to ask one or two, learned a bit of image processing technology, did an Android platform image processing tools, Enables users to quickly preview the results of the image under different algorithms in real time. Also because I learned the algorithm is too little, in the future after learning more will be slowly added to the software, the software function to grow up.

Let's start by talking about the features that the software can achieve so far:
* Turn on the phone camera preview image
* Convert the captured image to a grayscale image preview
* Preview the grayscale image after Sobel conversion
* Binary image after Sobel
* The segmentation threshold can be dynamically adjusted at any time during the process of binary value
* The image can be locked by the photo button, and the edge is extracted at once

The software is as follows:

The first is to open the main interface that the software sees:

Then click on the menu to switch to the grayscale image:

Then switch to the Sobel transform.

Two-value segmentation with a threshold value of 50

Finally, edge extraction is performed:

Which involves the algorithm has four, the specific algorithm implementation can be found in the software source code, click to download the source code

The entire software has 3 classes, there are three different functional parts, of which Mainactivity.java is the main class, following a detailed description of the entire software architecture according to three different functional components:

1. Main interface

Mainactivity.java:

The first is the interface layout, mainly a imageview used to display the entire preview box, plus a photo button and a menu button, click the menu button pop-up menu. Finally, if the current request is a binary algorithm, a sliding progress bar and a text input box are displayed to dynamically adjust the threshold. The code is as follows:

<relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:id  = "@+id/root"  android:layout_width  = "match_parent"  android:layout_height  = "match_parent"  tools:context  =". Mainactivity ";     <imageview  android:id
      = "@+id/preview"  android:layout_width  =< Span class= "Hljs-value" > "match_parent"  android:layout_height  = "match_parent" />     <button  android:id  = "@+id/takepic_bt"  android:layout_width  = "60DP"  android:layout_height  = "60DP"  android:layout_alignparentright  = "true"  android:layout_centervertical  =" true " android:layout_marginright  =         "15DP"  android:background  = "@drawable/shutter"  Span class= "Hljs-attribute" >android:onclick  = "onclick_takepic" />     <listview  android:id  = "@+id/list_view"  android:layout_width  = "150DP"  android:layout_height  = "match_parent"  android:layout_margintop  = "20DP"  android:divider  = " #ff555555 " android:dividerheight  =" 1.3DP "
       android:listselector  = "@android: Color/holo_ Blue_bright " android:visibility  =" Gone " ;     </ListView>    <Button        Android:id="@+id/takepic_bt"        Android:layout_width="40DP"        Android:layout_height="40DP"        Android:layout_alignparentbottom="true"        Android:layout_alignparentleft="true"        Android:layout_marginbottom="15DP"        Android:layout_marginleft="5DP"        Android:background="@drawable/menu"        Android:onclick="Onclick_menu"/>    <EditText        Android:id="@+id/input_threshold"        Android:layout_width="Wrap_content"        Android:layout_height="Wrap_content"        Android:layout_alignparentbottom="true"        Android:layout_alignparentright="true"        Android:layout_margin="20DP"        Android:inputtype="Numberdecimal"        Android:text=" the"        Android:textcolor="@android: Color/holo_blue_bright"        android:visibility="Gone"/>    <SeekBar        Android:id="@+id/seekbar"        Android:layout_width="Match_parent"        Android:layout_height="Wrap_content"        Android:layout_alignparentbottom="true"        Android:layout_marginbottom="20DP"        Android:layout_marginleft="50DP"        Android:layout_toleftof="@id/input_threshold"        Android:max="255"        android:progress=" the"        android:visibility="Gone"/>    <view  android:id  =         "@+id/topline"  android:layout_width  = "match_parent"  android:layout_height  = "2DP"  android:layout_marginleft  =" 100DP " android:layout_marginright  =" 100DP " android:layout_margintop  =" 50DP " android:background  =" #444 "/>     <view  android:layout_ Width  = "2DP"  android:layout_height  =< Span class= "Hljs-value" > "180DP"  android:layout_alignend  =" @id/topline " android:layout_below  =" @id/topline " android:background  =  "#444" />     <Viewandroid:layout_width="2DP"android:layout_height="180DP"  Android:layout_alignstart="@id/topline"android:layout_below="@id/topline"  Android:background="#444" />                                        </relativelayout>

Next, add a listener to the menu and photo button events as follows:

public   void  onclick_menu  (view view) {if  (isme            nuvisible) {listview.startanimation (menuinvisible);            Listview.setvisibility (View.gone);        ismenuvisible = false ;            } else  {listview.startanimation (menuvisible);            Listview.setvisibility (view.visible);        ismenuvisible = true ; }} public  void                  Onclick_takepic  (view view) throws  ioexception {getscreen.takepic (); //camera photo }  

Where the menu is displayed I added a transparency animation to make the software interface softer. Transparency tween animations are simple, add two animation resources to the res/anim/directory to display and disappear, and then click to play the animation in the event.
Then I write a click event on the pop-up menu, where I use the ListView menu to set the callback method using the Listview.setonitemclicklistener () method. The code is as follows:

Listview.setonitemclicklistener (NewOnitemclicklistener () {@Override             Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {if(Position = =3)//Grayscale chart Opens the progress bar{seekbar.setvisibility (view.visible);                Inputthreshold.setvisibility (view.visible); }Else{seekbar.setvisibility (View.gone);                Inputthreshold.setvisibility (View.gone); } whichtodisplay = position;//Switching algorithm}        });

The main functions here are two: 1, open and close the threshold progress bar, 2, the algorithm of switching user clicks. Of course, the progress bar will also have a matching listener, where you need to listen to Seekbar and edittext two control, need to keep the synchronization of the two, the content is relatively simple and understandable, as follows:

//Handling SeekbarSeekbar.setonseekbarchangelistener (NewOnseekbarchangelistener () {@Override             Public void Onstoptrackingtouch(SeekBar arg0) {thresholdvalue = arg0.getprogress () >255?255: arg0. getprogress (); }@Override             Public void Onstarttrackingtouch(SeekBar arg0) {            }@Override             Public void onprogresschanged(SeekBar arg0,intArg1,BooleanARG2) {Inputthreshold.settext (arg0.getprogress () +""); }        });//Handling EditTextInputthreshold.setoneditoractionlistener (NewOneditoractionlistener () {@Override             Public Boolean oneditoraction(TextView V,intActionId, KeyEvent event) {seekbar.setprogress (Integer.parseint (Inputthresho Ld.gettext (). toString ());return false; }        });

Well, here basically the main interface is taken into account, the following is the image of the acquisition part.

2. Get camera image and show preview

Getscreen.java:
This section mainly applies to the use of Android cameras and preview features.
1. Use of the camera:
According to my personal use of the camera, can be summarized as a 6 steps:
① Open Camera--camera.open ();
② Setting Camera Parameters--camera.getparameters ();
③ Set Picture preview container--camera.setpreviewtexture ();
④ start previewing--camera.startpreview ();
⑤ Set Preview Callback Interface--camera.setpreviewcallback ();
⑥ Close the camera--camera.close ();
The whole process is simple, and the official documentation highlights a few error-prone areas: one is to note that the sequence of 3 and 42 steps cannot be reversed, and that the camera must be turned off, or else the camera will not be started by other programs. After my test, if you do not close the camera, the exit will also throw an exception, so need to pay more attention!

Another thing to note is that the callback incoming Setpreviewcallback () method image is the yuv420p format and needs to be converted to RGB format for easy processing. Here to use a DECODEYUV420SP algorithm, detailed content see source code. After converting to RGB format, you can easily generate bitmap pictures and then display them on the ImageView.

In fact, this part of the more troublesome is the focus of the steps. First we calculate the formula by luminance:

Bright = 0.299 * r + 0.587 * g + 0.114 * b;
Every 500ms calculates the luminance average of the entire picture, and then compares it with the previous value, if within a certain interval, the camera front-end image is stabilized, a focus is made, and the corresponding flag is set to ensure that the focus is not in place until the next stable image appears. The focus function is as follows:

Private void AutoFocus()    {LongCurrentTime =0, Stabletime =0;        CurrentTime = System.currenttimemillis (); Bright = Pictures.getlight (RGB);if(Math.Abs (Bright-lastbright) > Focusthreshold)            {lastbright = bright; Hasfocus =false;        Stabletime = currenttime; }Else{if(!hasfocus && currenttime-stabletime >= -) {Camera.autofocus (NewAutofocuscallback () {@Override                     Public void Onautofocus(BooleanSuccess, Camera camera) {}}); Hasfocus =true; }        }    }
3. Image algorithm

Pictures.java:
Entered here to really begin to relate to the image processing algorithm part, here will involve a few relatively simple algorithm, the principle of the algorithm here is not much to say, online about the image processing technology to explain a lot.

In the software, there are several static methods of the following algorithms:
* mentioned above will convert yuv420p into RGB format: DECODEYUV420SP ();
* Calculate the average brightness of the image: GetLight ();
* Get an array of eight-bit grayscale images: Getlightarray ();
* Color map converted to grayscale: Converttogrey ();
* Sobel Transform: Sobel ();
* Binary processing of images: TurnTo2 ();
* Edge Extraction: findframe ();

Here I put each algorithm into a static method, so that it is convenient to use the algorithm in other code to process the image, the image processing in the second part of the Getscreen Camera Preview callback function Onpreviewframe (), which is the core step of the entire software. The code is as follows:

@Override     Public void Onpreviewframe(byte[] data, Camera camera) {RGB =New int[Pictures.pic_length]; Sobelpic =New int[Pictures.pic_length]; Grey =New int[Pictures.pic_length]; PICTURES.DECODEYUV420SP (RGB, data, width, height);Switch(Mainactivity.whichtodisplay) { Case 1://Display grayscale imagePictures.converttogrey (RGB, grey); display = grey; Break; Case 2: Pictures.converttogrey (RGB, grey);//Sobel transform after displayPictures.sobel (grey, width, height, sobelpic); display = Sobelpic; Break; Case 3://Two value displayPictures.converttogrey (RGB, grey);            Pictures.sobel (grey, width, height, sobelpic);            Pictures.turnto2 (Sobelpic); display = Sobelpic; Break;default: display = RGB;        } bitmap = Bitmap.createbitmap (display, width, height, config.rgb_565);        Mainactivity.setimageview (bitmap);    AutoFocus (); }

First convert the image to the RGB format (for ease of processing), then switch the different algorithms according to the menu options of the main interface, and display the images under different algorithms on the ImageView, and then focus on them.

Usually in the image processing process, the first simulation in MATLAB or Visual Stuio. This need to constantly import the picture → then compile → final output. With this Android image processing tool can be real-time observation of various images in different algorithms of the results of the map, compared to more convenient. Only now support the algorithm is not many, there will be time to continue to learn some common algorithms added to the software. I will also instantly update the blog to share with you.

If you have any comments or questions can be a message at any time to discuss ~

apk:http://yun.baidu.com/share/link?shareid=515722756&uk=67973003
GitHub Source Address: Https://github.com/hust-MC/Find_Different.git

Android image processing software

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.