检测 UIImage 中的颜色并保存 CGPoints

Detect Colour in a UIImage and save CGPoints

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

现在我想从这个 UIImage 中获取红色的 CGPoints。红色可能是一条直线 horizontal/vertical。它也可能是 curved/zigzag 行。

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

遍历图像 size/pixels 以检测颜色

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!
}

这就是我得到的.....

注意:背景不一定是白色的,当然也不是红色的。

使用相同的 getPixelColor() 函数,我可以从下面的任何 CGPoint 图像中获得准确的颜色,但 y 常数(比如 y: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)
}
}

结果是: