Skip to content

Commit

Permalink
Finished lab1
Browse files Browse the repository at this point in the history
  • Loading branch information
danilchican committed Sep 27, 2017
1 parent 26e7d75 commit badf94d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 41 deletions.
32 changes: 19 additions & 13 deletions laba1/src/main/java/com/bsuir/dip/action/TranslationAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bsuir.dip.image.Image;
import com.bsuir.dip.image.ImageConverter;
import com.bsuir.dip.index.Main;

public class TranslationAction extends Action {

Expand All @@ -10,38 +11,43 @@ public void executeGS() {

if (!isEmptyImage) {
if (!image.isGrayScale()) {
image = ImageConverter.convertToGS(image);
Main.window.setLastImage(ImageConverter.convertToGS(new Image(image.getImg().clone())));
}

image.show();
Main.window.replaceImage();
}
}

public void executePreparing() {
public void executePreparing(final int leftThreshold, final int rightThreshold) {
System.out.println("Execute preparing.");

if (!isEmptyImage) {
// TODO Change
image = ImageConverter.convertToGS(image);
image.show();
Main.window.setLastImage(new Image(image.getImg().clone()));
Main.window.getLastImage().execPreparing(leftThreshold, rightThreshold);

Main.window.replaceImage();
}
}

public void executeBinPreparing() {
public void executeBinPreparing(int threshold) {
System.out.println("Execute preparing.");

if (!isEmptyImage) {
// TODO Change
image = ImageConverter.convertToGS(image);
image.show();
Main.window.setLastImage(new Image(image.getImg().clone()));
Main.window.getLastImage().execBinPreparing(threshold);

Main.window.replaceImage();
}
}

public void executeSobiel() {
System.out.println("Execute Sobiel.");

Image img = new Image(image.getImg().clone());
img.execSobel();
img.show();
if (!isEmptyImage) {
Main.window.setLastImage(new Image(image.getImg().clone()));
Main.window.getLastImage().execSobel();

Main.window.replaceImage();
}
}
}
136 changes: 127 additions & 9 deletions laba1/src/main/java/com/bsuir/dip/drawing/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.converter.NumberStringConverter;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import java.io.ByteArrayInputStream;
import java.io.File;

public class Window {

final private Stage stage;

private static final String WINDOW_TITLE = "DIP Lab1";

private static final String OPEN_FILE_BTN_TITLE = "Choose image...";
private static final String SAVE_FILE_BTN_TITLE = "Save image";
private static final String EXECUTE_BTN_TITLE = "Execute";
private static final String SHOW_IMAGE_BTN_TITLE = "Show in Window";

private static final String FILE_PREFIX = "file:///";
private static final String DEFAULT_BG_IMAGE_PATH = "image_bg.jpg";

Expand All @@ -38,25 +47,36 @@ public class Window {
private static final int LAYOUT_PADDING = 10;
private static final int MAX_PREVIEW_WIDTH = 300;

private final SplitPane mainLayout;

private final VBox leftBar;
private final VBox rightBar;
private final SplitPane mainLayout;

private final FileChooser fileChooser;

private final Button openFileBtn;
private final Button saveFileBtn;
private final Button execTranslateBtn;
private final Button showImageBtn;

private final ImageView imageView;

private final Label optionsLabel;
private final Label histogramLabel;
private final Label translateLabel;

private Label leftThresholdLabel;
private Label rightThresholdLabel;

private TextField leftLineThreshold;
private TextField rightLineThreshold;

private final ComboBox optionsBox;
private final ComboBox histogramsBox;
private final ComboBox translationsBox;

private Image image;
private Image lastImage;

public Window(Stage stage) {
this.stage = stage;
Expand All @@ -71,6 +91,11 @@ public Window(Stage stage) {

openFileBtn = new Button(OPEN_FILE_BTN_TITLE);
saveFileBtn = new Button(SAVE_FILE_BTN_TITLE);
execTranslateBtn = new Button(EXECUTE_BTN_TITLE);
showImageBtn = new Button(SHOW_IMAGE_BTN_TITLE);

leftLineThreshold = new TextField();
rightLineThreshold = new TextField();

leftBar = new VBox();
rightBar = new VBox();
Expand Down Expand Up @@ -105,9 +130,26 @@ public void show() {
}

private void fillScene() {
optionsBox.setDisable(true);
histogramsBox.setDisable(true);
translationsBox.setDisable(true);
saveFileBtn.setDisable(true);
showImageBtn.setDisable(true);

leftLineThreshold.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
rightLineThreshold.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));

leftLineThreshold.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*")) {
leftLineThreshold.setText(newValue.replaceAll("[^\\d]", ""));
}
});

rightLineThreshold.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*")) {
rightLineThreshold.setText(newValue.replaceAll("[^\\d]", ""));
}
});

mainLayout.setOrientation(Orientation.HORIZONTAL);
mainLayout.setDividerPositions(0.5);
Expand All @@ -126,7 +168,7 @@ private void fillScene() {
imageView.setImage(image);
imageView.setPreserveRatio(true);

rightBar.getChildren().addAll(imageView, openFileBtn, saveFileBtn);
rightBar.getChildren().addAll(imageView, openFileBtn, saveFileBtn, showImageBtn);
mainLayout.getItems().addAll(leftBar, rightBar);
}

Expand All @@ -138,33 +180,53 @@ public void setImage(Image image) {
this.image = image;
}

public void setLastImage(Image lastImage) {
this.lastImage = lastImage;
}

public Image getLastImage() {
return lastImage;
}

