SciSharp / SharpCV

A Computer Vision library for C# and F# that combines OpenCV and NDArray together in .NET Standard.
Apache License 2.0
294 stars 54 forks source link

SharpCV

A image library combines OpenCV and NumSharp together. SharpCV returns Mat object with NDArray supported, which makes it easier to do data manipulation like slicing.

Join the chat at https://gitter.im/publiclab/publiclab NuGet

How to use

Install OpenCV prebuild binary

PM> Install-Package SharpCV
PM> Install-Package OpenCvSharp4.runtime.win

Import SharpCV and OpenCV library

using SharpCV;
using static SharpCV.Binding;

Interact with NDArray

NDArray kernel = new float[,]
{
    { 0, -1, 0 },
    { -1, 5, -1 },
    { 0, -1, 0 }
};

var mat = new Mat(kernel);

Assert.AreEqual((3, 3), mat.shape);
Assert.AreEqual(kernel[0], mat.data[0]); // { 0, -1, 0 }
Assert.AreEqual(kernel[1], mat.data[1]); // { -1, 5, -1 }
Assert.AreEqual(kernel[2], mat.data[2]); // { 0, -1, 0 }

Pixel level access

var img = cv2.imread(imgSolar, IMREAD_COLOR.IMREAD_GRAYSCALE);
byte p = img[8, 8];
Assert.AreEqual(18, p);

img = cv2.imread(imgSolar);
var (b, g, r) = img[8, 8];
Assert.AreEqual((32, 19, 11), (b, g, r));

Convert to black and white image

var img = cv2.imread("solar.jpg");
var gray = cv2.cvtColor(img, ColorConversionCodes.COLOR_RGB2GRAY);
var (ret, binary) = cv2.threshold(gray, 0, 255, ThresholdTypes.THRESH_BINARY | ThresholdTypes.THRESH_TRIANGLE);
cv2.imshow("black and white", binary);
cv2.waitKey(0);

Video capture from file or camera

var vid = cv2.VideoCapture("road.mp4");
var (loaded, frame) = vid.read();
while (loaded)
{
    (loaded, frame) = vid.read();
    cv2.imshow("video", frame);
}

If you want to learn more about the API implementation, please refer to the official documentation.