OpenCV 之ios 反向投影

2019-11-18  本文已影响0人  充满活力的早晨

OpenCV 之ios 反向投影

目标

本文档尝试解答如下问题:

原理

什么是反向投影?

反向投影的工作原理?

对测试图像中的每个像素 (p(i,j)),获取色调数据并找到该色调((hi,j,si,j))在直方图中的bin的位置。
查询 模型直方图 中对应的bin - (hi,j,si,j)- 并读取该bin的数值。
将此数值储存在新的图像中(BackProjection)。 你也可以先归一化 模型直方图 ,这样测试图像的输出就可以在屏幕显示了。
通过对测试图像中的每个像素采用以上步骤, 我们得到了下面的 BackProjection 结果图:


使用统计学的语言, BackProjection 中储存的数值代表了测试图像中该像素属于皮肤区域的 概率 。比如以上图为例, 亮起的区域是皮肤区域的概率更大(事实确实如此),而更暗的区域则表示更低的概率(注意手掌内部和边缘的阴影影响了检测的精度)。

源码

本程序做什么?
* 装载图像
* 转换原图像到 HSV 格式,再分离出 Hue 通道来建立直方图 (使用 OpenCV 函数 mixChannels)
* 让用户输入建立直方图所需的bin的数目

计算同一图像的直方图 (如果bin的数目改变则更新直方图) 和反向投影图。


#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#import <opencv2/imgcodecs/ios.h>
#import <opencv2/imgproc.hpp>
#import <opencv2/highgui.hpp>
#import <opencv2/core/operations.hpp>

#import <opencv2/core/core_c.h>
using namespace cv;
using namespace std;

#endif
#import "BackProjectionViewController.h"

@interface BackProjectionViewController ()

@end

@implementation BackProjectionViewController
Mat src; Mat hsv; Mat hue;
int bins = 25;

- (void)viewDidLoad {
    [super viewDidLoad];
    Mat src_base, hsv_base;
    Mat src_test1, hsv_test1;
    Mat src_test2, hsv_test2;
    Mat hsv_half_down;
    
    UIImage * srcImage = [UIImage imageNamed:@"handle1.jpg"];
    src  = [self cvMatFromUIImage:srcImage];
  UIImageView *imageView;
        imageView = [self createImageViewInRect:CGRectMake(0, 100, 150, 150)];
        [self.view addSubview:imageView];
        imageView.image  = [self UIImageFromCVMat:src];
    cvtColor( src, hsv, CV_BGR2HSV );
    hue.create( hsv.size(), hsv.depth());
     int ch[] = { 0, 0 };
     mixChannels( &hsv, 1, &hue, 1, ch, 1 );

    [self createSliderFrame:CGRectMake(150, 100, 150, 50) maxValue:180 minValue:2 block:^(float value) {
        bins= value;
        [self Hist_and_Backproj];
    }];
    [self Hist_and_Backproj];
}

-(void)Hist_and_Backproj{
    MatND hist;
     int histSize = MAX( bins, 2 );
     float hue_range[] = { 0, 180 };
     const float* ranges = { hue_range };

     /// 计算直方图并归一化
     calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges, true, false );
     normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );

     /// 计算反向投影
     MatND backproj;
     calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );
    UIImageView *imageView;
              imageView = [self createImageViewInRect:CGRectMake(0, 250, 150, 150)];
              [self.view addSubview:imageView];
              imageView.image  = [self UIImageFromCVMat:backproj];
     /// 显示反向投影

     /// 显示直方图
     int w = 400; int h = 400;
     int bin_w = cvRound( (double) w / histSize );
     Mat histImg = Mat::zeros( w, h, CV_8UC3 );

     for( int i = 0; i < bins; i ++ )
        { rectangle( histImg, cv::Point( i*bin_w, h ), cv::Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ), Scalar( 0, 0, 255 ), -1 );
        }
    calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );
    imageView = [self createImageViewInRect:CGRectMake(0, 400, 150, 150)];
            [self.view addSubview:imageView];
             imageView.image  = [self UIImageFromCVMat:histImg];
}

#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

解释

  1. 申明图像矩阵,初始化bin数目:
Mat src; Mat hsv; Mat hue;
int bins = 25;

  1. 读取输入图像并转换到HSV 格式:
 UIImage * srcImage = [UIImage imageNamed:@"handle1.jpg"];
    src  = [self cvMatFromUIImage:srcImage];

2.本教程仅仅使用Hue通道来创建1维直方图 (你可以从上面的链接下载增强版本,增强版本使用了更常见的H-S直方图,以获取更好的结果):

hue.create( hsv.size(), hsv.depth() );
int ch[] = { 0, 0 };
mixChannels( &hsv, 1, &hue, 1, ch, 1 );

你可以看到这里我们使用 mixChannels 来抽取 HSV图像的0通道(Hue)。 该函数接受了以下的实参:

[self createSliderFrame:CGRectMake(150, 100, 150, 50) maxValue:180 minValue:2 block:^(float value) {
        bins= value;
        [self Hist_and_Backproj];
    }];
  1. Hist_and_Backproj 函数: 初始化函数 calcHist 需要的实参,
void Hist_and_Backproj(int, void* )
{
  MatND hist;
  int histSize = MAX( bins, 2 );
  float hue_range[] = { 0, 180 };
  const float* ranges = { hue_range };
  1. 计算直方图并归一化到范围[0,255]
calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges, true, false );
normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
  1. 调用函数 calcBackProject 计算同一张图像的反向投影
MatND backproj;
calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );
  1. 显示1维 Hue 直方图:
int w = 400; int h = 400;
int bin_w = cvRound( (double) w / histSize );
Mat histImg = Mat::zeros( w, h, CV_8UC3 );

for( int i = 0; i < bins; i ++ )
   { rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ), Scalar( 0, 0, 255 ), -1 ); }

结果


github 地址

摘录博客

上一篇下一篇

猜你喜欢

热点阅读