An open API service indexing awesome lists of open source software.

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.

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.

![](art/01_rgba_image.png)

## Contrast
This is example for pixel operation

![](art/02_contrast.png)

```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
![](art/03_grab_r.png)

```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

![](art/04_grab_g.png)

```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

![](art/05_grab_b.png)

```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

![](art/06_composite.png)

```
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
![](art/08_split.png)

```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
![](art/09_add_sub_mul_div.png)

```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)
}
```

![](art/10_add_sub_mul_div.png)

## Blending
![](art/11_blending.png)

```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..