【问题标题】:how can i draw square or circle arround eyes and mouth in ciimage iOS如何在 uiimage iOS 中围绕眼睛和嘴巴绘制正方形或圆形
【发布时间】:2013-03-16 04:24:45
【问题描述】:

我是 iOS 编程新手,我已经搜索了很多核心图像链接、答案和教程,但我仍然不太了解。我有一个带有按钮的测试应用程序,简单的东西,当我按下某个过滤器按钮时,它会生成过滤器并显示它。

CIImage *inputImage = [[CIImage alloc]initWithImage:[UIImage imageNamed:@"pic1.JPG"]];
    CIFilter *colorControls = [CIFilter filterWithName:@"CIColorControls"];
    [colorControls setValue:inputImage forKey:@"inputImage"];
    [colorControls setValue:[NSNumber numberWithFloat:0.5f] forKey:@"inputSaturation"];
    [colorControls setValue:[NSNumber numberWithFloat:0.8f] forKey:@"inputContrast"];
    [colorControls setValue:[NSNumber numberWithFloat:0.4f] forKey:@"inputBrightness"];
    CIImage *outputImage = [colorControls valueForKey:@"outputImage"];
    CIContext *context = [CIContext contextWithOptions:nil];
    theImageView.image = [UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];

现在我想要一个按钮,它可以检测眼睛和嘴巴并在其周围绘制矩形或圆形,无论是什么颜色和什么颜色都没有关系。我一直到

- (IBAction)detectFace:(id)sender
{
    CIImage *inputImage = [[CIImage alloc]initWithImage:[UIImage imageNamed:@"pic1.JPG"]];
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];
    NSArray* features = [detector featuresInImage:inputImage];
   for (CIFaceFeature *faceFeature in features){}
}

现在我遇到了一个问题(我不确定这是否是正确的开始)。请帮助我想知道下一步该怎么做..提前谢谢你

【问题讨论】:

    标签: iphone draw face-detection ciimage


    【解决方案1】:

    怎么样(还没编译,但应该很接近):

    for (CIFaceFeature *faceFeature in features){
            CGRect faceRect = faceFeature.bounds;
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSetLineWidth(context, 1.0);
    
        CGContextAddRect(context, faceRect);
        CGContextDrawPath(context, kCGPathStroke);
    }
    

    【讨论】:

    • 另外,确保添加#import
    • 如果您需要按钮,您也可以使用 faceFeature.bounds 矩形创建 UIButtons,但这应该会给您大致的概念。
    • 谢谢 Owen 现在我收到错误 CGContextAddRect: invalid context 0x0 CGContextDrawPath: invalid context 0x0 CGContextSetLineWidth: invalid context 0x0 再次感谢
    • 我有按钮和ibaction,所有这些代码都在这个ibaction中。
    • 这需要在您的视图类中的一个方法中 - (void)drawRect:(CGRect)rect。绘图不是实时完成的,你调用绘图,系统会通过事件循环进行下次绘图。
    猜你喜欢
    • 2013-07-14
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多