微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

opencv快速检测对象?

如何解决opencv快速检测对象?

我尝试使用opencv进行人脸识别,我按照链接https://medium.com/@dwayneforde/image-recognition-on-ios-with-swift-and-opencv-b5cf0667b79上的教程进行操作,但是相机无法检测到教程之类的对象, 我尝试更改NostalgiaCamera.mm上的图像以检测到它,但是仍然无法检测到。 请帮助我

这是我相机的视图

enter image description here

有时我将NostalgiaCamera.mm中的图像更改为另一幅图像,但仍然无法正常工作

  #import <opencv2/opencv.hpp>
  #import <opencv2/videoio/cap_ios.h>
  #import <opencv2/imgcodecs/ios.h>
  #include "NostalgiaCamera.h"

  using namespace cv;
  using namespace std;

  @interface NostalgiaCamera () <CvVideoCameraDelegate>
  @end

  @implementation NostalgiaCamera
   {
     UIViewController<NostalgiaCameraDelegate> * delegate;
     UIImageView * imageView;
     CvVideoCamera * videoCamera;
     cv::Mat gtpl;
    }

   - (id)initWithController:(UIViewController<NostalgiaCameraDelegate>*)c andImageView:(UIImageView*)iv
    {
   delegate = c;
   imageView = iv;

   videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView]; // Init with the UIImageView from the ViewController
   videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack; // Use the back camera
   videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait; // Ensure proper orientation
   videoCamera.rotateVideo = YES; // Ensure proper orientation
   videoCamera.defaultFPS = 30; // How often 'processImage' is called,adjust based on the amount/complexity of images
   videoCamera.delegate = self;

   // Convert UIImage to Mat and store greyscale version
   UIImage *tplImg = [UIImage imageNamed:@"item1"]; // here i change my Image
   cv::Mat tpl;
   UIImagetoMat(tplImg,tpl);
   cv::cvtColor(tpl,gtpl,CV_BGR2GRAY);

   return self;
    }

   - (void)processImage:(cv::Mat &)img {
     cv::Mat gimg;

   // Convert incoming img to greyscale to match template
   cv::cvtColor(img,gimg,CV_BGR2GRAY);

  // Check for matches with a certain threshold to help with scaling and angles
   cv::Mat res(img.rows-gtpl.rows+1,gtpl.cols-gtpl.cols+1,CV_32FC1);
   cv::matchTemplate(gimg,res,CV_TM_CCOEFF_norMED);
   cv::threshold(res,0.5,1.,CV_THRESH_TOZERO);

   double minval,maxval,threshold = 0.9;
   cv::Point minloc,maxloc;
   cv::minMaxLoc(res,&minval,&maxval,&minloc,&maxloc);

   // If it's a good enough match
   if (maxval >= threshold)
    {
    // Draw a rectangle for confirmation
     cv::rectangle(img,maxloc,cv::Point(maxloc.x + gtpl.cols,maxloc.y + gtpl.rows),CV_RGB(0,255,0),2);
    cv::floodFill(res,cv::Scalar(0),cv::Scalar(.1),cv::Scalar(1.));
    
    // Call our delegates callback method
    [delegate matchedItem];
      }
    }

    - (void)start
      {
        [videoCamera start];
      }

    - (void)stop
      {
       [videoCamera stop];
      }

    @end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。