在UIImage中检测颜色并保存CGPoints

时间:2018-10-12 06:51:48

标签: ios swift image-processing core-graphics core-image

我有一个UIImage,我想要该图像具有特定颜色的所有cgPoints。例如,我有这张图片

enter image description here

现在,我想从此UIImage中获得红色CGPoints。红色可能是水平/垂直的直线。也可能是弯曲/锯齿形的线。

这是我尝试过的方法,但是我无法检测到所需的红色CGPoint。

循环遍历图像大小/像素以检测颜色

var requiredPointsInImage = [CGPoint]()
    let testImage = UIImage.init(named: "imgToTest1")
    for heightIteration in 0..<Int(testImage!.size.height) {
        for widthIteration in 0..<Int(testImage!.size.width) {
            let colorOfPoints = testImage!.getPixelColor(pos: CGPoint(x:CGFloat(widthIteration), y:CGFloat(heightIteration)), withFrameSize: testImage!.size)
            if colorOfPoints == MYColor {
                print(colorOfPoints)
                requiredPointsInImage.append(CGPoint(x:CGFloat(widthIteration), y:CGFloat(heightIteration)))
            }

        }
    }

    let newImage = drawShapesOnImage(image: testImage!, points: requiredPointsInImage)

//颜色检测

extension UIImage {
func getPixelColor(pos: CGPoint, withFrameSize size: CGSize) -> UIColor {
    let x: CGFloat = (self.size.width) * pos.x / size.width
    let y: CGFloat = (self.size.height) * pos.y / size.height
    let pixelPoint: CGPoint = CGPoint(x: x, y: y)
    let pixelData = self.cgImage!.dataProvider!.data
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
    let pixelInfo: Int = ((Int(self.size.width) * Int(pixelPoint.y)) + Int(pixelPoint.x)) * 3 //4

    let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
    let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
    let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
//        let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

    return UIColor(red: r, green: g, blue: b, alpha: 1)
}
}

//绘制检测点

func drawShapesOnImage(image: UIImage, points:[CGPoint]) -> UIImage {

    UIGraphicsBeginImageContext(image.size)
    image.draw(at: CGPoint.zero)
    let context = UIGraphicsGetCurrentContext()
    context!.setLineWidth(2.0)
    context!.setStrokeColor(UIColor.green.cgColor)
    let radius: CGFloat = 5.0
    for point in points  {
        let center = CGPoint(x: point.x, y: point.y);
        context!.addArc(center: CGPoint(x: center.x,y: center.y), radius: radius, startAngle: 0, endAngle: 360, clockwise: true)
        context!.strokePath()
    }
    let resultImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return resultImage!
}

这就是我所拥有的.....

enter image description here

注意:背景可能并不总是白色,但当然不是红色。

使用相同的getPixelColor()函数,我可以从下面任何CGPoint的图像中获得确切的颜色,但是具有y常量(例如y:1)

enter image description here

如果您认为还有其他更好的方法可以检测所有这些点,请提出建议。

1 个答案:

答案 0 :(得分:1)

所以我找到了解决方案,实际上问题是我忽略了UIImage扩展名getPixelColor()中的Alpha通道。使用Alpha解决了我的问题。

这是更新的代码!

extension UIImage {
func getPixelColor(pos: CGPoint, withFrameSize size: CGSize) -> UIColor {
let x: CGFloat = (self.size.width) * pos.x / size.width
let y: CGFloat = (self.size.height) * pos.y / size.height
let pixelPoint: CGPoint = CGPoint(x: x, y: y)
let pixelData = self.cgImage!.dataProvider!.data
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
let pixelInfo: Int = ((Int(self.size.width) * Int(pixelPoint.y)) + Int(pixelPoint.x)) * 4

let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

return UIColor(red: r, green: g, blue: b, alpha: a)
}
}

结果是:

enter image description here