1
1
#import " ImageDetectionPlugin.h"
2
2
#import " ImageUtils.h"
3
- #import < opencv2/highgui /ios.h>
3
+ #import < opencv2/imgcodecs /ios.h>
4
4
#import < opencv2/features2d/features2d.hpp>
5
- #import < opencv2/nonfree/nonfree.hpp>
6
5
7
6
using namespace cv ;
8
7
9
8
@interface ImageDetectionPlugin ()
10
9
{
11
10
Mat patt, desc1;
12
- vector<KeyPoint> kp1;
11
+ std:: vector<KeyPoint> kp1;
13
12
bool processFrames, debug, save_files, thread_over, called_success_detection, called_failed_detection;
14
13
NSMutableArray *detection;
15
14
NSString *callbackID;
@@ -155,9 +154,9 @@ -(void)setBase64Pattern:(NSString *)image_base64
155
154
{
156
155
UIImageWriteToSavedPhotosAlbum ([ImageUtils UIImageFromCVMat: patt], nil , nil , nil );
157
156
}
158
- ORB orb;
159
- orb. detect (patt, kp1);
160
- orb. compute (patt, kp1, desc1);
157
+ Ptr< ORB> orb = ORB::create () ;
158
+ orb-> detect (patt, kp1);
159
+ orb-> compute (patt, kp1, desc1);
161
160
}
162
161
163
162
- (void )pluginInitialize {
@@ -209,6 +208,7 @@ - (void)pluginInitialize {
209
208
210
209
[self .camera start ];
211
210
NSLog (@" ----------- CAMERA STARTED ----------" );
211
+ NSLog (@" ----------- CV_VERSION %s ----------" , CV_VERSION);
212
212
}
213
213
214
214
#pragma mark - Protocol CvVideoCameraDelegate
@@ -260,18 +260,18 @@ - (void)backgroundImageProcessing:(const Mat &)image
260
260
Mat gray = image;
261
261
// Mat image_copy = image;
262
262
Mat desc2;
263
- vector<KeyPoint> kp2;
263
+ std:: vector<KeyPoint> kp2;
264
264
265
265
cvtColor (image, gray, CV_BGRA2GRAY);
266
266
// equalizeHist(gray, gray);
267
267
268
- ORB orb;
269
- orb. detect (gray, kp2);
270
- orb. compute (gray, kp2, desc2);
268
+ Ptr< ORB> orb = ORB::create () ;
269
+ orb-> detect (gray, kp2);
270
+ orb-> compute (gray, kp2, desc2);
271
271
272
272
BFMatcher bf = BFMatcher::BFMatcher (NORM_HAMMING2, true );
273
- vector<DMatch> matches;
274
- vector<DMatch> good_matches;
273
+ std:: vector<DMatch> matches;
274
+ std:: vector<DMatch> good_matches;
275
275
276
276
if (!desc1.empty () && !desc2.empty ())
277
277
{
@@ -293,7 +293,7 @@ - (void)backgroundImageProcessing:(const Mat &)image
293
293
}
294
294
}
295
295
296
- vector<DMatch> good_matches_reduced;
296
+ std:: vector<DMatch> good_matches_reduced;
297
297
298
298
for (int i = 0 ; i < size; i++)
299
299
{
@@ -312,14 +312,14 @@ - (void)backgroundImageProcessing:(const Mat &)image
312
312
if (debug)
313
313
{
314
314
Mat imageMatches;
315
- drawMatches (patt, kp1, gray, kp2, good_matches_reduced, imageMatches, Scalar::all (-1 ), Scalar::all (-1 ), vector<char >(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
315
+ drawMatches (patt, kp1, gray, kp2, good_matches_reduced, imageMatches, Scalar::all (-1 ), Scalar::all (-1 ), std:: vector<char >(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
316
316
// image_copy = imageMatches;
317
317
}
318
318
319
319
Mat img_matches = image;
320
320
// -- Localize the object
321
- vector<Point2f> obj;
322
- vector<Point2f> scene;
321
+ std:: vector<Point2f> obj;
322
+ std:: vector<Point2f> scene;
323
323
324
324
for ( int i = 0 ; i < good_matches.size (); i++ )
325
325
{
@@ -332,23 +332,51 @@ - (void)backgroundImageProcessing:(const Mat &)image
332
332
333
333
bool result = true ;
334
334
335
- const double det = H.at <double >(0 , 0 ) * H.at <double >(1 , 1 ) - H.at <double >(1 , 0 ) * H.at <double >(0 , 1 );
336
- if (det < 0 )
337
- result = false ;
335
+ if (!H.empty ()) {
336
+ const double p1 = H.at <double >(0 , 0 );
337
+ const double p2 = H.at <double >(1 , 1 );
338
+ const double p3 = H.at <double >(1 , 0 );
339
+ const double p4 = H.at <double >(0 , 1 );
340
+ const double p5 = H.at <double >(2 , 0 );
341
+ const double p6 = H.at <double >(2 , 1 );
342
+ double det = 0 , N1 = 0 , N2 = 0 , N3 = 0 ;
343
+
344
+ if (p1 && p2 && p3 && p4) {
345
+ det = p1 * p2 - p3 * p4;
346
+ if (det < 0 )
347
+ result = false ;
348
+ } else {
349
+ result = false ;
350
+ }
338
351
339
- const double N1 = sqrt (H.at <double >(0 , 0 ) * H.at <double >(0 , 0 ) + H.at <double >(1 , 0 ) * H.at <double >(1 , 0 ));
340
- if (N1 > 4 || N1 < 0.1 )
341
- result = false ;
352
+ if (p1 && p3) {
353
+ N1 = sqrt (p1 * p1 + p3 * p3);
354
+ if (N1 > 4 || N1 < 0.1 )
355
+ result = false ;
356
+ } else {
357
+ result = false ;
358
+ }
342
359
343
- const double N2 = sqrt (H.at <double >(0 , 1 ) * H.at <double >(0 , 1 ) + H.at <double >(1 , 1 ) * H.at <double >(1 , 1 ));
344
- if (N2 > 4 || N2 < 0.1 )
345
- result = false ;
360
+ if (p2 && p4) {
361
+ N2 = sqrt (p4 * p4 + p2 * p2);
362
+ if (N2 > 4 || N2 < 0.1 )
363
+ result = false ;
364
+ } else {
365
+ result = false ;
366
+ }
346
367
347
- const double N3 = sqrt (H.at <double >(2 , 0 ) * H.at <double >(2 , 0 ) + H.at <double >(2 , 1 ) * H.at <double >(2 , 1 ));
348
- if (N3 > 0.002 )
349
- result = false ;
368
+ if (p5 && p6) {
369
+ N3 = sqrt (p5 * p5 + p6 * p6);
370
+ if (N3 > 0.002 )
371
+ result = false ;
372
+ } else {
373
+ result = false ;
374
+ }
350
375
351
- // NSLog(@"det %f, N1 %f, N2 %f, N3 %f, result %i", det, N1, N2, N3, result);
376
+ // NSLog(@"det %f, N1 %f, N2 %f, N3 %f, result %i", det, N1, N2, N3, result);
377
+ } else {
378
+ result = false ;
379
+ }
352
380
353
381
if (result)
354
382
{
@@ -361,10 +389,10 @@ - (void)backgroundImageProcessing:(const Mat &)image
361
389
if (debug)
362
390
{
363
391
// -- Get the corners from the image_1 ( the object to be "detected" )
364
- vector<Point2f> obj_corners (4 );
392
+ std:: vector<Point2f> obj_corners (4 );
365
393
obj_corners[0 ] = cvPoint (0 ,0 ); obj_corners[1 ] = cvPoint ( patt.cols , 0 );
366
394
obj_corners[2 ] = cvPoint ( patt.cols , patt.rows ); obj_corners[3 ] = cvPoint ( 0 , patt.rows );
367
- vector<Point2f> scene_corners (4 );
395
+ std:: vector<Point2f> scene_corners (4 );
368
396
369
397
perspectiveTransform ( obj_corners, scene_corners, H);
370
398
0 commit comments