目前我正在使用CIDetector来检测我的UI Image中的矩形.我正在做一个建议的方式,你将坐标传递给一个过滤器,以获取一个CIImage来放弃所占用的UIImage.看起来像这样:
func performRectangleDetection(image: UIKit.CIImage) -> UIKit.CIImage? {
    var resultimage: UIKit.CIImage?
    let detector:CIDetector = CIDetector(ofType: CIDetectorTypeRectangle,context: nil,options: [CIDetectorAccuracy : CIDetectorAccuracyHigh])
        // Get the detections
        let features = detector.featuresInImage(image)
        for feature in features as! [CIRectangleFeature] {
            resultimage = self.drawHighlightOverlayForPoints(image,topLeft: feature.topLeft,topRight: feature.topRight,bottomLeft: feature.bottomLeft,bottomright: feature.bottomright)
        }
    return resultimage

}


func drawHighlightOverlayForPoints(image: UIKit.CIImage,topLeft: CGPoint,topRight: CGPoint,bottomLeft: CGPoint,bottomright: CGPoint) -> UIKit.CIImage {

    var overlay = UIKit.CIImage(color: CIColor(red: 1.0,green: 0.55,blue: 0.0,alpha: 0.45))
    overlay = overlay.imageByCroppingToRect(image.extent)
    overlay = overlay.imageByApplyingFilter("CIPerspectiveTransformWithExtent",withInputParameters: [
                                                "inputExtent": CIVector(CGRect: image.extent),"inputTopLeft": CIVector(CGPoint: topLeft),"inputTopRight": CIVector(CGPoint: topRight),"inputBottomLeft": CIVector(CGPoint: bottomLeft),"inputBottomright": CIVector(CGPoint: bottomright)
        ])
    return overlay.imageByCompositingOverImage(image)
}

调用performRectangleDetection通过CIImage显示检测到的矩形.

它看起来像上图.我需要使用设置为笔画的UIBezierPath显示相同的红色矩形.我需要这样做,所以用户可以调整检测,以防它不是100%准确.我试图画出一条路,但是一直没有成功.这是我如何绘制路径.我使用一个叫做rect的自定义类来保存4点.这是检测:

func detectRect() -> Rect{
    var rect:Rect?
    let detector:CIDetector = CIDetector(ofType: CIDetectorTypeRectangle,options: [CIDetectorAccuracy : CIDetectorAccuracyHigh])
    // Get the detections
    let features = detector.featuresInImage(UIKit.CIImage(image: self)!)
    for feature in features as! [CIRectangleFeature] {
        rect = Rect(tL: feature.topLeft,tR: feature.topRight,bR: feature.bottomright,bL: feature.bottomLeft)
    }
    return rect!
}

接下来,我必须缩放坐标.这是Rect类里面的函数:

func scaleRect(image:UIImage,imageView:UIImageView) ->Rect{

    let scaleX = imageView.bounds.width/image.size.width
    var tlx = topLeft.x * scaleX
    var tly = topLeft.y * scaleX
    tlx += (imageView.bounds.width - image.size.width * scaleX) / 2.0
    tly += (imageView.bounds.height - image.size.height * scaleX) / 2.0
    let tl = CGPointMake(tlx,tly)

    var trx = topRight.x * scaleX
    var trY = topRight.y * scaleX
    trx += (imageView.bounds.width - image.size.width * scaleX) / 2.0
    trY += (imageView.bounds.height - image.size.height * scaleX) / 2.0
    let tr = CGPointMake(trx,trY)

    var brx = bottomright.x * scaleX
    var bry = bottomright.y * scaleX
    brx += (imageView.bounds.width - image.size.width * scaleX) / 2.0
    bry += (imageView.bounds.height - image.size.height * scaleX) / 2.0
    let br = CGPointMake(brx,bry)

    var blx = bottomLeft.x * scaleX
    var bly = bottomLeft.y * scaleX
    blx += (imageView.bounds.width - image.size.width * scaleX) / 2.0
    bly += (imageView.bounds.height - image.size.height * scaleX) / 2.0
    let bl = CGPointMake(blx,bly)

    let rect = Rect(tL: tl,tR: tr,bR: br,bL: bl)
    return rect
}

最后我画的路径:

