public class MainActivity extends Activity {
Button button;
ImageView res4;
TextView T1;
private static final String TAG = "training";
private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "Open CV loaded successfully");
break;
default:
super.onManagerConnected(status);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this,mLoaderCallBack);
}
public void addListenerOnButton() {
// imageView = (ImageView) findViewById(R.id.imageView1);
res4 = (ImageView) findViewById(R.id.imageView2);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
train();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
public void train (){
String path = Environment.getExternalStorageDirectory().toString();
Mat trainData = new Mat();
Mat response_array = new Mat();
Mat img =Imgcodecs.imread("/storage/emulated/0/DCIM/Camera/n1.png");
img.convertTo(img, CvType.CV_32FC1);
Size dsize = new Size(50, 50);
Imgproc.resize(img, img, dsize);
img.convertTo(img, CvType.CV_32FC1);
Mat imgResized = img.reshape(1, 1);
trainData.push_back(imgResized);
Mat response = new Mat();
Mat tmp;
tmp=response_array.reshape(1,1); //make continuous `
tmp.convertTo(response,CvType.CV_32FC1); // Convert to float`
KNearest knn = KNearest.create();
knn.train(trainData,Ml.ROW_SAMPLE, response);
Bitmap bmpOut1 = Bitmap.createBitmap(trainData.cols(), trainData.rows(), Bitmap.Config.ARGB_8888);
trainData.convertTo(trainData,CvType.CV_8UC1);
Utils.matToBitmap(trainData, bmpOut1);
File file = new File(path);
file.mkdirs();
File file1 = new File(path, "train.jpg");
OutputStream fout = null;
try { fout = new FileOutputStream(file1);
BufferedOutputStream bos = new BufferedOutputStream(fout);
bmpOut1.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
bmpOut1.recycle();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) {e.printStackTrace();}
}
}
I get these errors .Please help me and also i don't know how can i recognize my code

↧