Zeit eingeführt die Doppelalarme verhindert
Durch clustern der Pixel eventuell bessere Detektion realisiert
This commit is contained in:
parent
840d323317
commit
7f41e4a555
@ -3,6 +3,8 @@ package com.example.greenwatch;
|
|||||||
import android.graphics.ImageFormat;
|
import android.graphics.ImageFormat;
|
||||||
import android.media.Image;
|
import android.media.Image;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
import android.util.Size;
|
import android.util.Size;
|
||||||
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@ -27,9 +29,22 @@ public class KameraAktivitaet extends AppCompatActivity {
|
|||||||
private boolean isMotionDetected;
|
private boolean isMotionDetected;
|
||||||
private boolean camera_alarm;
|
private boolean camera_alarm;
|
||||||
private TextView alarm;
|
private TextView alarm;
|
||||||
|
private TextView test;
|
||||||
|
|
||||||
|
|
||||||
|
// Bildverarbeitung Variablen
|
||||||
private ByteBuffer previousBuffer;
|
private ByteBuffer previousBuffer;
|
||||||
private int previousWidth;
|
private int previousWidth;
|
||||||
private int previousHeight;
|
private int previousHeight;
|
||||||
|
private int threshold = 50;
|
||||||
|
private int startX;
|
||||||
|
private int startY;
|
||||||
|
private int endX;
|
||||||
|
private int endY;
|
||||||
|
|
||||||
|
private static final long ALARM_RESET_DELAY = 5000;
|
||||||
|
private boolean isAlarmSet = false;
|
||||||
|
private Handler alarmResetHandler = new Handler();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -38,6 +53,7 @@ public class KameraAktivitaet extends AppCompatActivity {
|
|||||||
|
|
||||||
// NUR FÜR TESTS
|
// NUR FÜR TESTS
|
||||||
alarm = findViewById(R.id.textView);
|
alarm = findViewById(R.id.textView);
|
||||||
|
test = findViewById(R.id.textView2);
|
||||||
|
|
||||||
// PREVIEW //
|
// PREVIEW //
|
||||||
// previewView = findViewById(R.id.previewView);
|
// previewView = findViewById(R.id.previewView);
|
||||||
@ -85,9 +101,8 @@ public class KameraAktivitaet extends AppCompatActivity {
|
|||||||
if (isMotionDetected) {
|
if (isMotionDetected) {
|
||||||
alarm.setText("ALARM");
|
alarm.setText("ALARM");
|
||||||
camera_alarm = true;
|
camera_alarm = true;
|
||||||
} else {
|
|
||||||
camera_alarm = false;
|
alarmResetHandler.postDelayed(alarmResetRunnable, ALARM_RESET_DELAY);
|
||||||
alarm.setText("OK");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -108,35 +123,87 @@ public class KameraAktivitaet extends AppCompatActivity {
|
|||||||
|
|
||||||
// Bildverarbeitung zur Bewegungserkennung
|
// Bildverarbeitung zur Bewegungserkennung
|
||||||
private boolean compareFrames(Image currentImage) {
|
private boolean compareFrames(Image currentImage) {
|
||||||
|
Image.Plane[] planes = currentImage.getPlanes();
|
||||||
ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer();
|
ByteBuffer currentBuffer = planes[0].getBuffer();
|
||||||
|
|
||||||
int currentWidth = currentImage.getWidth();
|
int currentWidth = currentImage.getWidth();
|
||||||
int currentHeight = currentImage.getHeight();
|
int currentHeight = currentImage.getHeight();
|
||||||
|
int yRowStride = planes[0].getRowStride();
|
||||||
|
int yPixelStride = planes[0].getPixelStride();
|
||||||
|
|
||||||
|
|
||||||
|
//System.out.println("Höhe: " + currentHeight + " Breite: " + currentWidth);
|
||||||
|
|
||||||
if (previousWidth != currentWidth || previousHeight != currentHeight) {
|
if (previousWidth != currentWidth || previousHeight != currentHeight) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int row = 0; row < previousHeight; row++) {
|
// Blöcke weiter verkleinern
|
||||||
for (int col = 0; col < previousWidth; col++) {
|
int blockSize = kleinstesQuadrat(previousHeight, previousWidth) / 8;
|
||||||
|
|
||||||
int previousIndex = row * previousWidth + col;
|
//System.out.println(blockSize);
|
||||||
int currentIndex = row * currentWidth + col;
|
|
||||||
|
|
||||||
int previousPixel = previousBuffer.get(previousIndex) & 0xFF;
|
int numBlocksX = currentWidth / blockSize;
|
||||||
int currentPixel = currentBuffer.get(currentIndex) & 0xFF;
|
int numBlocksY = currentHeight / blockSize;
|
||||||
|
|
||||||
int pixelDifference = Math.abs(previousPixel - currentPixel);
|
for (int blockY = 0; blockY < numBlocksY; blockY++) {
|
||||||
int threshold = 120;
|
for (int blockX = 0; blockX < numBlocksX; blockX++) {
|
||||||
|
startX = blockX * blockSize;
|
||||||
|
startY = blockY * blockSize;
|
||||||
|
endX = startX + blockSize;
|
||||||
|
endY = startY + blockSize;
|
||||||
|
|
||||||
|
float currentLuminance = berechneMittlereLuminanz(currentBuffer, yRowStride, yPixelStride);
|
||||||
|
float previousLuminance = berechneMittlereLuminanz(previousBuffer, yRowStride, yPixelStride);
|
||||||
|
|
||||||
|
int pixelDifference = Math.abs((int) previousLuminance - (int) currentLuminance);
|
||||||
|
|
||||||
|
//System.out.println(pixelDifference);
|
||||||
|
|
||||||
if (pixelDifference > threshold) {
|
if (pixelDifference > threshold) {
|
||||||
// Testzwecke
|
System.out.println(pixelDifference);
|
||||||
//String text = String.valueOf(pixelDifference);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private float berechneMittlereLuminanz(ByteBuffer buffer, int rowStride, int pixelStride) {
|
||||||
|
int sumLuminance = 0;
|
||||||
|
int countPixels = 0;
|
||||||
|
|
||||||
|
for (int row = startY; row < endY; row++) {
|
||||||
|
for (int col = startX; col < endX; col++) {
|
||||||
|
int bufferIndex = row * rowStride + col * pixelStride;
|
||||||
|
int currentPixel = buffer.get(bufferIndex) & 0xFF;
|
||||||
|
|
||||||
|
sumLuminance += currentPixel;
|
||||||
|
countPixels++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float averageLuminance = (float) sumLuminance / countPixels;
|
||||||
|
|
||||||
|
return averageLuminance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int kleinstesQuadrat(int height, int width) {
|
||||||
|
if (height == width) {
|
||||||
|
return height;
|
||||||
|
} else if (height > width) {
|
||||||
|
return kleinstesQuadrat(height - width, width);
|
||||||
|
} else {
|
||||||
|
return kleinstesQuadrat(height, width - height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Runnable alarmResetRunnable = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
camera_alarm = false;
|
||||||
|
alarm.setText("OK");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -35,6 +35,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void starteKameraAktivitaet() {
|
private void starteKameraAktivitaet() {
|
||||||
startActivity(new Intent(this, Kamera_Beschleunigungssensor.class));
|
startActivity(new Intent(this, KameraAktivitaet.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,5 +30,15 @@
|
|||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:layout_constraintVertical_bias="0.247" />
|
app:layout_constraintVertical_bias="0.247" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView2"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="TextView"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="@+id/textView" />
|
||||||
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user