var tet = image.detectRect()
tet = tet.scaleRect(image,imageView: imageView)
let shapeLayer = CAShapeLayer()
let path = ViewController.drawPath(tet.topLeft,p2: tet.topRight,p3: tet.bottomright,p4: tet.bottomLeft)
shapeLayer.path = path.CGPath
shapeLayer.linewidth = 5
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.orangeColor().CGColor
imageView.layer.addSublayer(shapeLayer)

路径已经离开屏幕并且不准确.我知道我必须将坐标从CoreImage坐标调整到UIKit坐标,然后将其缩放为UIImageView.不幸的是,我不知道该怎么做.我知道我可以重用我的一些检测代码来完成这个,但我不知道正确的步骤.任何帮助将不胜感激!谢谢.这是发生的一个例子:

更新

为了测试我在scaleRect()中执行的缩放比例,我决定使我的ImageView大小与我的图像大小相同.然后我打印了缩放之前和之后的坐标.我会认为,因为它们是一样的,我的缩放是正确的.这是代码:

var tet = image.detectRect()
//Before scaling
print(tet.topLeft)
print(tet.topRight)
print(tet.bottomright)
print(tet.bottomLeft)
print("**************************************************")
//After scaling
tet = tet.scaleRect(image,imageView: imageView)
print(tet.topLeft)
print(tet.topRight)
print(tet.bottomright)
print(tet.bottomLeft)

这是输出:

(742.386596679688,927.240844726562)

(1514.93835449219,994.811096191406)

(1514.29675292969,155.2802734375)

(741.837524414062,208.55403137207)

(742.386596679688,208.55403137207)

更新

为了尝试和缩放我的坐标,我再尝试了两件事情.

1号:
我已经尝试使用UIView convertPoint函数,以将点从图像转换为UIImageView.这是我如何编码:
我用scaleRect()函数替换了

let view_image = UIView(frame: CGRectMake(0,image.size.width,image.size.height))
let tL = view_image.convertPoint(self.topLeft,toView: imageView)
let tR = view_image.convertPoint(self.topRight,toView: imageView)
let bR = view_image.convertPoint(self.bottomright,toView: imageView)
let bL = view_image.convertPoint(self.bottomLeft,toView: imageView)

然后,我回到了这一点.

2号:
我根据图像和imageView的宽度和高度的差异,尝试了简单的坐标转换.代理代码:

let widthDiff = (image.size.width - imageView.frame.size.width)
 let highDiff = (image.size.height - imageView.frame.size.height)

 let tL = CGPointMake(self.topLeft.x-widthDiff,self.topLeft.y-highDiff)
 let tR = CGPointMake(self.topRight.x-widthDiff,self.topRight.y-highDiff)
 let bR = CGPointMake(self.bottomright.x-widthDiff,self.bottomright.y-highDiff)
 let bL = CGPointMake(self.bottomLeft.x-widthDiff,self.bottomLeft.y-highDiff)

更新
我也试过使用CGAffineTransform.码:

var transform = CGAffineTransformMakeScale(1,-1)
transform = CGAffineTransformTranslate(transform,-imageView.bounds.size.height)
let tL = CGPointApplyAffineTransform(self.topLeft,transform)
let tR = CGPointApplyAffineTransform(self.topRight,transform)
let bR = CGPointApplyAffineTransform(self.bottomright,transform)
let bL = CGPointApplyAffineTransform(self.bottomLeft,transform)

没有工作我不知道还有什么可以尝试的.请帮忙.这将不胜感激.谢谢!

解决方法

我一直在努力解决同样的问题几天,这就是我如何克服这个问题:

我做了一个自定义类来存储点并添加一些帮助函数:

//
//  ObyRectangleFeature.swift
//
//  Created by 4oby on 5/20/16.
//  copyright © 2016 cvv. All rights reserved.
//

import Foundation
import UIKit

extension CGPoint {
    func scalePointByCeficient(ƒ_x: CGFloat,ƒ_y: CGFloat) -> CGPoint {
        return CGPoint(x: self.x/ƒ_x,y: self.y/ƒ_y) //original image
    }

    func reversePointCoordinates() -> CGPoint {
        return CGPoint(x: self.y,y: self.x)
    }

    func sumPointCoordinates(add: CGPoint) -> CGPoint {
        return CGPoint(x: self.x + add.x,y: self.y + add.y)
    }

