-1

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. enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

3

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:

enter image description here

Maybe you need to reduce the array size by getting average values, and then calculate the paths.

Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • I think `x=17, y=38` could represent element 0 to 8 in array in screenshot. Follow this way, you could find out key points of paths. – Yun CHEN Feb 24 '18 at 11:16
0
-(NSMutableArray *)getPointsfromRGB:(CGFloat)Red :(CGFloat)Green :(CGFloat)Blue andAlpha:(CGFloat)Alpha
{
    NSMutableArray* resultPoint = [NSMutableArray new];

    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage));
    const UInt8* data = CFDataGetBytePtr(pixelData);
    CGFloat allowDiff = 0.015;

    for (int x=0; x<self.size.width; x++) {

        for (int y=0; y<self.size.width; y++) {

            int pixelInfo = ((self.size.width  * y) + x ) * 4; // The image is png
            UInt8 red = (data[pixelInfo] / 255.0 ) - Red;
            UInt8 green = (data[(pixelInfo + 1)] / 255.0) - Green;
            UInt8 blue = (data[pixelInfo + 2] / 255.0) - Blue;
            UInt8 alpha = (data[pixelInfo + 3] / 255.0) - Alpha;
            if ((red) < allowDiff && (green) < allowDiff && (blue) < allowDiff && (alpha) < allowDiff) {
                [resultPoint addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
            }

        }

    }

    return resultPoint;
}