Skip to content

Commit

Permalink
add PDM graph
Browse files Browse the repository at this point in the history
add multiple image selection for MAA
  • Loading branch information
linzuzeng committed Feb 5, 2018
1 parent 610b140 commit 3c3a2ff
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 20 deletions.
Binary file added app/src/main/assets/IMG_20170707_103528R.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 33 additions & 3 deletions app/src/main/java/org/lezizi/microsphere/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.opencv.imgproc.Imgproc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -86,9 +87,34 @@ protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (detection_mode == R.id.mode_maa)
new MAADetectionTask().execute(app_mainpath);
{
Album.image(MainActivity.this) // Image selection.
.multipleChoice()
.requestCode(0)
.camera(true)
.columnCount(3)
.onResult(new Action<ArrayList<AlbumFile>>() {
@Override
public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
for (AlbumFile file: result){
try {
copyFile(new FileInputStream(new File(file.getPath())), new FileOutputStream(new File(app_mainpath,file.getName())));
} catch (IOException e) {
e.printStackTrace();
}
}
new MAADetectionTask().execute(app_mainpath);
}
})
.onCancel(new Action<String>() {
@Override
public void onAction(int requestCode, @NonNull String result) {
new MAADetectionTask().execute(app_mainpath);
}
})
.start();
}
else{
Album.image(MainActivity.this) // Image selection.
.singleChoice()
Expand All @@ -102,7 +128,6 @@ public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
add_log("PMD: "+file.getPath());
Intent intent = new Intent(MainActivity.this, PMDActivity.class);
intent.putExtra(PMDActivity_Filename, file.getPath());

startActivity(intent);
}

