Skip to content

Commit

Permalink
gs-cv: MeshGrid: Use ints for widths/heights in dewarp2
Browse files Browse the repository at this point in the history
  • Loading branch information
fducroquet committed Apr 3, 2018
1 parent da1f29b commit 6791743
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions gs-cv/src/main/java/org/genericsystem/cv/application/MeshGrid.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.genericsystem.cv.application;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -95,7 +94,7 @@ public Mat dewarp2() {
Map<Key, Point3[]> mesh3D = toPoint3d();

// Average width of the 3D edges for each column.
double[] widths = new double[2 * (int) kSize.width + 1];
int[] widths = new int[2 * (int) kSize.width + 1];
for (int j = 0; j < widths.length; j++) {
double sum = 0;
for (int i = (int) -kSize.height; i <= kSize.height; i++) {
Expand All @@ -105,11 +104,11 @@ public Mat dewarp2() {
// Last line, bottom edge.
Point3[] para = mesh3D.get(new Key((int) kSize.height, j - (int) kSize.width));
sum += euclideanDistance(para[2], para[3]);
widths[j] = sum / (2 * kSize.height + 2);
widths[j] = (int) Math.round(sum / (2 * kSize.height + 2));
}

// Averaghe height of the 3D edges for each line.
double[] heights = new double[2 * (int) kSize.height + 1];
// Average height of the 3D edges for each line.
int[] heights = new int[2 * (int) kSize.height + 1];
for (int i = 0; i < heights.length; i++) {
double sum = 0;
for (int j = (int) -kSize.width; j <= kSize.width; j++) {
Expand All @@ -119,14 +118,13 @@ public Mat dewarp2() {
// Last column, right edge.
Point3[] para = mesh3D.get(new Key(i - (int) kSize.height, (int) kSize.width));
sum += euclideanDistance(para[1], para[2]);
heights[i] = sum / (2 * kSize.width + 2);
heights[i] = (int) Math.round(sum / (2 * kSize.width + 2));
}

// Rescaling ratio.
double totalHeight = sum(heights, heights.length);
double textSep = 20;
double ratio = (totalHeight / heights.length) / textSep;
System.out.println("ratio " + ratio);
int totalHeight = sum(heights, heights.length);
int textSep = 20;
double ratio = ((double) totalHeight / heights.length) / textSep;

for (int i = 0; i < widths.length; i++)
widths[i] /= ratio;
Expand All @@ -135,25 +133,24 @@ public Mat dewarp2() {
heights[i] /= ratio;

// New sizes
double totalWidth = sum(widths, widths.length);
int totalWidth = sum(widths, widths.length);
totalHeight = sum(heights, heights.length);

double rectHeight = totalHeight / heights.length;
int rectHeight = totalHeight / heights.length;

Mat dewarpedImage = new Mat((int) totalHeight + 1, (int) totalWidth + 1, CvType.CV_8UC3, new Scalar(255, 255, 255));
logger.info("Column widths: {}", Arrays.toString(widths));
Mat dewarpedImage = new Mat(totalHeight + 1, totalWidth + 1, CvType.CV_8UC3, new Scalar(255, 255, 255));

for (int i = (int) -kSize.height; i <= kSize.height; i++) {
double currX = 0;
int currX = 0;
for (int j = (int) -kSize.width; j <= kSize.width; j++) {
int wJ = j + (int) kSize.width;
if (wJ > 0)
currX += widths[wJ - 1];
if (inImageBorders(mesh.get(new Key(i, j)))) {
double rectWidth = widths[wJ];
int rectWidth = widths[wJ];
Rect subImageRect = subImageRect(i, j);
double x = currX;
double y = (i + (int) kSize.height) * rectHeight;
int x = currX;
int y = (i + (int) kSize.height) * rectHeight;
Mat homography = dewarpPolygon(mesh.get(new Key(i, j)), subImageRect, rectHeight, rectWidth);
// logger.info("i {}, j {}, x {}, y {}, width {}", i, j, x, y, rectWidth);
if ((x + rectWidth) <= dewarpedImage.width() && (y + rectHeight) <= dewarpedImage.height()) {
Expand All @@ -173,8 +170,8 @@ public Mat dewarp2() {
return dewarpedImage;
}

private double sum(double[] array, int end) {
double sum = 0;
private int sum(int[] array, int end) {
int sum = 0;
for (int i = 0; i < end; i++)
sum += array[i];
return sum;
Expand Down

0 comments on commit 6791743

Please sign in to comment.