https://github.com/skyfe79/swiftimageprocessing
This project demonstrates how to do pixel operations in swift.
https://github.com/skyfe79/swiftimageprocessing
Last synced: about 1 year ago
JSON representation
This project demonstrates how to do pixel operations in swift.
- Host: GitHub
- URL: https://github.com/skyfe79/swiftimageprocessing
- Owner: skyfe79
- License: mit
- Created: 2016-03-15T16:09:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-07-29T14:26:39.000Z (almost 7 years ago)
- Last Synced: 2025-03-29T19:06:18.402Z (about 1 year ago)
- Language: Swift
- Size: 1.14 MB
- Stars: 527
- Watchers: 16
- Forks: 36
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Image Processing
This project contains swift playgrounds that demonstrate how to do pixel operations in swift.
* Swift4 : checkout master branch
* Swift3.x : checkout syntax/swift3.x branch
* Swift2.x : checkout syntax/swift2.x branch
## Thanks to RGBAImage
* [http://mhorga.org/2015/10/19/image-processing-in-ios-part-3.html](http://mhorga.org/2015/10/19/image-processing-in-ios-part-3.html)
* This articles help me a lot!
## Convert UIImage to RGBA Image
RGBAImage has pixels flat memory. You can access pixels with index directly.

## Contrast
This is example for pixel operation

```swift
let rgba = RGBAImage(image: UIImage(named: "monet")!)!
var totalR = 0
var totalG = 0
var totalB = 0
rgba.process { (pixel) -> Pixel in
totalR += Int(pixel.R)
totalG += Int(pixel.G)
totalB += Int(pixel.B)
return pixel
}
let pixelCount = rgba.width * rgba.height
let avgR = totalR / pixelCount
let avgG = totalG / pixelCount
let avgB = totalB / pixelCount
func contrast(_ image: RGBAImage) -> RGBAImage {
image.process { (pixel) -> Pixel in
var pixel = pixel
let deltaR = Int(pixel.R) - avgR
let deltaG = Int(pixel.G) - avgG
let deltaB = Int(pixel.B) - avgB
pixel.R = UInt8(max(min(255, avgR + 3 * deltaR), 0)) //clamp
pixel.G = UInt8(max(min(255, avgG + 3 * deltaG), 0))
pixel.B = UInt8(max(min(255, avgB + 3 * deltaB), 0))
return pixel
}
return image
}
let newImage = contrast(rgba).toUIImage()
```
## Grab color space
### Grab Red component

```swift
func grabR(_ image: RGBAImage) -> RGBAImage {
var outImage = image
outImage.process { (pixel) -> Pixel in
var pixel = pixel
pixel.R = pixel.R
pixel.G = 0
pixel.B = 0
return pixel
}
return outImage
}
```
### Grab Green component

```swift
func grabG(_ image: RGBAImage) -> RGBAImage {
var outImage = image
outImage.process { (pixel) -> Pixel in
var pixel = pixel
pixel.R = 0
pixel.G = pixel.G
pixel.B = 0
return pixel
}
return outImage
}
```
### Grab Blue component

```swift
func grabB(_ image: RGBAImage) -> RGBAImage {
var outImage = image
outImage.process { (pixel) -> Pixel in
var pixel = pixel
pixel.R = 0
pixel.G = 0
pixel.B = pixel.B
return pixel
}
return outImage
}
```
### Compose RGB Color components

```
public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage {
let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height)
for y in 0.. RGBAImage {
var outImage = image
outImage.process { (pixel) -> Pixel in
var pixel = pixel
let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0)
pixel.Rf = result
pixel.Gf = result
pixel.Bf = result
return pixel
}
return outImage
}
let rgba5 = RGBAImage(image: UIImage(named: "monet")!)!
gray5(rgba5).toUIImage()
```
## Refactoring Split Color Space

```swift
public static func splitRGB(_ rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) {
let R = ByteImage(width: rgba.width, height: rgba.height)
let G = ByteImage(width: rgba.width, height: rgba.height)
let B = ByteImage(width: rgba.width, height: rgba.height)
rgba.enumerate { (index, pixel) -> Void in
R.pixels[index] = pixel.R.toBytePixel()
G.pixels[index] = pixel.G.toBytePixel()
B.pixels[index] = pixel.B.toBytePixel()
}
return (R, G, B)
}
```
`ByteImage` has only one color component.
## Images ADD, SUB, MUL, DIV

```swift
public static func op(_ functor : (Double, Double) -> Double, rgbaImage1: RGBAImage, rgbaImage2: RGBAImage) -> RGBAImage {
let result : RGBAImage = RGBAImage(width:rgbaImage1.width, height: rgbaImage1.height)
for y in 0.. RGBAImage {
return op((+), rgbaImage1: rgba1, rgbaImage2: rgba2)
}
```

## Blending

```swift
public static func blending(_ img1: RGBAImage, _ img2: RGBAImage, alpha: Double) -> RGBAImage {
let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height)
for y in 0.. RGBAImage {
let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height)
for y in 0..) -> ByteImage {
var image = image
let height = image.height
let width = image.width
let maskHeight = mask.rowCount()
let maskWidth = mask.colCount()
for y in 0.. height) || (x+maskWidth) > width {
continue
}
for my in 0..