public Button getShowImageBtn() {
return showImageBtn;
}

private void setActions() {
openFileBtn.setOnAction(event -> {
File file = fileChooser.showOpenDialog(stage);

if (file != null && file.exists()) {
javafx.scene.image.Image displayingImage = new javafx.scene.image.Image(FILE_PREFIX + file.getAbsolutePath());
this.image = new Image(ImageLoader.load(file.getAbsolutePath()));
Mat pixs = ImageLoader.load(file.getAbsolutePath());

this.lastImage = new Image(pixs);
this.image = new Image(pixs.clone());

if (displayingImage.getWidth() > MAX_PREVIEW_WIDTH) {
imageView.setFitWidth(MAX_PREVIEW_WIDTH);
}

imageView.setImage(displayingImage);
optionsBox.setDisable(false);
saveFileBtn.setDisable(false);
showImageBtn.setDisable(false);
}
});

showImageBtn.setOnAction(event -> {
getLastImage().show();
});

saveFileBtn.setOnAction(event -> {
saveFileBtn.setDisable(true);
image = lastImage;

if (image == null) {
new Dialog(
Alert.AlertType.ERROR, "Error",
null, "Image is not selected."
).show();

saveFileBtn.setDisable(false);
return;
}

Expand All @@ -183,8 +245,6 @@ private void setActions() {
null, "Image has been saved."
).show();
}

saveFileBtn.setDisable(false);
});

optionsBox
Expand All @@ -197,6 +257,8 @@ private void setActions() {

this.handleByOption(option);
}

clearTranslationsObject();
});

histogramsBox
Expand All @@ -209,6 +271,8 @@ private void setActions() {

this.handleByHistogramItem(item);
}

clearTranslationsObject();
});

translationsBox
Expand All @@ -224,6 +288,21 @@ private void setActions() {
});
}

public void replaceImage() {
if(getLastImage() != null && getImage() != getLastImage()) {
MatOfByte byteMat = new MatOfByte();
Imgcodecs.imencode(".jpg", getLastImage().getImg(), byteMat);
javafx.scene.image.Image displayingImage = new javafx.scene.image.Image(new ByteArrayInputStream(byteMat.toArray()));

if (displayingImage.getWidth() > MAX_PREVIEW_WIDTH) {
imageView.setFitWidth(MAX_PREVIEW_WIDTH);
}

imageView.setImage(displayingImage);
System.out.println("Image replaced.");
}
}

private void handleByOption(Option option) {
System.out.println("Selected option: " + option.getTitle());

Expand Down Expand Up @@ -286,21 +365,60 @@ private void handleByTranslateItem(Translation item) {

switch (item) {
case EMPTY:
clearTranslationsObject();
break;
case GRAYSCALE:
clearTranslationsObject();
action.executeGS();
break;
case BIN_PREPARING:
action.executeBinPreparing();
clearTranslationsObject();
leftThresholdLabel = new Label("Threshold:");
leftBar.getChildren().addAll(leftThresholdLabel, leftLineThreshold, execTranslateBtn);

actionBinPrepExecute();
break;
case PREPARING:
action.executePreparing();
clearTranslationsObject();
leftThresholdLabel = new Label("Left threshold:");
rightThresholdLabel = new Label("Right threshold:");

leftBar.getChildren().addAll(leftThresholdLabel, leftLineThreshold,
rightThresholdLabel, rightLineThreshold, execTranslateBtn);
actionPrepExecute();
break;
case SOBIEL:
clearTranslationsObject();
action.executeSobiel();
break;
default:
throw new IllegalArgumentException("Translation item was not found");
}
}

private void clearTranslationsObject() {
leftBar.getChildren().removeAll(leftThresholdLabel, leftLineThreshold);
leftBar.getChildren().removeAll(rightThresholdLabel, rightLineThreshold, execTranslateBtn);
}

private void actionBinPrepExecute() {
execTranslateBtn.setOnAction(event -> {
TranslationAction action = new TranslationAction();
int value = Integer.parseInt(leftLineThreshold.getText());

action.execute();
action.executeBinPreparing(value);
});
}

private void actionPrepExecute() {
execTranslateBtn.setOnAction(event -> {
TranslationAction action = new TranslationAction();
int left = Integer.parseInt(leftLineThreshold.getText());
int right = Integer.parseInt(rightLineThreshold.getText());

action.execute();
action.executePreparing(left, right);
});
}
}
18 changes: 0 additions & 18 deletions laba1/src/main/java/com/bsuir/dip/index/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,5 @@ public static void main(String[] args) {
public void start(Stage stage) {
window = new Window(stage);
window.show();

//Image image = new Image(ImageLoader.load("D:/test.png"));

//image.execBinPreparing(130);
//image.execPreparing(80, 130);

//Image gs = ImageConverter.convertToGS(image);

//gs.execSobel();
//gs.show();
//image.showHistogram(Channel.ALL);

// Image gs = ImageConverter.convertToGS(image);
// gs.execBinPreparing(50);
// gs.show();
// gs.showHistogram(Channel.ALL);
//
// ImageLoader.save("D:/gs.jpg", gs);
}
}
2 changes: 1 addition & 1 deletion laba1/src/main/java/com/bsuir/dip/type/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;

public enum Option {
VIEW_IMAGE(0, "View Image", null),
VIEW_IMAGE(0, "", null),
VIEW_HIST(1, "View Histogram", new HistogramAction()),
TRANSLATION(2, "Translate Image", new TranslationAction());

Expand Down

0 comments on commit badf94d

Please sign in to comment.