Created
May 10, 2020 18:25
-
-
Save Samuel-IH/09a6d3822bc7a5ea76951e67f618c7c8 to your computer and use it in GitHub Desktop.
Basic Blob-Testing in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| //: Blob testing in swift based on black background and higher values for | |
| //: areas to be isolated | |
| import Cocoa | |
| let startImg = #imageLiteral(resourceName: "blobTest.png")//base imaage here | |
| let baseCGI = startImg.cgImage(forProposedRect: nil, context: nil, hints: nil) | |
| let pixData = baseCGI!.dataProvider!.data! | |
| let data = CFDataGetBytePtr(pixData)! | |
| let expectedLengthA = Int(baseCGI!.width * baseCGI!.height) | |
| let expectedLengthRGB = 3 * expectedLengthA | |
| let expectedLengthRGBA = 4 * expectedLengthA | |
| let numBytes = CFDataGetLength(pixData) | |
| var totalDiff : CGFloat = 0.0 | |
| var totalPossible : CGFloat = 0.0 | |
| var blobs : Array<Array<CGPoint>> = [] | |
| var lastBlobNumber = -1 | |
| var lastRowBlobNumbers : Array<Int> = [] | |
| var newRowBlobNumbers : Array<Int> = [] | |
| //and finally iterate through all the pixels | |
| for yWise : Int in 0...(baseCGI!.height - 1) { | |
| for xWise : Int in 0...(baseCGI!.width - 1) { | |
| let index = yWise * xWise | |
| var theColor1 : NSColor? | |
| switch numBytes { | |
| case expectedLengthA: | |
| theColor1 = NSColor(red: 0, green: 0, blue: 0, alpha: CGFloat(data[index])/255.0) | |
| totalPossible += 1 | |
| case expectedLengthRGB: | |
| theColor1 = NSColor(red: CGFloat(data[3*index])/255.0, green: CGFloat(data[3*index+1])/255.0, blue: CGFloat(data[3*index+2])/255.0, alpha: 1.0) | |
| totalPossible += 3 | |
| case expectedLengthRGBA: | |
| theColor1 = NSColor(red: CGFloat(data[4*index])/255.0, green: CGFloat(data[4*index+1])/255.0, blue: CGFloat(data[4*index+2])/255.0, alpha: CGFloat(data[4*index+3])/255.0) | |
| totalPossible += 4 | |
| default: | |
| theColor1 = NSColor.clear | |
| } | |
| if theColor1! == NSColor.white { | |
| if lastBlobNumber != -1{ | |
| blobs[lastBlobNumber].append(CGPoint.init(x: yWise, y: xWise)) | |
| } else { | |
| var shouldCreateNewBlob = false | |
| if lastRowBlobNumbers.count == 0 { | |
| shouldCreateNewBlob = true | |
| } else { | |
| if lastRowBlobNumbers[xWise] == -1 { | |
| shouldCreateNewBlob = true | |
| } else { | |
| lastBlobNumber = lastRowBlobNumbers[xWise] | |
| blobs[lastBlobNumber].append(CGPoint.init(x: xWise, y: yWise)) | |
| } | |
| } | |
| if shouldCreateNewBlob == true { | |
| lastBlobNumber = blobs.count | |
| blobs.append([CGPoint.init(x: xWise, y: yWise)]) | |
| } | |
| } | |
| } else { | |
| lastBlobNumber = -1 | |
| } | |
| newRowBlobNumbers.append(lastBlobNumber) | |
| } | |
| lastRowBlobNumbers = newRowBlobNumbers | |
| newRowBlobNumbers = [] | |
| } | |
| blobs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment