Я работаю над автономным обнаружением препятствий и избегаю 2 -х колесного прототипа. Телефон Android используется в качестве обработчивого блока и, очевидно, камера. Моя проблема связана конкретно при принятии решения, когда я получаю бинарную маску от порогового модуля. Мне нужна помощь! < /P>
Вот часть кода, которая должна быть улучшена: < /p>
private static int findFirstZeroPosition(Mat mat, int col) {
int firstZero = -1;
for (int y = mat.rows() - 1; y >= 0; y--) {
if (mat.get(y, col)[0] == 0) {
firstZero = y;
break;
}
}
return firstZero;
}
public static int findLongestSequenceMiddle(Mat mat) {
int maxLength = 0; // length of the longest sequence of 1
int currentLength = 0; // length of the current sequence
int maxStart = 0; // start of the longest sequence
// Go along the Mat
for (int i = 0; i < mat.cols(); i++) {
double val = mat.get(0, i)[0];
if (val == 1) {
currentLength++;
// If the current sequence is longer than the previous longest, update the start
// and length
if (currentLength > maxLength) {
maxLength = currentLength;
maxStart = i - currentLength + 1;
}
} else {
currentLength = 0;
}
}
// Calculate the middle index of the longest sequence and return it
return maxStart + (maxLength / 2);
}
public static Mat calculateDepthMapParallel(Mat mask, BluetoothGattCharacteristic characteristic,
BluetoothGatt bluetoothGatt) {
int width = mask.cols();
int height = mask.rows();
Mat mat = new Mat(1, width, CvType.CV_8UC1);
Mat distMap = new Mat(height, width, CvType.CV_8UC1, new Scalar(255));
CountDownLatch latch = new CountDownLatch(width);
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Process each column in parallel
for (int x = 0; x < width; x++) {
final int currentColumn = x;
executor.execute(() -> {
try {
int firstZeroPosition = findFirstZeroPosition(mask, currentColumn);
synchronized (mat) {
mat.put(0, currentColumn, new byte[] { (byte) 255 });
}
if (firstZeroPosition != -1) {
GroundPlaneCamera camera = new GroundPlaneCamera(Fx, Fy, width / 2.0, height / 2.0, Y, tilt);
Point imagePoint = new Point(currentColumn, firstZeroPosition);
Point groundPoint = camera.imageToGround(imagePoint);
double Z = groundPoint.y;
double X = groundPoint.x;
double minDepth = 0; // TO CHANGE
double maxDepth = 40; // TO CHANGE
Log.d("worldCoordinate", "X: " + X + " Y: " + Z);
int intensity = (int) Math.max(0, Math.min(255,
MAP.map(Z, minDepth, maxDepth, 0, 255)));
// Thread-safe update of mat
synchronized (mat) {
byte[] data = new byte[] { (byte) intensity };
mat.put(0, currentColumn, data);
}
}
} finally {
latch.countDown();
}
});
}
// Wait for all threads to finish
try {
latch.await();
} catch (InterruptedException e) {
Log.e("ParallelView", "Thread interruption during depth map calculation", e);
Thread.currentThread().interrupt(); // Restore the interrupted status
// Optionally, you can handle the interruption here
distMap.setTo(new Scalar(255)); // Default to white if interrupted
}
executor.shutdown();
Mat matCopy = mat.clone();
int intOfZ = (int) Math.max(0, Math.min(255,
MAP.map(20, 0, 40, 0, 255)));
Imgproc.threshold(matCopy, matCopy, intOfZ, 1, Imgproc.THRESH_BINARY);
int a = findLongestSequenceMiddle(matCopy);
Decision d = new Decision();
d.directionFromPathMiddlePosition(a, characteristic, bluetoothGatt);
// Repeat mat to match the height of distMap
Core.repeat(mat, height, 1, distMap);
return distMap;
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... vel-enough
Улучшение кода Android Java для лучшей производительности? (Достаточно низкий уровень) ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Улучшение кода Android Java для лучшей производительности? (Достаточно низкий уровень)
Anonymous » » в форуме JAVA - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-