OpenCV 之ios 多边形测试

OpenCV 之ios 多边形测试

目的

本教程指导用户:

  • 使用OpenCV函数 pointPolygonTest

代码


#ifdef __cplusplus
#import 
#import 
#import 
#import 
#import 

#import 
using namespace cv;
using namespace std;

#endif
#import "PointPolygonTestViewController.h"

@interface PointPolygonTestViewController ()

@end

@implementation PointPolygonTestViewController



- (void)viewDidLoad {
    [super viewDidLoad];
   /// 创建一个图形
     const int r = 100;
    Mat src = Mat::zeros( cv::Size( 4*r, 4*r ), CV_8UC1 );

     /// 绘制一系列点创建一个轮廓:
     vector vert(6);

     vert[0] = cv::Point( 1.5*r, 1.34*r );
     vert[1] = cv::Point( 1*r, 2*r );
     vert[2] = cv::Point( 1.5*r, 2.866*r );
     vert[3] = cv::Point( 2.5*r, 2.866*r );
     vert[4] = cv::Point( 3*r, 2*r );
     vert[5] = cv::Point( 2.5*r, 1.34*r );
    for( int j = 0; j < 6; j++ )
        { line( src, vert[j],  vert[(j+1)%6], Scalar( 255 ), 3, 8 ); }
    UIImageView *imageView;
    imageView = [self createImageViewInRect:CGRectMake(0, 100, 150, 150)];
    [self.view addSubview:imageView];
    imageView.image  = [self UIImageFromCVMat:src];
     /// 得到轮廓
    vector > contours; vector hierarchy;
     Mat src_copy = src.clone();
     findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
    
    Mat raw_dist( src.size(), CV_32FC1 );

    for( int j = 0; j < src.rows; j++ )
       { for( int i = 0; i < src.cols; i++ )
            { raw_dist.at(j,i) = pointPolygonTest( contours[0], Point2f(i,j), true ); }
       }

    double minVal; double maxVal;
    minMaxLoc( raw_dist, &minVal, &maxVal, 0, 0, Mat() );
    minVal = abs(minVal); maxVal = abs(maxVal);

    /// 图形化的显示距离
    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );

    for( int j = 0; j < src.rows; j++ )
       { for( int i = 0; i < src.cols; i++ )
            {
              if( raw_dist.at(j,i) < 0 )
                { drawing.at(j,i)[0] = 255 - (int) abs(raw_dist.at(j,i))*255/minVal; }
              else if( raw_dist.at(j,i) > 0 )
                { drawing.at(j,i)[2] = 255 - (int) raw_dist.at(j,i)*255/maxVal; }
              else
                { drawing.at(j,i)[0] = 255; drawing.at(j,i)[1] = 255; drawing.at(j,i)[2] = 255; }
            }
       }
    
    
    imageView = [self createImageViewInRect:CGRectMake(0, 250, 150, 150)];
       [self.view addSubview:imageView];
       imageView.image  = [self UIImageFromCVMat:drawing];

    
   
}



#pragma mark  - private
//brg
- (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
  CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();
    
  CGFloat cols = image.size.width;
  CGFloat rows = image.size.height;
    Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
  CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to  data
                                                 cols,                       // Width of bitmap
                                                 rows,                       // Height of bitmap
                                                 8,                          // Bits per component
                                                 cvMat.step[0],              // Bytes per row
                                                 colorSpace,                 // Colorspace
                                                 kCGImageAlphaNoneSkipLast |
                                                 kCGBitmapByteOrderDefault); // Bitmap info flags
  CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
  CGContextRelease(contextRef);
    
    Mat dst;
    Mat src;
    cvtColor(cvMat, dst, COLOR_RGBA2BGRA);
    cvtColor(dst, src, COLOR_BGRA2BGR);

  return src;
}

-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
//    mat 是brg 而 rgb
    Mat src;
    NSData *data=nil;
    CGBitmapInfo info =kCGImageAlphaNone|kCGBitmapByteOrderDefault;
    CGColorSpaceRef colorSpace;
    if (cvMat.depth()!=CV_8U) {
        Mat result;
        cvMat.convertTo(result, CV_8U,255.0);
        cvMat = result;
    }
  if (cvMat.elemSize() == 1) {
      colorSpace = CGColorSpaceCreateDeviceGray();
      data= [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
  } else if(cvMat.elemSize() == 3){
      cvtColor(cvMat, src, COLOR_BGR2RGB);
       data= [NSData dataWithBytes:src.data length:src.elemSize()*src.total()];
      colorSpace = CGColorSpaceCreateDeviceRGB();
  }else if(cvMat.elemSize() == 4){
      colorSpace = CGColorSpaceCreateDeviceRGB();
      cvtColor(cvMat, src, COLOR_BGRA2RGBA);
      data= [NSData dataWithBytes:src.data length:src.elemSize()*src.total()];
      info =kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault;
  }else{
      NSLog(@"[error:] 错误的颜色通道");
      return nil;
  }
  CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
  // Creating CGImage from cv::Mat
  CGImageRef imageRef = CGImageCreate(cvMat.cols,                                 //width
                                     cvMat.rows,                                 //height
                                     8,                                          //bits per component
                                     8 * cvMat.elemSize(),                       //bits per pixel
                                     cvMat.step[0],                            //bytesPerRow
                                     colorSpace,                                 //colorspace
                                     kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
                                     provider,                                   //CGDataProviderRef
                                     NULL,                                       //decode
                                     false,                                      //should interpolate
                                     kCGRenderingIntentAbsoluteColorimetric                   //intent
                                     );
  // Getting UIImage from CGImage
  UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
  CGImageRelease(imageRef);
  CGDataProviderRelease(provider);
  CGColorSpaceRelease(colorSpace);
  return finalImage;
 }
@end

结果

OpenCV 之ios 多边形测试_第1张图片

github 地址

摘录博客

你可能感兴趣的:(OpenCV 之ios 多边形测试)