Consider the below sample irregular shape of images.How to get CGPoint from the border color. All are separate images. i'm trying to get border path.
Following method finds the points of the color approximately(Swift 4):
extension UIImage {
func getPoints(displayP3Red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> [CGPoint] {
var points = [CGPoint]()
if let imageData = self.cgImage?.dataProvider?.data {
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(imageData)
let allowDiff:CGFloat = 0.5
for x in 0 ..< Int(self.size.width) {
for y in 0 ..< Int(self.size.height) {
let pixelInfo: Int = ((Int(self.size.width) * y) + x) * 4
let diffRed = CGFloat(data[pixelInfo]) / 255.0 - displayP3Red
let diffGreen = CGFloat(data[pixelInfo+1]) / 255.0 - green
let diffBlue = CGFloat(data[pixelInfo+2]) / 255.0 - blue
let diffAlpha = CGFloat(data[pixelInfo+3]) / 255.0 - alpha
if abs(diffRed) < allowDiff
&& abs(diffGreen) < allowDiff
&& abs(diffBlue) < allowDiff
&& abs(diffAlpha) < allowDiff { // compare the color approximately
points.append(CGPoint(x: x, y: y))
}
}
}
}
return points
}
}
Use it like:
let image = UIImage(named: "yourImage.png")
let pointsOfColor = image?.getPoints(displayP3Red: 204.0/255.0, green: 33.0/255.0, blue: 50.0/255.0, alpha: 1.0)
The posted image is tested:
Maybe you need to reduce the array size by getting average values, and then calculate the paths.