    func substractPointCoordinates(sub: CGPoint) -> CGPoint {
        return CGPoint(x: self.x - sub.x,y: self.y - sub.y)
    }
}

class ObyRectangleFeature : NSObject {

    var topLeft: CGPoint!
    var topRight: CGPoint!
    var bottomLeft: CGPoint!
    var bottomright: CGPoint!

    var centerPoint : CGPoint{
        get {
            let centerX = ((topLeft.x + bottomLeft.x)/2 + (topRight.x + bottomright.x)/2)/2
            let centerY = ((topRight.y + topLeft.y)/2 + (bottomright.y + bottomLeft.y)/2)/2
            return CGPoint(x: centerX,y: centerY)
        }

    }

    convenience init(_ rectangleFeature: CIRectangleFeature) {
        self.init()
        topLeft = rectangleFeature.topLeft
        topRight = rectangleFeature.topRight
        bottomLeft = rectangleFeature.bottomLeft
        bottomright = rectangleFeature.bottomright
    }

    override init() {
        super.init()
    }


    func rotate90Degree() -> Void {

        let centerPoint =  self.centerPoint

//        /rotate cos(90)=0,sin(90)=1
        topLeft = CGPoint(x: centerPoint.x + (topLeft.y - centerPoint.y),y: centerPoint.y + (topLeft.x - centerPoint.x))
        topRight = CGPoint(x: centerPoint.x + (topRight.y - centerPoint.y),y: centerPoint.y + (topRight.x - centerPoint.x))
        bottomLeft = CGPoint(x: centerPoint.x + (bottomLeft.y - centerPoint.y),y: centerPoint.y + (bottomLeft.x - centerPoint.x))
        bottomright = CGPoint(x: centerPoint.x + (bottomright.y - centerPoint.y),y: centerPoint.y + (bottomright.x - centerPoint.x))
    }

    func  scaleRectWithCoeficient(ƒ_x: CGFloat,ƒ_y: CGFloat) -> Void {
        topLeft =  topLeft.scalePointByCeficient(ƒ_x,ƒ_y: ƒ_y)
        topRight = topRight.scalePointByCeficient(ƒ_x,ƒ_y: ƒ_y)
        bottomLeft = bottomLeft.scalePointByCeficient(ƒ_x,ƒ_y: ƒ_y)
        bottomright = bottomright.scalePointByCeficient(ƒ_x,ƒ_y: ƒ_y)
    }

    func correctOriginPoints() -> Void {

        let deltaCenter = self.centerPoint.reversePointCoordinates().substractPointCoordinates(self.centerPoint)

        let TL = topLeft
        let TR = topRight
        let BL = bottomLeft
        let BR = bottomright

        topLeft = BL.sumPointCoordinates(deltaCenter)
        topRight = TL.sumPointCoordinates(deltaCenter)
        bottomLeft = BR.sumPointCoordinates(deltaCenter)
        bottomright = TR.sumPointCoordinates(deltaCenter)
    }
}

这是初始化代码:

let scalatedRect : ObyRectangleFeature = ObyRectangleFeature(rectangleFeature)
        // fromSize -> Initial size of the CIImage
        // toSize -> the size of the scaled Image
        let ƒ_x = (fromSize.width/toSize.width)
        let ƒ_y = (fromSize.height/toSize.height)

        /*the coeficients are interchange intentionally cause of the different
        coordinate system used by CIImage and UIImage,you Could rotate before 
        scaling,to preserve the order,but if you do,the result will be offCenter*/

        scalatedRect.scaleRectWithCoeficient(ƒ_y,ƒ_y: ƒ_x)
        scalatedRect.rotate90Degree()
        scalatedRect.correctOriginPoints()

在这一点上,ScaleRect已经准备好以任何你喜欢的方式绘制.

ios – 用UIBezierPath – Swift表示CIRectangleFeature的更多相关文章

  1. ios – 如何使用渐变填充UIBezierPath?

    我使用UIBezierPath绘制了一个图形.我可以使用纯色填充图表下方的区域,但我想用渐变而不是纯色填充图表下方的区域.但我不知道如何使渐变仅适用于图形而不是整个视图,我已经阅读了一些问题,但没有找到任何适用的问题.这是主要的图形绘制代码:我一直试图通过试验下面的代码来填充图表,但说实话,不是吗?

  2. ios – 使用watchOS 2在Apple Watch上渲染折线图

    我正在尝试使用watchOS2在AppleWatch上渲染线条/步骤图.与iOS9不同,watchOS2不支持Quartz.它只支持CoreGraphics.我尝试编写一些代码来绘制折线图但我得到一个错误“CGContextRestoreGState:无效的上下文0x0.这是一个严重的错误.这个应用程序,或它使用的库,正在使用无效的上下文,从而有助于整体系统稳定性和可靠性降低.这个通知是礼貌的:请

  3. ios – 在两个圆形UIBezierPaths之间进行动画制作时的奇怪行为

    问题我正在创造爆炸环效果.我正在使用多个CAShapeLayer并为每个层创建一个UIBezierPath.初始化视图时,所有图层都有一个宽度为0的路径.触发动作时,环会为更大的路径设置动画.如下面的演示所示,每个图层的右边缘比每个圆形图层的其余部分的动画效果要慢.演示码绘制图层:更新图层解决方法避免从路径宽度为0开始.从0开始缩放并且微小的浮点误差放大是非常困难的.非常接近零的值在浮点上不是很连续(嗯……

  4. ios – 如何使用渐变颜色填充贝塞尔曲线路径

    我的自定义UIView绘图(_rect:CGRect)函数中有一个UIBezierPath.我想用渐变色填充路径.请任何人都可以指导我如何做到这一点.我需要用渐变颜色填充剪辑,然后用黑色描边路径.SO中有一些帖子无法解决问题.例如Swift:Gradientalongabezierpath(usingCALayers)这篇文章指导如何在UIView中绘制图层,而不是在UIBezierPath中绘制

  5. ios – 如何为UIBezierPath提供cornerRadius

    我使用以下代码创建了一个矩形,现在我需要舍入这个矩形的角.但是我找不到一个名为layer.cornerRadius的属性,有人可以帮帮我吗?如果你想要特定的角落在下面的方法使用.

  6. ios – UIBezierPath具有圆形边缘的三角形

    我设计了这个代码来生成一个BezierPath,该路径用作CAShapeLayer屏蔽UIView的路径这个代码生成一个具有尖锐边缘的三角形,但我想让它圆角!,lineCapStyle和lineJoinStyle等工作,但似乎没有任何工作.所以我的问题是,我将如何舍弃UIBezierPath中的三角形的所有边缘?N.B我没有绘制这个BezierPath,所以在drawRect中的所有CGContext…

  7. ios – 如何在UIView中绘制不同颜色的多个UIBezierPath

    我想在uiview中绘制多个UIBezierPath,具有不同的笔画和填充颜色.这是代码问题是笔画和填充颜色是在当前上下文中设置的.在同一个CGContextRef中可以用不同的颜色绘制不同的UIBezierPath吗?或者我必须在单独的uiview中绘制每个UIBezierPath?解决方法你应该添加表明这些是在这种情况下必须进一步使用的新颜色.你每次打电话之前都应该这样做以便用这些颜色绘制路径.不需要为每个贝塞尔路径使用新的视图.

  8. iOS UIImage剪辑路径

    我正在使用用户使用UIBezierPath选择其中一部分的图像.如何删除/清除/清除不是该选择的一部分的所有内容?解决方法有一条路很容易.只需将路径设置为剪切路径:如果要使用多个路径的并集,则更难,因为Quartz没有任何直接计算两个路径的并集的函数.一种方法是将每个路径逐个填入蒙版,然后通过掩码绘制图像:

  9. ios – 完全删除UIBezierPath和CAShapeLayer绘制的项目

    在纵向模式下(第一次绘图)旋转到横向后,为什么旧的形状:我正在使用UIBezierPath&CAShapeLayer绘制圆圈&自定义UIView图层中的线条.问题在于能够吸引新的圈子和新的圈子.设备旋转后成功的线条,我无法删除旧的绘制形状.在设备旋转后绘制的新形状是完美的,我只需要从屏幕上移除那些粘在屏幕上的旧形状.附图像.解决方法您可以删除以前的CAShapeLayer(使用removeFrom

  10. ios – 如何在Swift中更改UIBezierPath的颜色?

    我有一个UIBezierPath的实例,我想将笔画的颜色改为黑色以外的东西.有谁知道如何在斯威夫特这样做?

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部