Expand Down Expand Up @@ -378,10 +403,15 @@ private void process(String filename, String fileoutname) {
try {
cimg = Imgcodecs.imread(filename);
Imgproc.cvtColor(cimg, img, Imgproc.COLOR_RGBA2GRAY);

} catch (Exception e) {
publishProgress("ERROR: File contains an unsupported image format. ");
return;
}
if (cimg.cols()>3000){
publishProgress("ERROR: Image resolution is too high. ");
return;
}
Mat th3 = new Mat();
Imgproc.adaptiveThreshold(img, th3, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 11, 2);
Imgproc.dilate(th3, th3, Mat.ones(2, 2, CvType.CV_8U));
Expand Down
148 changes: 139 additions & 9 deletions app/src/main/java/org/lezizi/microsphere/PMDActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;


import org.opencv.android.Utils;
import org.opencv.core.CvType;
Expand All @@ -22,21 +32,24 @@
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.security.Key;
import java.util.ArrayList;
import java.util.List;


public class PMDActivity extends AppCompatActivity {
protected MatOfKeyPoint keypoints;
protected Mat cimg;
protected Mat resized;
protected float barValue1;
protected float barValue2;
protected float barValue3;
protected void update_preview(){
SeekBar bar1 = findViewById(R.id.seekBar1);
SeekBar bar2 = findViewById(R.id.seekBar2);
SeekBar bar3 = findViewById(R.id.seekBar3);
float barValue1 = bar1.getProgress()/(float)100.0;
float barValue2 = bar2.getProgress()/(float)100.0;
float barValue3 = bar3.getProgress()/(float)100.0;
barValue1 = bar1.getProgress()/(float)100.0;
barValue2 = bar2.getProgress()/(float)100.0;
barValue3 = bar3.getProgress()/(float)100.0;
TextView result= findViewById(R.id.textView2);

Mat out=new Mat(cimg.rows()/4, cimg.cols()/4, CvType.CV_64FC3);
Expand All @@ -57,7 +70,7 @@ protected void update_preview(){
}
}

result.setText(String.format("(%.2f pix<d)=%d; (%.2f pix< d <%.2f pix)=%d; (%.2f pix< d <%.2f pix)=%d;",
result.setText(String.format("(d<%.2f)=%d (%.2f<d<%.2f)=%d (%.2f<d<%.2f)=%d",
barValue1,
samll_list.size(),
barValue1,
Expand Down Expand Up @@ -91,6 +104,107 @@ protected void update_preview(){
ImageView iv = findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
}
protected void draw_histogram(){
final int bin_num=50;
SeekBar bar1 = findViewById(R.id.seekBar1);
float bin_step= (bar1.getMax()/(float)100.0)/(float)bin_num;
int histogram[] = new int[bin_num];
for (KeyPoint vkp : keypoints.toList())
{
int i = (int)(vkp.size/bin_step);
if (i>=bin_num) i = bin_num-1;
if (i<=0) i=0;
histogram[i]+=1;
}

LineChart mChart = findViewById(R.id.result_barchart);

ArrayList<Entry> BARENTRY1= new ArrayList<>();
ArrayList<Entry> BARENTRY2= new ArrayList<>();
ArrayList<Entry> BARENTRY3= new ArrayList<>();
ArrayList<Entry> BARENTRY4= new ArrayList<>();
for (int i=0;i<bin_num;i++){
float size = (float)i*bin_step;
if (i<=(int)(barValue1/bin_step)){
BARENTRY1.add(new Entry(size,histogram[i]));
}
if ((int)(barValue1/bin_step)<=i && i<=(int)(barValue2/bin_step)){
BARENTRY2.add(new Entry(size,histogram[i]));
}
if ((int)(barValue2/bin_step)<=i && i<=(int)(barValue3/bin_step)){
BARENTRY3.add(new Entry(size,histogram[i]));
}
if (i>=(int)(barValue3/bin_step)){
BARENTRY4.add(new Entry(size,histogram[i]));
}
}
List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
if (BARENTRY1.size()>0){
LineDataSet DS1=new LineDataSet(BARENTRY1, String.format("(d <%.1f)",barValue1));
DS1.setFillColor(getResources().getColor(R.color.chart_color1));
DS1.setColor(getResources().getColor(R.color.chart_color1));
DS1.setDrawCircles(false);
DS1.setDrawValues(false);
DS1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
DS1.setDrawFilled(true);
dataSets.add( DS1);
}

if (BARENTRY2.size()>0){
LineDataSet DS1= new LineDataSet(BARENTRY2, String.format("(%.1f< d <%.1f)",barValue1,barValue2));
DS1.setFillColor(getResources().getColor(R.color.chart_color2));
DS1.setColor(getResources().getColor(R.color.chart_color2));
DS1.setDrawCircles(false);
DS1.setDrawValues(false);
DS1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
DS1.setDrawFilled(true);
dataSets.add( DS1);
}

if (BARENTRY3.size()>0){
LineDataSet DS1=new LineDataSet(BARENTRY3, String.format("(%.1f< d <%.1f)",barValue2,barValue3));
DS1.setFillColor(getResources().getColor(R.color.chart_color3));
DS1.setColor(getResources().getColor(R.color.chart_color3));
DS1.setDrawCircles(false);
DS1.setDrawValues(false);
DS1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
DS1.setDrawFilled(true);
dataSets.add( DS1);
}

if (BARENTRY4.size()>0){
LineDataSet DS1=new LineDataSet(BARENTRY4, String.format("(%.1f< d)",barValue3));
DS1.setFillColor(getResources().getColor(R.color.chart_color4));
DS1.setColor(getResources().getColor(R.color.chart_color4));
DS1.setDrawCircles(false);
DS1.setDrawValues(false);
DS1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
DS1.setDrawFilled(true);
dataSets.add( DS1);
}


LineData BARDATA = new LineData( dataSets);
XAxis xAxis = mChart.getXAxis();

xAxis.setDrawAxisLine(false);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMinimum(0);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setAxisMinimum(0);
rightAxis.setDrawAxisLine(false);
rightAxis.setDrawLabels(false);

mChart.setMinOffset(0);
mChart.setData(BARDATA);
mChart.getDescription().setText("Diameter");
// mChart.getDescription().setYOffset(-25);

mChart.invalidate();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -120,11 +234,13 @@ protected void onCreate(Bundle savedInstanceState) {
SeekBar bar1 = findViewById(R.id.seekBar1);
SeekBar bar2 = findViewById(R.id.seekBar2);
SeekBar bar3 = findViewById(R.id.seekBar3);

bar1.setMax((int)(max_size*100.0));
bar2.setMax((int)(max_size*100.0));
bar3.setMax((int)(max_size*100.0));


bar1.setProgress((int)(max_size*30.0));
bar2.setProgress((int)(max_size*60.0));
bar3.setProgress((int)(max_size*90.0));
SeekBar.OnSeekBarChangeListener listener=new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Expand All @@ -150,6 +266,20 @@ public void onStopTrackingTouch(SeekBar seekBar) {
bar3.setOnSeekBarChangeListener(listener);

update_preview();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LinearLayout result_barchart = findViewById(R.id.result_layout);
if (result_barchart.getVisibility()==View.VISIBLE) {
result_barchart.setVisibility(View.INVISIBLE);
}else{
result_barchart.setVisibility(View.VISIBLE);
draw_histogram();
}
}
});
}

}
50 changes: 42 additions & 8 deletions app/src/main/res/layout/activity_pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

Expand Down Expand Up @@ -51,7 +51,7 @@
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView3"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

Expand All @@ -78,15 +78,49 @@
/>

</LinearLayout>

<com.github.mikephil.charting.charts.BarChart
android:background="#ffffff"
android:id="@+id/result_text"
android:visibility="invisible"
<LinearLayout
android:id="@+id/result_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#ffffff"
android:visibility="invisible"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:scrollHorizontally="false"
android:text="PM Diameter Distribution"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />


<TextView
android:textColor="#000000"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number of particles"
android:textSize="7dp"
/>
<com.github.mikephil.charting.charts.LineChart
android:background="#ffffff"
android:id="@+id/result_barchart"

android:layout_width="match_parent"
android:layout_height="250dp"
/>

</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
<color name="colorPrimaryBlack">#FF2B2B2B</color>

<color name="colorWhite">#FFF</color>

<color name="colorFont">#333</color>
<color name="chart_color1">#32e413</color>
<color name="chart_color2">#1781e4</color>
<color name="chart_color3">#ae09f4</color>
<color name="chart_color4">#d91414</color>
</resources>

0 comments on commit 3c3a2ff

Please sign in to comment.