Zeit eingeführt die Doppelalarme verhindert

Durch clustern der Pixel eventuell bessere Detektion realisiert
This commit is contained in:
Michael Pilhoefer 2023-06-19 18:42:25 +02:00
parent 840d323317
commit 7f41e4a555
3 changed files with 95 additions and 18 deletions

View File

@ -3,6 +3,8 @@ package com.example.greenwatch;
import android.graphics.ImageFormat;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.Size;
import android.widget.TextView;
@ -27,9 +29,22 @@ public class KameraAktivitaet extends AppCompatActivity {
private boolean isMotionDetected;
private boolean camera_alarm;
private TextView alarm;
private TextView test;
// Bildverarbeitung Variablen
private ByteBuffer previousBuffer;
private int previousWidth;
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
protected void onCreate(Bundle savedInstanceState) {
@ -38,6 +53,7 @@ public class KameraAktivitaet extends AppCompatActivity {
// NUR FÜR TESTS
alarm = findViewById(R.id.textView);
test = findViewById(R.id.textView2);
// PREVIEW //
// previewView = findViewById(R.id.previewView);
@ -85,9 +101,8 @@ public class KameraAktivitaet extends AppCompatActivity {
if (isMotionDetected) {
alarm.setText("ALARM");
camera_alarm = true;
} else {
camera_alarm = false;
alarm.setText("OK");
alarmResetHandler.postDelayed(alarmResetRunnable, ALARM_RESET_DELAY);
}
}
@ -108,35 +123,87 @@ public class KameraAktivitaet extends AppCompatActivity {
// Bildverarbeitung zur Bewegungserkennung
private boolean compareFrames(Image currentImage) {
ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer();
Image.Plane[] planes = currentImage.getPlanes();
ByteBuffer currentBuffer = planes[0].getBuffer();
int currentWidth = currentImage.getWidth();
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) {
return false;
}
for (int row = 0; row < previousHeight; row++) {
for (int col = 0; col < previousWidth; col++) {
// Blöcke weiter verkleinern
int blockSize = kleinstesQuadrat(previousHeight, previousWidth) / 8;
int previousIndex = row * previousWidth + col;
int currentIndex = row * currentWidth + col;
//System.out.println(blockSize);
int previousPixel = previousBuffer.get(previousIndex) & 0xFF;
int currentPixel = currentBuffer.get(currentIndex) & 0xFF;
int numBlocksX = currentWidth / blockSize;
int numBlocksY = currentHeight / blockSize;
int pixelDifference = Math.abs(previousPixel - currentPixel);
int threshold = 120;
for (int blockY = 0; blockY < numBlocksY; blockY++) {
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) {
// Testzwecke
//String text = String.valueOf(pixelDifference);
System.out.println(pixelDifference);
return true;
}
}
}
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");
}
};
}

View File

@ -35,6 +35,6 @@ public class MainActivity extends AppCompatActivity {
}
private void starteKameraAktivitaet() {
startActivity(new Intent(this, Kamera_Beschleunigungssensor.class));
startActivity(new Intent(this, KameraAktivitaet.class));
}
}

View File

@ -30,5 +30,15 @@
app:layout_constraintTop_toTopOf="parent"
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>