Skip to content
This repository was archived by the owner on Dec 7, 2020. It is now read-only.

Commit 2779e47

Browse files
author
Severi Haverila
committed
refactoring and debugging crop error
1 parent 253beec commit 2779e47

File tree

7 files changed

+172
-74
lines changed

7 files changed

+172
-74
lines changed

queryimages/test.png

68 KB
Loading

src/main/java/TestdroidImageRecognition.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import library.AkazeImageFinder;
21
import library.ImageRecognition;
32

4-
import org.apache.commons.io.FileUtils;
53
import org.opencv.core.Point;
64
import org.openqa.selenium.Dimension;
7-
import org.openqa.selenium.OutputType;
85
import org.slf4j.Logger;
96
import org.slf4j.LoggerFactory;
107

8+
import dtos.ImageLocation;
119
import dtos.ImageRecognitionSettingsDTO;
1210
import dtos.ImageSearchDTO;
1311

@@ -33,9 +31,9 @@ public void setQueryImageFolder() {
3331
}
3432
}
3533

36-
public Point[] findImageOnScreen(String image) throws Exception {
34+
public ImageLocation findImageOnScreen(String image) throws Exception {
3735
ImageRecognitionSettingsDTO defaultSettings = new ImageRecognitionSettingsDTO();
38-
return findImageOnScreen(image, defaultSettings).getImageRectangle();
36+
return findImageOnScreen(image, defaultSettings).getImageLocation();
3937
}
4038

4139
public ImageSearchDTO findImageOnScreen(String imageName, ImageRecognitionSettingsDTO settings) throws Exception {
@@ -94,12 +92,17 @@ private Dimension getAndroidScreenSize() throws IOException, InterruptedExceptio
9492

9593

9694
public String grabTextFromImage(String image) throws Exception {
97-
ImageRecognitionSettingsDTO settings = new ImageRecognitionSettingsDTO();
98-
settings.setCrop(true);
99-
ImageSearchDTO imageSearch = findImageOnScreen(image, settings);
95+
ImageSearchDTO imageSearch = findAndCropImage(image);
10096
String text = ImageRecognition.getTextStringFromImage(imageSearch.getScreenshotFile());
10197
return text;
10298
}
10399

100+
public ImageSearchDTO findAndCropImage(String image) throws Exception {
101+
ImageRecognitionSettingsDTO settings = new ImageRecognitionSettingsDTO();
102+
settings.setCrop(true);
103+
ImageSearchDTO imageSearch = findImageOnScreen(image, settings);
104+
return imageSearch;
105+
}
106+
104107

105108
}

src/main/java/dtos/ImageLocation.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package dtos;
2+
3+
import org.opencv.core.Point;
4+
5+
public class ImageLocation {
6+
7+
public ImageLocation(){}
8+
9+
10+
public Point getTopLeft() {
11+
return topLeft;
12+
}
13+
public void setTopLeft(Point topLeft) {
14+
this.topLeft = topLeft;
15+
}
16+
17+
public Point getTopRight() {
18+
return topRight;
19+
}
20+
21+
public void setTopRight(Point topRight) {
22+
this.topRight = topRight;
23+
}
24+
25+
public Point getBottomLeft() {
26+
return bottomLeft;
27+
}
28+
29+
public void setBottomLeft(Point bottomLeft) {
30+
this.bottomLeft = bottomLeft;
31+
}
32+
33+
public Point getBottomRight() {
34+
return bottomRight;
35+
}
36+
37+
public void setBottomRight(Point bottomRight) {
38+
this.bottomRight = bottomRight;
39+
}
40+
41+
public Point getCenter() {
42+
return center;
43+
}
44+
45+
public void setCenter(Point center) {
46+
this.center = center;
47+
}
48+
49+
public void divideCoordinatesBy(int i) {
50+
this.topLeft.x = this.topLeft.x/i;
51+
this.topLeft.y = this.topLeft.y/i;
52+
53+
this.topRight.x = this.topRight.x/i;
54+
this.topRight.y = this.topRight.y/i;
55+
56+
this.bottomLeft.x = this.bottomLeft.x/i;
57+
this.bottomLeft.y = this.bottomLeft.y/i;
58+
59+
this.bottomRight.x = this.bottomRight.x/i;
60+
this.bottomRight.y = this.bottomRight.y/i;
61+
62+
this.center.x = this.center.x/i;
63+
this.center.y = this.center.y/i;
64+
}
65+
66+
public double getWidth(){
67+
return this.topRight.x-this.topLeft.x;
68+
}
69+
70+
public double getHeight(){
71+
return this.bottomLeft.y-this.topLeft.y;
72+
}
73+
74+
75+
76+
private Point topLeft;
77+
private Point topRight;
78+
private Point bottomLeft;
79+
private Point bottomRight;
80+
private Point center;
81+
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package dtos;
22

3-
import org.opencv.core.Point;
4-
53
public class ImageSearchDTO {
64

75
public ImageSearchDTO(){
86
this.screenshotFile = null;
9-
this.imageRectangle = null;
7+
this.imageLocation = null;
108
}
119

12-
public ImageSearchDTO(String screenshotFile, Point[] imageRectangle){
10+
public ImageSearchDTO(String screenshotFile, ImageLocation imageLocation){
1311
this.screenshotFile = screenshotFile;
14-
this.imageRectangle = imageRectangle;
12+
this.imageLocation = imageLocation;
1513
}
1614

1715
public boolean isFound(){
18-
return screenshotFile!=null && imageRectangle!=null;
16+
return screenshotFile!=null && imageLocation!=null;
1917
}
2018

2119
public String getScreenshotFile() {
@@ -25,14 +23,14 @@ public void setScreenshotFile(String screenshotFile) {
2523
this.screenshotFile = screenshotFile;
2624
}
2725

28-
public Point[] getImageRectangle() {
29-
return imageRectangle;
26+
public ImageLocation getImageLocation() {
27+
return imageLocation;
3028
}
3129

32-
public void setImageRectangle(Point[] imageRectangle) {
33-
this.imageRectangle = imageRectangle;
30+
public void setImageLocation(ImageLocation imageLocation) {
31+
this.imageLocation = imageLocation;
3432
}
3533

3634
private String screenshotFile;
37-
private Point[] imageRectangle;
35+
private ImageLocation imageLocation;
3836
}

src/main/java/library/AkazeImageFinder.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

12+
import dtos.ImageLocation;
13+
import dtos.ImageSearchDTO;
14+
1215
import java.io.*;
1316
import java.lang.reflect.Field;
1417
import java.math.BigDecimal;
@@ -37,7 +40,7 @@ public double getSceneWidth(String sceneFile) {
3740
return scene_width;
3841
}
3942

40-
public Point[] findImage(String queryImageFile, String sceneFile, double tolerance) {
43+
public ImageLocation findImage(String queryImageFile, String sceneFile, double tolerance) {
4144

4245
long start_time = System.nanoTime();
4346
Mat img_object = Highgui.imread(queryImageFile, Highgui.CV_LOAD_IMAGE_UNCHANGED);
@@ -186,13 +189,16 @@ public Point[] findImage(String queryImageFile, String sceneFile, double toleran
186189
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
187190
logger.info("==> Image finder took: " + difference + " secs.");
188191

189-
if (checkFoundImageDimensions(top_left, top_right, bottom_left, bottom_right, tolerance))
190-
return null;
191-
if (checkFoundImageSizeRatio(initial_height, initial_width, top_left, top_right, bottom_left, bottom_right, initial_ratio, found_ratio1, found_ratio2, tolerance))
192-
return null;
192+
if (checkFoundImageDimensions(top_left, top_right, bottom_left, bottom_right, tolerance)){
193+
return null;
194+
}
195+
if (checkFoundImageSizeRatio(initial_height, initial_width, top_left, top_right, bottom_left, bottom_right, initial_ratio, found_ratio1, found_ratio2, tolerance)){
196+
return null;
197+
}
193198

194199
//calculate points in original orientation
195200
Point[] points = new Point[5];
201+
196202

197203
if (rotationAngle == 1.0) {
198204
points[0] = new Point(scene_height / resizeFactor - bottom_left.y, bottom_left.x);
@@ -218,25 +224,48 @@ public Point[] findImage(String queryImageFile, String sceneFile, double toleran
218224

219225
Point centerOriginal = new Point((points[0].x + (points[1].x - points[0].x) / 2) * resizeFactor, (points[0].y + (points[3].y - points[0].y) / 2) * resizeFactor);
220226

221-
points[4] = centerOriginal;
222-
223-
logger.info("Image found at coordinates: " + (int) points[4].x + ", " + (int) points[4].y + " on screen.");
224-
logger.info("All corners: " + points[0].toString() + " " + points[1].toString() + " " + points[2].toString() + " " + points[4].toString());
225227

226228
points[0] = new Point(points[0].x * resizeFactor, points[0].y * resizeFactor);
227229
points[1] = new Point(points[1].x * resizeFactor, points[1].y * resizeFactor);
228230
points[2] = new Point(points[2].x * resizeFactor, points[2].y * resizeFactor);
229231
points[3] = new Point(points[3].x * resizeFactor, points[3].y * resizeFactor);
232+
points[4] = centerOriginal;
233+
234+
logger.info("Image found at coordinates: " + (int) points[4].x + ", " + (int) points[4].y + " on screen.");
235+
logger.info("All corners: " + points[0].toString() + " " + points[1].toString() + " " + points[2].toString() + " " + points[4].toString());
230236

231-
return points;
237+
ImageLocation location = new ImageLocation();
238+
location.setTopLeft(points[0]);
239+
location.setTopRight(points[1]);
240+
location.setBottomRight(points[2]);
241+
location.setBottomLeft(points[3]);
242+
location.setCenter(centerOriginal);
243+
244+
return location;
232245
}
233246

234-
public void cropImage(String scene_filename_nopng, double x, double y, double width, double height) {
235-
236-
String scene_filename = scene_filename_nopng + ".png";
247+
public void cropImage(ImageSearchDTO imageDto) {
248+
double x = imageDto.getImageLocation().getTopLeft().x;
249+
double y = imageDto.getImageLocation().getTopLeft().y;
250+
double width = imageDto.getImageLocation().getWidth();
251+
double height = imageDto.getImageLocation().getHeight();
252+
String scene_filename = imageDto.getScreenshotFile();
253+
254+
log("x: "+x);
255+
log("y: "+y);
256+
log("width:"+width);
257+
log("height: "+height);
258+
log("lastResizeFactor: "+lastResizeFactor);
259+
237260
Mat img_object = Highgui.imread(scene_filename);
238-
239-
Rect croppedRect = new Rect((int) (x / lastResizeFactor), (int) (y / lastResizeFactor), (int) (width / lastResizeFactor), (int) (height / lastResizeFactor));
261+
262+
int x_resized = (int) (x / lastResizeFactor);
263+
int y_resized = (int) (y / lastResizeFactor);
264+
int width_resized = (int) (width / lastResizeFactor);
265+
int height_resized = (int) (height / lastResizeFactor);
266+
Rect croppedRect = new Rect(x_resized, y_resized, width_resized, height_resized);
267+
log(img_object.toString());
268+
log(croppedRect.toString());
240269
Mat croppedImage = new Mat(img_object, croppedRect);
241270
Highgui.imwrite(scene_filename, croppedImage);
242271
}

0 commit comments

Comments
 (0)