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.CGPathshapeLayer.linewidth = 5shapeLayer.fillcolor = nilshapeLayer.strokecolor = UIcolor.orangecolor().CGcolorimageVIEw.layer.addSublayer(shapeLayer)
路径已经离开屏幕并且不准确.我知道我必须将坐标从CoreImage坐标调整到UIKit坐标,然后将其缩放为UIImageVIEw.不幸的是,我不知道该怎么做.我知道我可以重用我的一些检测代码来完成这个,但我不知道正确的步骤.任何帮助将不胜感激!谢谢.这是发生的一个例子:
更新
为了测试我在scaleRect()中执行的缩放比例,我决定使我的ImageVIEw大小与我的图像大小相同.然后我打印了缩放之前和之后的坐标.我会认为,因为它们是一样的,我的缩放是正确的.这是代码:
var tet = image.detectRect()//Before scalingprint(tet.topleft)print(tet.topRight)print(tet.bottomright)print(tet.bottomleft)print("**************************************************")//After scalingtet = 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 Foundationimport UIKitextension 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全部内容,希望文章能够帮你解决ios – 用UIBezierPath – Swift表示CIRectangleFeature所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)