OpenCV Integration Example

Note: SwiftCV package requires OpenCV installed in order to compile.


In [ ]:
%install '.package(url: "https://github.com/vvmnnnkv/SwiftCV.git", .branch("master"))' SwiftCV


Installing packages:
	.package(url: "https://github.com/vvmnnnkv/SwiftCV.git", .branch("master"))
		SwiftCV
With SwiftPM flags: []
Working in: /tmp/tmpunmxqat3
Fetching https://github.com/vvmnnnkv/SwiftCV.git
Completed resolution in 2.15s
Cloning https://github.com/vvmnnnkv/SwiftCV.git
Resolving https://github.com/vvmnnnkv/SwiftCV.git at master
Compile COpenCV imgproc.cpp
Compile COpenCV version.cpp
Compile COpenCV imgcodecs.cpp
Compile COpenCV core.cpp
Compile Swift Module 'SwiftCV' (3 sources)
Compile Swift Module 'jupyterInstalledPackages' (1 sources)
Linking ./.build/x86_64-unknown-linux/debug/libjupyterInstalledPackages.so
Initializing Swift...
Loading library...
Installation complete!

Imports


In [ ]:
%include "EnableIPythonDisplay.swift"
import Foundation
import Python
import TensorFlow
import SwiftCV

let plt = Python.import("matplotlib.pyplot")
let np = Python.import("numpy")
IPythonDisplay.shell.enable_matplotlib("inline")


Out[ ]:
('inline', 'module://ipykernel.pylab.backend_inline')

In [ ]:
// display opencv version
print(cvVersion())


4.0.1

Utility


In [ ]:
func show_img(_ img: Mat, _ w: Int = 7, _ h: Int = 5) {
    // convert from OpenCV to Tensor
    let tens = Tensor<UInt8>(cvMat: img)!
    // and from Tensor to numpy array for matplot
    show_img(tens.makeNumpyArray(), w, h)
}

func show_img(_ img: PythonObject, _ w: Int = 7, _ h: Int = 5) {
    plt.figure(figsize: [w, h])
    plt.imshow(img)
    plt.show()
}

Load image


In [ ]:
// load image in memory
let url = "https://live.staticflickr.com/2842/11335865374_0b202e2dc6_o_d.jpg"
let imgContent = Data(contentsOf: URL(string: url)!)

// make opencv image
var cvImg = imdecode(imgContent)
// convert color scheme to RGB
cvImg = cvtColor(cvImg, nil, ColorConversionCode.COLOR_BGR2RGB)
show_img(cvImg)


OpenCV Transformations

Resize


In [ ]:
show_img(
    resize(cvImg, nil, Size(100, 50), 0, 0, InterpolationFlag.INTER_AREA)
)


Zoom / Crop


In [ ]:
let zoomMat = getRotationMatrix2D(Size(cvImg.cols, cvImg.rows / 2), 0, 2)
show_img(
    warpAffine(cvImg, nil, zoomMat, Size(600, 600))
)


Rotate


In [ ]:
let rotMat = getRotationMatrix2D(Size(cvImg.cols / 2, cvImg.rows / 2), 20, 1)
show_img(
    warpAffine(cvImg, nil, rotMat, Size(cvImg.cols, cvImg.rows))
)


Pad


In [ ]:
show_img(
    copyMakeBorder(cvImg, nil, 40, 40, 40, 40, BorderType.BORDER_CONSTANT, RGBA(0, 127, 0, 0))
)


Blur


In [ ]:
show_img(
    GaussianBlur(cvImg, nil, Size(25, 25))
)