Hallo Leute,
ich habe schon in Forum wegen Kantenerkennung gefragt, aber hier in dem Beitrag möchte ich auf Konkrete Sache eingehen
Legostein kantenerkennung forumsbeitrag
ich bekomme diese Ergebnis
01-11 14:19:49.709: I/System.out(15056): test1
01-11 14:19:49.709: I/System.out(15056): test2
01-11 14:19:49.709: I/System.out(15056): test3
01-11 14:19:49.709: I/System.out(15056): Error: null
warum wird null gegeben . das Mat mat0 = Highgui.imread(input.getAbsolutePath());
sollte doch image laden?
public EdgeDetector(File input){
try {
System.out.println("test1");
Mat mat0 = Highgui.imread(input.getAbsolutePath());
System.out.println("test");
System.out.println("test4");
Mat mat1 = new Mat(mat0.rows(), mat0.cols(), mat0.type());
Imgproc.cvtColor(mat0, mat1, Imgproc.COLOR_RGB2GRAY); // RGB-Bild
System.out.println("test5"); // wird zu
// einem
// Graustufenbild
// umgewandelt
// (zur
// besseren
// Erkennung)
// Mat mat1 = new Mat();
Imgproc.GaussianBlur(mat0, mat1, new Size(5, 5), 2, 2); // macht
// einen
// blur
// effekt.
// vorherige
// Werte =
// (9,9),2,2
mat1 = detectEdges(mat1);
Mat circles = new Mat();
double iCannyUpperThreshold = 10; // 10
int iMinRadius = 10; // 10
int iMaxRadius = 40; // 40
double iAccumulator = 30; // 30
Imgproc.HoughCircles(mat1, circles, Imgproc.CV_HOUGH_GRADIENT, 1,
20, iCannyUpperThreshold, iAccumulator, iMinRadius,
iMaxRadius); // alternativ 1, 20, 10, 20, 7, 13
if (circles.cols() > 0)
for (int x = 0; x < circles.cols(); x++) {
double vCircle[] = circles.get(0, x);
if (vCircle == null)
break;
Point pt = new Point(Math.round(vCircle[0]),
Math.round(vCircle[1]));
int radius = (int) Math.round(vCircle[2]);
System.out.println("Der Mittelpunkt ist in: " + pt.x + " "
+ pt.y);
System.out.println("Der Radius ist: " + radius);
}
System.out.println("anzahl gefundener kreise: " + circles.cols());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
public static Mat detectEdges(Mat matOriginal) {
Mat imageCny = new Mat();
Imgproc.Canny(matOriginal, imageCny, 300, 600, 5, true); // f¸r BLAU
// funktioniert:
// 300, 600,
// 5, true ;
// experimentelle
// werte:
// 40, 120,
return imageCny; // enth‰lt jetzt ein bild, in welchem alle kanten mit
// der Farbe WEISS dargestellt sind und der rest des
// Bildes ist schwarz
}
}}```
Hier in onActivityResult wird EdgeDetector aufgerufen
```public class MainActivity extends Activity implements CvCameraViewListener2 {
public boolean mainIsOpen = true;
String mCurrentPhotoPath;
public static final int REQUEST_IMAGE_CAPTURE = 1;
private int mViewMode;
private SeekBar seekbar;
int iCannyLowerThreshold;
int iCannyUpperThreshold;
private File photo;
private static final int VIEW_MODE_RGBA = 0;
private static final int VIEW_MODE_CANNY = 1;
private static final int VIEW_MODE_THRESH = 2;
private Mat mRgba;
private Mat mIntermediateMat;
private Mat mGray;
static {
if(!OpenCVLoader.initDebug()) {
Log.d("ERROR", "Unable to load OpenCV");
} else {
Log.d("SUCCESS", "OpenCV loaded");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addLego(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
// Create the File where the photo should go
File photoFile = null;
try {
photo = photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
/*
* Intent intent = new Intent(this, LegoSaveActivity.class);
* intent.putExtra("fileName", this.mCurrentPhotoPath);
* startActivity(intent);
*/
}
}
// Intent intent = new Intent(this, DisplayLegoDetailsActivity.class);
}
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 2;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(this, LegoSaveActivity.class);
intent.putExtra("fileName", this.mCurrentPhotoPath);
startActivity(intent);
EdgeDetector edgeDetector = new EdgeDetector(photo);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Video saved to:
" + data.getData(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
public void showInfos(View view) {
LegoDbHelper dbHelper = new LegoDbHelper(this.getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
dbHelper.onCreate(db);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && mainIsOpen == false) {
mainIsOpen = true;
setContentView(R.layout.activity_main);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onCameraViewStarted(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void onCameraViewStopped() {
// TODO Auto-generated method stub
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100);
Imgproc.cvtColor( mIntermediateMat, mRgba,
Imgproc.COLOR_RGBA2GRAY, 4 );
return mRgba;
}
}
LG Anni
*** Edit ***
komische daran ist wenn ich nochmal ausgeführt habe hat das zurückgegeben, dabei habe ich etwas fotografiert und Foto enthält kein Kreis
01-11 14:41:20.724: I/System.out(19336): test1
01-11 14:41:21.824: I/System.out(19336): test
01-11 14:41:21.824: I/System.out(19336): test4
01-11 14:41:21.869: I/System.out(19336): test5
01-11 14:41:29.169: I/System.out(19336): Der Mittelpunkt ist in: 633.0 1159.0
01-11 14:41:29.169: I/System.out(19336): Der Radius ist: 20
01-11 14:41:29.169: I/System.out(19336): Der Mittelpunkt ist in: 1639.0 1743.0
01-11 14:41:29.169: I/System.out(19336): Der Radius ist: 21
01-11 14:41:29.169: I/System.out(19336): Der Mittelpunkt ist in: 1479.0 1881.0
01-11 14:41:29.169: I/System.out(19336): Der Radius ist: 16
01-11 14:41:29.169: I/System.out(19336): anzahl gefundener kreise: 3