Commit 2314dcee authored by Nguyeng Hoang Giang's avatar Nguyeng Hoang Giang

inital commit

parents
#Fri Mar 11 11:18:36 ICT 2022
gradle.version=7.2
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
ext{
version = [
'javacv' : '1.5.3',
'opencv' : '4.3.0',
]
}
dependencies {
implementation('org.bytedeco:opencv:4.5.1-1.5.5:android-x86')
implementation('org.bytedeco:opencv:4.3.0-1.5.3:android-x86_64')
implementation('org.bytedeco:openblas:0.3.13-1.5.5:android-x86')
implementation('org.bytedeco:openblas:0.3.9-1.5.3:android-x86_64')
implementation(group:'org.apache.commons', name: 'commons-collections4', version: '4.0')
implementation(group :'org.bytedeco', name: 'javacv-platform', version: '1.5.5'){
exclude group: 'org.bytedeco.javacpp-presets'
}
implementation('org.apache.commons:commons-lang3:3.12.0')
implementation('com.github.sh0nk:matplotlib4j:0.5.0')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
\ No newline at end of file
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
#!/usr/bin/env sh
#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MSYS* | MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=`expr $i + 1`
done
case $i in
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=`save "$@"`
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
exec "$JAVACMD" "$@"
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
rootProject.name = 'FleetDetection'
import org.apache.commons.lang3.ArrayUtils;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.w3c.dom.ls.LSOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class Fleet_single_cam {
private String id;
private Mat image;
private double resize_ratio;
private int num_x;
private int num_y;
private int num_z;
private boolean show;
// private
HashMap<String, intArray> side;
public class intArray {
private int[][] intArray;
public intArray(int width, int height) {
this.intArray = new int[width][height];
}
public intArray(int[][] intArray) {
this.intArray = intArray;
}
public int[][] getIntArray() {
return intArray.clone();
}
public void setIntArray(int[][] intArray) {
this.intArray = intArray;
}
}
public Fleet_single_cam(String id, String image, double resize_ratio, int num_x, int num_y, int num_z, boolean show) {
this.id = id;
this.image = Imgcodecs.imread(image);
this.resize_ratio = resize_ratio;
this.num_x = num_x;
this.num_y = num_y;
this.num_z = num_z;
this.show = show;
this.side = new HashMap<>();
//each camera can view 3 side
side.put("front", null);
side.put("side", null);
side.put("bottom", null);
//resize image if resize_ratio !=0
if (resize_ratio != 0) {
Imgproc.resize(this.image, this.image, new Size(0, 0), resize_ratio, resize_ratio);
}
//save image if require
if (show) {
Imgcodecs.imwrite("cam_" + id + ".png", this.image);
}
}
public void add_side(String side_name, float[][] side_loc) {
assert (Arrays.asList(new String[]{"front", "side", "bottom"}).contains(side_name)) : "Wrong side_name";
//get side_x, side_y
int side_x = 0;
int side_y = 0;
switch (side_name) {
case "front": {
side_x = num_z;
side_y = num_x;
break;
}
case "side": {
side_x = num_z;
side_y = num_y;
break;
}
case "bottom": {
side_x = num_x;
side_y = num_y;
break;
}
}
//get side
if (side_loc == null) {
side.put(side_name, new intArray(side_x, side_y));
} else {
Fleet_single_side sideProcess = new Fleet_single_side(image, side_name, side_loc, side_x, side_y, "Yellow", false);
side.put(side_name, new intArray(sideProcess.process()));
}
}
private int[][] fill_matrix(int[][] matrix) {
// Change 0 to 1
// [[1 0 0 1 1] [[1 1 1 1 1]
// [1 1 1 1 1] == [1 1 1 1 1]
// [1 1 1 0 1] == [1 1 1 0 1]
// [0 0 0 0 0]] [0 0 0 0 0]]
int h = matrix.length;
int w = matrix[0].length;
for (int i = h - 1; i > 0; i--) {
if (w == Arrays.stream(matrix[i]).sum()) {
for (int j = 0; j < i; j++) {
Arrays.fill(matrix[j], 1);
}
}
}
return matrix;
}
public void refine_matrix() {
//get front, side, bottom
int[][] front_ = this.side.get("front").getIntArray();
int[][] side_ = this.side.get("side").getIntArray();
int[][] bottom_ = this.side.get("bottom").getIntArray();
int[][] side = padding(side_, num_z, num_y);
int[][] front = padding(front_, num_z, num_x);
int[][] bottom = padding(bottom_, num_x, num_y);
this.side.get("front").setIntArray(fill_matrix(front));
this.side.get("side").setIntArray(fill_matrix(side));
this.side.get("bottom").setIntArray(fill_matrix(bottom));
}
private int[][] padding(int[][] matrix, int require_height, int require_width) {
//get width, height
int height = 0;
int width = 0;
if (matrix != null) {
height = matrix.length;
if (matrix[0] != null) {
width = matrix[0].length;
}
}
//get padding size
if (height > require_height) {
require_height = height;
}
if (width > require_width) {
require_width = width;
}
int[][] return_matrix = new int[require_height][require_width];
//copy matrix
for (int i = 0; i < require_height; i++) {
//padding
Arrays.fill(return_matrix[i], 0);
//copy matrix
if (i < height) {
System.arraycopy(matrix[i], 0, return_matrix[i], 0, width);
}
}
return return_matrix;
}
}
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.util.Arrays;
import java.util.Comparator;
public class Fleet_single_side {
private Mat image;
private String side_name;
private float[][] side_loc; //4-point location on image
private int num_x; //number of fleet on height
private int num_y; //number of fleet on width
private String color; // color of fleet
private boolean show;
private Scalar lower_color; // lower color of fleet
private Scalar upper_color; // upper color of fleet
public Fleet_single_side(Mat image, String side_name, float[][] side_loc, int num_x, int num_y, String color, boolean show) {
this.image = image;
this.side_name = side_name;
this.side_loc = side_loc;
this.num_x = num_x;
this.num_y = num_y;
this.color = color;
this.show = show;
//initialize lower, u
if (color == "Yellow") {
this.lower_color = new Scalar(20, 100, 100);
this.upper_color = new Scalar(30, 255, 255);
}
}
public Mat four_point_transform() {
//get the order coordinates
float[][] rect = order_points(this.side_loc);
float[] tl = rect[0];
float[] tr = rect[1];
float[] br = rect[2];
float[] bl = rect[3];
// compute the width of the new image, which will be the
// maximum distance between bottom-right and bottom-left
// x-coordiates or the top-right and top-left x-coordinates
double widthA = Math.sqrt(Math.pow((br[0] - bl[0]), 2) + Math.pow((br[1] - bl[1]), 2));
double widthB = Math.sqrt(Math.pow((tr[0] - tl[0]), 2) + Math.pow((tr[1] - tl[1]), 2));
int maxWidth = Math.max((int) widthA, (int) widthB);
// compute the height of the new image, which will be the
// maximum distance between the top-right and bottom-right
// y-coordinates or the top-left and bottom-left y-coordinates
double heightA = Math.sqrt(Math.pow((tr[0] - br[0]), 2) + Math.pow((tr[1] - br[1]), 2));
double heightB = Math.sqrt(Math.pow((tl[0] - bl[0]), 2) + Math.pow((tl[1] - bl[1]), 2));
int maxHeight = Math.max((int) heightA, (int) heightB);
// now that we have the dimensions of the new image, construct
// the set of destination points to obtain a "birds eye view",
// (i.e. top-down view) of the image, again specifying points
// in the top-left, top-right, bottom-right, and bottom-left
// order
MatOfPoint2f dst = new MatOfPoint2f(
new Point(0, 0),
new Point(maxWidth - 1, 0),
new Point(maxWidth - 1, maxHeight - 1),
new Point(0, maxHeight - 1)
);
//gen src from rect
MatOfPoint2f src = new MatOfPoint2f(
new Point(rect[0][0], rect[0][1]),
new Point(rect[1][0], rect[1][1]),
new Point(rect[2][0], rect[2][1]),
new Point(rect[3][0], rect[3][1])
);
//compute the perspective transform matrix and then apply it
Mat M = Imgproc.getPerspectiveTransform(src, dst);
Mat warped = new Mat();
Imgproc.warpPerspective(this.image, warped, M, new Size(maxWidth, maxHeight));
//return the warped image
return warped;
}
public Mat[] filter_color(Mat image) {
//convert image from bgr to hsv
Mat hsv = new Mat();
Imgproc.cvtColor(image, hsv, Imgproc.COLOR_BGR2HSV);
//gen mask from hsv, lower color, upper color
Mat mask = new Mat();
Core.inRange(hsv, this.lower_color, this.upper_color, mask);
//gen bitwise_and from image using mask
Mat result = new Mat();
Core.bitwise_and(image, image, result, mask);
//return result, mask
return new Mat[]{result, mask};
}
public int[][] image2mat(Mat image) {
int[] shape = new int[]{num_x, num_y};
//initialize mat
int[][] mat = new int[num_x][num_y];
//get h, w
int w = image.width();
int h = image.height();
//cal stride_w, stride_h
int stride_w = w / shape[1];
int stride_h = h / shape[0];
//create mat
for (int i = 0; i < shape[0]; i++) {
for (int j = 0; j < shape[1]; j++) {
//cal mat[i][j]
double sum = 0;
for (int x = i * stride_h; x < (i + 1) * stride_h; x++) {
for (int y = j * stride_w; y < (j + 1) * stride_w; y++) {
sum += Arrays.stream(image.get(x, y)).sum();
}
}
mat[i][j] = (int) Math.floor(sum / 255);
//draw circle if require show
if (this.show) {
Imgproc.circle(image, new Point(j * stride_w, i * stride_h), 1, new Scalar(255), 1);
}
}
}
//save image is require
if (this.show) {
Imgcodecs.imwrite(this.side_name + ".png", image);
}
//cal mean
double mean = Arrays.stream(mat).flatMapToInt(Arrays::stream).sum() / (num_x * num_y);
//cal std
double std = cal_std(mat, mean);
//map condition
for (int i = 0; i < num_x; i++) {
for (int j = 0; j < num_y; j++) {
if ((mat[i][j] >= (mean - 0.5 * std)) && (mat[i][j] <= (mean + 3.0 * std)) && (mat[i][j]>stride_h*stride_w/16)) {
mat[i][j] = 1;
} else {
mat[i][j] = 0;
}
}
}
return mat;
}
public int[][] process() {
Mat wrap_side = four_point_transform();
Mat[] result_and_mask = filter_color(wrap_side);
return image2mat(result_and_mask[1]);
}
private double cal_std(int[][] mat, double avg) {
double mean = 0;
double size = 0;
//cal mean = sum((value-avg)^2)
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
mean += Math.pow(mat[i][j] - avg, 2);
size++;
}
}
return Math.sqrt(mean / size);
}
private float[][] order_points(float[][] pts) {
//initialize empty rect
float[][] rect = new float[4][];
float[][] xSorted = pts.clone();
float[][] ySorted = pts.clone();
//sort the points base on their x_coordinates
Arrays.sort(xSorted, new Comparator<float[]>() {
@Override
public int compare(float[] point1, float[] point2) {
return Float.compare(point1[0], point2[0]);
}
});
//sort the points base on their y_coordinates
Arrays.sort(ySorted, new Comparator<float[]>() {
@Override
public int compare(float[] point1, float[] point2) {
return Float.compare(point1[1], point2[1]);
}
});
//grab the left-most and right-most points from the sorted x-roodinate points
float[][] leftMost = new float[][]{xSorted[0], xSorted[1]};
//grab the left-most from the sorted y-roodinate points
float[] topMost = ySorted[0];
//get the clockwise sort
float[][] clockwise_pts = sort_clockwise(pts);
int idx = 0;
//find index of topMost
for (int i=0;i<clockwise_pts.length;i++){
if (Arrays.equals(clockwise_pts[i], topMost)){
idx=i;
break;
}
}
//sort topMost is the first element
float[][] order_pts = new float[4][2];
if (idx!=0){
System.arraycopy(clockwise_pts, idx, order_pts, 0, clockwise_pts.length-idx);
System.arraycopy(clockwise_pts, 0, order_pts, clockwise_pts.length-idx, idx);
}else{
order_pts = clockwise_pts;
}
// topMost is top-left
if (Arrays.stream(leftMost).anyMatch(point -> {
return Arrays.equals(point, topMost);
})){
return order_pts;
}else{//topMost is top-right
float[] temp = order_pts[order_pts.length-1];
System.arraycopy(order_pts,0 , order_pts, 1, order_pts.length-1);
order_pts[0] = temp;
return order_pts;
}
}
private float[][] sort_clockwise(float[][] pts){
float[][] points = pts.clone();
float sum_x = 0;
float sum_y = 0;
//cal sum x, sum y
for (int i=0;i<points.length;i++){
sum_x+=points[i][0];
sum_y+=points[i][1];
}
final float centre_x = sum_x / points.length;
final float centre_y = sum_y / points.length;
//sort by angle;
Arrays.sort(points, new Comparator<float[]>() {
@Override
public int compare(float[] floats1, float[] floats2) {
return Double.compare(Math.atan2(floats1[1]-centre_y, floats1[0]-centre_x)
, Math.atan2(floats2[1]-centre_y, floats2[0]-centre_x) );
}
});
return points;
}
}
import org.bytedeco.javacpp.Loader;
import org.bytedeco.opencv.opencv_java;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.Arrays;
import java.util.List;
public class main {
public static void main(String[] args){
Loader.load(opencv_java.class);
//create fleet cam
Fleet_single_cam cam = new Fleet_single_cam("a","5b8ab6b219c9d6978fd827.jpg", 0.5, 5, 5, 4,false);
//create side_loc
Point[] side_loc = new Point[4];
//create bottom side
cam.add_side("front", new float[][]{
new float[]{687, 8},
new float[]{1264, 54},
new float[]{1027, 432},
new float[]{672, 305}
});
cam.add_side("side", new float[][]{
new float[]{12, 132},
new float[]{687, 8},
new float[]{672, 305},
new float[]{275, 497}
});
cam.add_side("bottom", new float[][]{
new float[]{672, 305},
new float[]{1027, 432},
new float[]{817, 923},
new float[]{275, 497}
});
cam.refine_matrix();
//print matrix
int[][] matrix = cam.side.get("front").getIntArray();
for (int i=0;i<matrix.length;i++){
for (int j=0; j<matrix[0].length;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
System.out.println();
int[][] matrix2 = cam.side.get("side").getIntArray();
for (int i=0;i<matrix2.length;i++){
for (int j=0; j<matrix2[0].length;j++){
System.out.print(matrix2[i][j]+" ");
}
System.out.println();
}
System.out.println();
int[][] matrix3 = cam.side.get("bottom").getIntArray();
for (int i=0;i<matrix2.length;i++){
for (int j=0; j<matrix3[0].length;j++){
System.out.print(matrix3[i][j]+" ");
}
System.out.println();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment