Skip to content

Commit

Permalink
add cropping and runner of datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkBurov committed Mar 31, 2018
1 parent db5f021 commit c41dbde
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
Binary file removed analyzed.jpg
Binary file not shown.
36 changes: 36 additions & 0 deletions src/main/java/io/opencv/first/matrixanalysis/ImagesChecker.java
@@ -0,0 +1,36 @@
package io.opencv.first.matrixanalysis;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class ImagesChecker {

private static final String[] IMG_EXTENSIONS = new String[]{".jpg", ".bmp", ".jpeg", ".png"};

public static void main(String[] args) throws IOException {

// List<File> images = Files.walk(Paths.get("D:\\Downloads\\random photos"))
List<File> images = Files.walk(Paths.get("D:\\Downloads\\cars\\cars"))
.filter(Files::isRegularFile)
.map(Path::toFile)
.filter(file -> isImage(file.getName()))
.collect(Collectors.toList());

images.parallelStream()
.forEach(file -> SamePixelColorMatricsFinder.handleFile(file, 20, 0.75));
}

private static boolean isImage(String fileName) {
for (String imgExtension : IMG_EXTENSIONS) {
if (fileName.endsWith(imgExtension)) {
return true;
}
}
return false;
}
}
Expand Up @@ -2,8 +2,10 @@

import io.opencv.OpenCvBased;
import io.opencv.util.Matrices;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
Expand All @@ -17,9 +19,10 @@
import java.util.List;
import java.util.Map;

@Slf4j
public class SamePixelColorMatricsFinder extends OpenCvBased {

private static final int MATRIX_SIZE = 30;
private static final int MATRIX_SIZE = 20;

public static void main(String[] args) throws IOException {
// File file = new ClassPathResource("images/IMG_1151_photoshoped.jpg").getFile();
Expand All @@ -28,16 +31,19 @@ public static void main(String[] args) throws IOException {
// File file = new ClassPathResource("images/14.jpg").getFile();
// File file = new ClassPathResource("images/d7fc888s-960.jpg").getFile();
// File file = new ClassPathResource("images/IMG_1151.jpg").getFile();
File file = new ClassPathResource("images/spacex_photoshoped.jpg").getFile();
// File file = new ClassPathResource("images/spacex_photoshoped.jpg").getFile();
File file = new ClassPathResource("images/carsgraz_001.bmp").getFile();
// File file = new ClassPathResource("images/spacex.jpg").getFile();

handleFile(file.getAbsolutePath(), MATRIX_SIZE);
handleFile(file, MATRIX_SIZE, 0.75);
}

public static void handleFile(String absolutePath, int matrixSize) {
public static void handleFile(File file, int matrixSize, double percentToSaveWhenCroping) {
log.info("Starting analyzing of " + file.getAbsolutePath());
try (Matrices matrices = new Matrices()) {
Mat orig = matrices.fromSupplier("orig", () -> Imgcodecs.imread(absolutePath));
Mat dst = matrices.fromSupplier("new", orig::clone);
Mat orig = matrices.fromSupplier("orig", () -> Imgcodecs.imread(file.getAbsolutePath()));
Mat dst = cropImage(matrices, orig, percentToSaveWhenCroping);
//Mat dst = matrices.fromSupplier("new", orig::clone);

List<Map.Entry<Integer, Integer>> rowColumns = new ArrayList<>();

Expand All @@ -51,14 +57,37 @@ public static void handleFile(String absolutePath, int matrixSize) {
}
}

// rectangle the similar matrices and update the image
for (Map.Entry<Integer, Integer> rowColumn : rowColumns) {
Point startingPoing = new Point(rowColumn.getValue() - 1, rowColumn.getKey() - 1);
Point endingPoint = new Point(rowColumn.getValue() - 1 + matrixSize, rowColumn.getKey() - 1 + matrixSize);
Imgproc.rectangle(dst, startingPoing, endingPoint, new Scalar(500));
}

Imgcodecs.imwrite("analyzed.jpg", dst);
if (!rowColumns.isEmpty()) {
Imgcodecs.imwrite(file.getName() + "_analyzed.jpg", dst);
}
}
log.info("Ended analyzing of " + file.getAbsolutePath());
}

// currently crop only the upper bound
private static Mat cropImage(Matrices matrices, Mat source, double percentToSave) {
//trying to crop from center
/*int x_center = source.cols() / 2;
int y_center = source.rows() / 2;
Double width = source.cols() * percentToSave / 2;
Double height = source.rows() * percentToSave / 2;
int x = x_center - width.intValue();
int y = y_center - height.intValue();*/
Double width = (double) source.cols();
Double height = source.rows() * percentToSave;
int x = 0;
int y = source.rows() - height.intValue();

Rect rectCrop = new Rect(x, y, width.intValue(), height.intValue());
return matrices.fromSupplier("croped", () -> new Mat(source, rectCrop));
}

private static Boolean isMatrixOfSameColor(Mat image, int row, int column, int matrixSize) {
Expand Down

0 comments on commit c41dbde

Please sign in to comment.