zoukankan      html  css  js  c++  java
  • 直接获取摄像头传回的图像数据(人脸、微笑、眨眼: 识别-->第一步):图像识别第一步


    转:ios通过摄像头获取特定数据(http://www.2cto.com/kf/201404/290777.html)

    凝视: 因为近期项目需求,须要一个可以实现对摄像头图片获取当中部分内容的功能,类似于二维码扫描。

    可是仅仅须要获取特定位置的像素块进行简单计算就可以,所以听上去还是非常easy的。可是经过实践,发现,现实似乎与想象中不一样,因为摄像头拍到的数据,採用的是QZ(也就是CG框架)进行绘制。所以涉及到坐标系与frame的坐标系不一致的问题。

     
    1:尝试直接拿到摄像头数据,先输出看下。 code: 代码略。 
    总结: 直接使用摄像头数据再用imageView的方式显示出来的时刻。我们发现,数据是正确的==。所见即所得的方式。 须要注意的是: 当图片是横向拍摄的时刻。我们能够看到。相机会自己主动将图片进行90度旋转,也就是说。系统在你横向拍摄的时刻。会自己主动将图片旋转90度。满足你正常情况下的观看。

     
    所以: 我们使用imageView对拿到的数据进行展示的时刻,数据并没有颠倒的现象。

    为了防止出现这个现象的原因是出于imageView的内部实现,我们使用CgimageRef的方式。再次验证 从摄像头拿到的数据真的和我们所示一样吗? 
    code:

    CGImageRef oldImageRef=image.CGImage;

    UIImage* newImage=[UIImage imageWithCGImage:oldImageRef];

    凝视: 
    在这里,我们仅仅是对拿到的Image对象,先转换成了CGimage。再使用UIImage的方法得到新的Image对象。 
    结果:我们发现,得到的图像果然就是旋转90度的图片。而不再是原图了。

    UIImage* newImage=[UIImage imageWithCGImage:oldImageRef scale:.1 orientation:UIImageOrientationRight];

    使用上面的语句,能够将图像旋转90度变成我们须要的样子。

     

    思考: 
    这么简单的变化中,图片会不会丢失数据? ---------

    NSData* data=UIImageJPEGRepresentation(image, 0);

    NSLog(@"%lu",(unsigned long)data.length);


    还好,数据没有丢失。既然数据没有丢失,那么我们应该能够获取到当中特定的一块数据得出。再显示出来才对。 
    问题来了: 我们要获取某一块数据的内容,我们应该怎么传递rect呢?由于我们默认的rect与CG的坐标系并不同。是否须要转换呢? 



    1:我们先试一下,先把数据转过来。再从里面拿一部分. code:

    CGImageRef imageRef=image.CGImage;

    CGImageRef newimage=CGImageCreateWithImageInRect(imageRef, rect);


    结果发现: 拿到的数据还是倾斜了90度。也就是说,我们上面对数据这种转换事实上根本没有起到作用,数据在底层存储的方式就是使用CG的坐标系存储的。 
    经过试验发现,[UIImage imageWithCGImage:oldImageRef scale:.1 orientation:UIImageOrientationRight]中,对orientation的改变。不会对我们的实验结果产生不论什么影响。 也就是说,这种方法,事实上并没有对底层的数据进行操作,而仅仅只是是在初始化新的UIImage的时刻,对底层像素的读取,orientation不同。方向也不同而已。 
    思考: 
    由于,我们使用上面的方法,并没有操作究竟层的像素矩阵。也就是我。我们假设想要解决问题,有两种方式, 1:将底层数据矩阵转换成我们须要的相应坐标系的内容。

    这也就是CTM转换。 将Image对象内部数据进行转换。

    code:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    - (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
    {
        longdoublerotate = 0.0;
        CGRect rect;
        floattranslateX = 0;
        floattranslateY = 0;
        floatscaleX = 1.0;
        floatscaleY = 1.0;
         
        switch(orientation) {
            caseUIImageOrientationLeft:
                rotate = M_PI_2;
                rect = CGRectMake(0,0, image.size.height, image.size.width);
                translateX = 0;
                translateY = -rect.size.width;
                scaleY = rect.size.width/rect.size.height;
                scaleX = rect.size.height/rect.size.width;
                break;
            caseUIImageOrientationRight:
                rotate = 3* M_PI_2;
                rect = CGRectMake(0,0, image.size.height, image.size.width);
                translateX = -rect.size.height;
                translateY = 0;
                scaleY = rect.size.width/rect.size.height;
                scaleX = rect.size.height/rect.size.width;
                break;
            caseUIImageOrientationDown:
                rotate = M_PI;
                rect = CGRectMake(0,0, image.size.width, image.size.height);
                translateX = -rect.size.width;
                translateY = -rect.size.height;
                break;
            default:
                rotate = 0.0;
                rect = CGRectMake(0,0, image.size.width, image.size.height);
                translateX = 0;
                translateY = 0;
                break;
        }
         
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        //做CTM变换
        CGContextTranslateCTM(context,0.0, rect.size.height);
        CGContextScaleCTM(context,1.0, -1.0);
        CGContextRotateCTM(context, rotate);
        CGContextTranslateCTM(context, translateX, translateY);
         
        CGContextScaleCTM(context, scaleX, scaleY);
        //绘制图片
        CGContextDrawImage(context, CGRectMake(0,0, rect.size.width, rect.size.height), image.CGImage);
         
        UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
         
        returnnewPic;
    }

    结论:

    这里的解决方式。就是对底层像素矩阵进行装换之后,在对里面数据截取一部分。

    能够解决截取部分内容的问题。

    -------------------

    2:解决方式二:就是对rect进行装换。依据数据底层,进行rect的转换。

  • 相关阅读:
    Java的自动拆箱和装箱
    记录一次买阿里云服务器、建站的经验
    java中的位运算符
    java String拼接时候的一个小问题
    java获取各类容器和数组的长度
    java多线程:循环屏障
    Spring框架10:spring编程式事务控制
    Spring框架9:spring实现声明式事务控制
    Spring框架8:spring使用AOP实现事务控制
    C++ 中的bind
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6725118.html
Copyright © 2011-2022 走看看