Browse Source

Zeit eingeführt die Doppelalarme verhindert

Durch clustern der Pixel eventuell bessere Detektion realisiert
Kamera
Michael Pilhoefer 1 year ago
parent
commit
7f41e4a555

+ 84
- 17
app/src/main/java/com/example/greenwatch/KameraAktivitaet.java View File

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;
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) {


// 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);
if (isMotionDetected) { if (isMotionDetected) {
alarm.setText("ALARM"); alarm.setText("ALARM");
camera_alarm = true; camera_alarm = true;
} else {
camera_alarm = false;
alarm.setText("OK");

alarmResetHandler.postDelayed(alarmResetRunnable, ALARM_RESET_DELAY);
} }


} }


// Bildverarbeitung zur Bewegungserkennung // Bildverarbeitung zur Bewegungserkennung
private boolean compareFrames(Image currentImage) { 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 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++) {
for (int col = 0; col < previousWidth; col++) {
// Blöcke weiter verkleinern
int blockSize = kleinstesQuadrat(previousHeight, previousWidth) / 8;

//System.out.println(blockSize);

int numBlocksX = currentWidth / blockSize;
int numBlocksY = currentHeight / blockSize;


int previousIndex = row * previousWidth + col;
int currentIndex = row * currentWidth + col;
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;


int previousPixel = previousBuffer.get(previousIndex) & 0xFF;
int currentPixel = currentBuffer.get(currentIndex) & 0xFF;
float currentLuminance = berechneMittlereLuminanz(currentBuffer, yRowStride, yPixelStride);
float previousLuminance = berechneMittlereLuminanz(previousBuffer, yRowStride, yPixelStride);


int pixelDifference = Math.abs(previousPixel - currentPixel);
int threshold = 120;
int pixelDifference = Math.abs((int) previousLuminance - (int) currentLuminance);

//System.out.println(pixelDifference);


if (pixelDifference > threshold) { if (pixelDifference > threshold) {
// Testzwecke
//String text = String.valueOf(pixelDifference);
System.out.println(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");
}
};
}


+ 1
- 1
app/src/main/java/com/example/greenwatch/MainActivity.java View File

} }


private void starteKameraAktivitaet() { private void starteKameraAktivitaet() {
startActivity(new Intent(this, Kamera_Beschleunigungssensor.class));
startActivity(new Intent(this, KameraAktivitaet.class));
} }
} }

+ 10
- 0
app/src/main/res/layout/activity_main.xml View File

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…
Cancel
Save