Вы находитесь на странице: 1из 5

package MorseDetection;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Stack;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Segmentasi extends Cleaning {

public int startX, startY;


public Point startPoint;
protected ArrayList<Point> p1;
protected ArrayList<Point> p2;
public boolean mark[][];
public int t[] = new int[4];
public int x, y;

public Segmentasi(String path) throws IOException {


super(path);
super.Tinning();
mark = new boolean[w][h];
startPoint = new Point();
p1 = new ArrayList<>();
p2 = new ArrayList<>();
}

// public void setStartPoint(int x, int y) {


// startPoint.setLocation(x, y);
// }

public boolean checkBounds(int j, int i) {


return (j >= 0 && j < w) && (i >= 0 && i < h);
}

public boolean checkColor(int j, int i) {


return new Color(image.getRGB(j, i)).getRGB() == Color.BLACK.getRGB();
}

public boolean isNotVisited(int j, int i) {


return mark[j][i] == false;
}

public boolean isReadyToRun(int j, int i) {


if (checkBounds(j, i)) {
return checkColor(j, i) && isNotVisited(j, i);
} else {
return false;
}
}

public void reset() {


for (int i = 0; i < t.length; i++) {
t[i] = 0;
}
}

public String flood(int j, int i) { // ini kordinat hitam pertama pas dapet
String code = "";
Stack<Point> currNode = new Stack<>();
currNode.push(new Point(j, i));
while (!currNode.isEmpty()) {
Point curr = currNode.peek();
int x = (int) curr.getX();
int y = (int) curr.getY();
if (x < t[0]) {
t[0] = x;
}
if (y < t[1]) {
t[1] = y;
}
if (x > t[2]) {
t[2] = x;
}
if (y > t[3]) {
t[3] = y;
}
mark[x][y] = true;
if (isReadyToRun(x + 1, y - 1)) {
code += "1";
currNode.push(new Point(x + 1, y - 1));
} else if (isReadyToRun(x + 1, y)) {
code += "2";
currNode.push(new Point(x + 1, y));
} else if (isReadyToRun(x + 1, y + 1)) {
code += "3";
currNode.push(new Point(x + 1, y + 1));
} else if (isReadyToRun(x, y + 1)) {
code += "4";
currNode.push(new Point(x, y + 1));
} else if (isReadyToRun(x - 1, y + 1)) {
code += "5";
currNode.push(new Point(x - 1, y + 1));
} else if (isReadyToRun(x - 1, y)) {
code += "6";
currNode.push(new Point(x - 1, y));
} else if (isReadyToRun(x - 1, y - 1)) {
code += "7";
currNode.push(new Point(x - 1, y - 1));
} else if (isReadyToRun(x, y - 1)) {
code += "8";
currNode.push(new Point(x, y - 1));
} else {
currNode.pop();
}
}
return code;
}

public void sorting_(ArrayList<Point> data) {


for (int i = 0; i < data.size() - 1; i++) {
for (int j = i + 1; j < data.size(); j++) {
if (data.get(i).getY() > data.get(j).getY()) {
Point temp = data.get(i);
data.set(i, data.get(j));
data.set(j, temp);
}
}
}
}

public void sorting(ArrayList<ObjectCode> data) {


for (int i = 0; i < data.size() - 1; i++) {
for (int j = i + 1; j < data.size(); j++) {
if (data.get(i).getPoint().getX() > data.get(j).getPoint().getX())
{
ObjectCode temp = data.get(i);
data.set(i, data.get(j));
data.set(j, temp);
}
}
}
}

// CREATING KNN ALGORITHM


public int clasifyObject(String code) throws FileNotFoundException {
String dataSet[] =
getText("/home/emiq/NetBeansProjects/MorseDetection/src/MorseDetection/DataSet/zAll
.txt").split("\n");
int bin_Code[] = new int[9];
for (int i = 0; i < code.length(); i++) {
int x = Integer.parseInt(code.charAt(i) + "");
bin_Code[x]++;
}
ArrayList<Point> List_value_code = new ArrayList<>();
//kordinat x adalah label chaincode pada index[0]
//kordinat y adalah hasil euclidean distance bincode dengan bincurrentcode
for (String dataList_code : dataSet) {
String temp[] = dataList_code.split(" ");
int bin_Currentcode[] = new int[9];
//input data dari notepad ke dalam bin ARRAY
for (int i = 0; i < temp.length; i++) {
bin_Currentcode[i] = Integer.parseInt(temp[i]);
}
//mencari kedekatan KNN start here
int sum = 0;
for (int i = 1; i < bin_Currentcode.length; i++) {
sum += Math.abs(bin_Code[i] - bin_Currentcode[i]);
}

//adding sum diatas kedalam LIST. kemudian di sorting setelahnya


List_value_code.add(new Point(bin_Currentcode[0], sum));
}

//***=== nilai k ===***


int k = 7;
sorting_(List_value_code);
int dash, dot, slash;
dash = dot = slash = 0;
for (int i = 0; i < k; i++) {
int x = (int) List_value_code.get(i).getX();
if (x == 1) {
dash++;
} else if (x == 2) {
slash++;
} else if (x == 0) {
dot++;
}
}
int ret = 0;
int result = dot;
if (result < dash) {
result = dash;
ret = 1;
}
if (result < slash) {
result = slash;
ret = 2;
}
return ret;
}

public String getText(String Path) throws FileNotFoundException {


BufferedReader doc = new BufferedReader(new FileReader(Path));
String ret = "";
try {
String temp;
while ((temp = doc.readLine()) != null) {
ret += temp + "\n";
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
protected ArrayList<ObjectCode> listCode = new ArrayList<>();

public void enterframeSegmented() throws FileNotFoundException {


for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (new Color(image.getRGB(j, i)).getRGB() == Color.WHITE.getRGB())
{
mark[j][i] = true;
} else {
if (mark[j][i] == true) {
//do nothing
} else {
mark[j][i] = true;
t[0] = j;
t[1] = i;
String code = flood(j, i);
ObjectCode thing = new ObjectCode(new Point(j, i), code);
listCode.add(thing);
reset();
}
}
}
}
sorting__(listCode);
}
public void sorting__(ArrayList<ObjectCode> data) {
for (int i = 0; i < data.size() - 1; i++) {
for (int j = i + 1; j < data.size(); j++) {
if (data.get(i).getPoint().getX() > data.get(j).getPoint().getX())
{
ObjectCode temp = data.get(i);
data.set(i, data.get(j));
data.set(j, temp);
}

}
}

public void print() {


for (ObjectCode objectCode : listCode) {
System.out.println(objectCode.getCode());

}
}
}

Вам также может понравиться