             
             for y in 0..<Int(inputImageHeight) {
                 for x in 0..<Int(inputImageWidth) {
-                    let pixelInfo: Int = ((Int(inputImageWidth) * Int(y)) + Int(x)) * numberOfComponents
-                    
+                    let pixelInfo: Int = ((Int(bytesPerRow) * Int(y)) + Int(x * numberOfComponents))
                     if data[pixelInfo] != 255 {
                         data[pixelInfo] = UInt16(parentArray.indexOf(labelsUnion.setOf(data[pixelInfo]) ?? 0) ?? 0)
                     }

            let numberOfComponents = CGImageGetBitsPerPixel(cgImage) / CGImageGetBitsPerComponent(cgImage)
            
            //data <- bitmapData
            
            let inputImageHeight = inputImage.size.height
            let inputImageWidth  = inputImage.size.width
            
            var data = [UInt16](count: (Int(inputImageWidth) * Int(inputImageHeight)) * numberOfComponents, repeatedValue: 0)
            
            for dataIndex in 0..<data.count {
                data[dataIndex] = UInt16(bitmapData[dataIndex])

                
                for y in 0..<Int(inputImageHeight) {
                    for x in 0..<Int(inputImageWidth) {
                        let pixelInfo: Int = ((Int(bytesPerRow) * Int(y)) + Int(x * numberOfComponents))
                        
                        if data[pixelInfo] == UInt16(label) {
                            minX = min(minX, x)
