9
9
import org .slf4j .Logger ;
10
10
import org .slf4j .LoggerFactory ;
11
11
12
+ import dtos .ImageLocation ;
13
+ import dtos .ImageSearchDTO ;
14
+
12
15
import java .io .*;
13
16
import java .lang .reflect .Field ;
14
17
import java .math .BigDecimal ;
@@ -37,7 +40,7 @@ public double getSceneWidth(String sceneFile) {
37
40
return scene_width ;
38
41
}
39
42
40
- public Point [] findImage (String queryImageFile , String sceneFile , double tolerance ) {
43
+ public ImageLocation findImage (String queryImageFile , String sceneFile , double tolerance ) {
41
44
42
45
long start_time = System .nanoTime ();
43
46
Mat img_object = Highgui .imread (queryImageFile , Highgui .CV_LOAD_IMAGE_UNCHANGED );
@@ -186,13 +189,16 @@ public Point[] findImage(String queryImageFile, String sceneFile, double toleran
186
189
int difference = (int ) ((end_time - start_time ) / 1e6 / 1000 );
187
190
logger .info ("==> Image finder took: " + difference + " secs." );
188
191
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
+ }
193
198
194
199
//calculate points in original orientation
195
200
Point [] points = new Point [5 ];
201
+
196
202
197
203
if (rotationAngle == 1.0 ) {
198
204
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
218
224
219
225
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 );
220
226
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 ());
225
227
226
228
points [0 ] = new Point (points [0 ].x * resizeFactor , points [0 ].y * resizeFactor );
227
229
points [1 ] = new Point (points [1 ].x * resizeFactor , points [1 ].y * resizeFactor );
228
230
points [2 ] = new Point (points [2 ].x * resizeFactor , points [2 ].y * resizeFactor );
229
231
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 ());
230
236
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 ;
232
245
}
233
246
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
+
237
260
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 ());
240
269
Mat croppedImage = new Mat (img_object , croppedRect );
241
270
Highgui .imwrite (scene_filename , croppedImage );
242
271
}
0 commit comments