SlideShare a Scribd company logo
@slorello
Intro to Computer Vision in .NET
Steve Lorello
.NET Developer advocate @Vonage
Twitter: @slorello
@slorello
What is Computer Vision?
@slorello
“ “The Goal of computer vision
is to write computer
programs that can interpret
images
Steve Seitz
@slorello
1. What is a Digital Image?
2. Hello OpenCV in .NET
3. Convolution and Edge Detection
4. Facial Detection
5. Facial Detection with Vonage Video API
6. Feature Tracking and Image Projection
Agenda
@slorello
What is a
Digital
Image?
@slorello
● An Image is a Function
● A function of Intensity Values
at Given Positions
● Those Intensity Values Fall
Along an Arbitrary Range
@slorello Source: Aaron Bobick’s Intro to Computer Vision Udacity
@slorello
Using Computer Vision in .NET
@slorello
● OpenCV (Open Source Computer Vision
Library): https://opencv.org/
● Emgu CV: http://www.emgu.com/
@slorello
● Create a Project in Visual Studio
● Install EmguCv with package manager:
Emgu.CV.runtime.<platform>
@slorello https://github.com/slorello89/ShowImage
var zero = CvInvoke.Imread(Path.Join("resources","zero.jpg"));
CvInvoke.Imshow("zero", zero);
CvInvoke.WaitKey(0);
@slorello
@slorello
Convolution and Edge Detection
@slorello https://carbon.now.sh/
@slorello http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm
Sobel Operator
@slorello
@slorello
@slorello https://github.com/slorello89/BasicSobel
CvInvoke.CvtColor(img, gray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
CvInvoke.GaussianBlur(gray, gray, new System.Drawing.Size(3, 3), 0);
CvInvoke.Sobel(gray, gradX, Emgu.CV.CvEnum.DepthType.Cv16S, 1, 0, 3);
CvInvoke.Sobel(gray, gradY, Emgu.CV.CvEnum.DepthType.Cv16S, 0, 1, 3);
CvInvoke.ConvertScaleAbs(gradX, absGradX, 1, 0);
CvInvoke.ConvertScaleAbs(gradY, absGradY, 1, 0);
CvInvoke.AddWeighted(absGradX, .5, absGradY, .5, 0, sobelGrad);
@slorello
Gradient in X Gradient in Y
@slorello
Gradient image
@slorello Source: https://dsp.stackexchange.com/
Gaussian Kernel
@slorello
Noise in images
@slorello Source: https://www.globalsino.com/EM/page1371.html
Sharpening Filter
@slorello https://github.com/slorello89/Convolution
//blur
CvInvoke.GaussianBlur(zero, blurred, new System.Drawing.Size(9, 9), 9);
var blurredImage = blurred.ToImage<Bgr, byte>();
//sharpen
var detail = (zero - blurredImage) * 2;
var sharpened = zero + detail;
@slorello
Original Blurred
@slorello
Detail Sharpened
@slorello
Original Sharpened
@slorello
Here it is at 10X detail
@slorello
Face Detection
@slorello
1. Use Haar-Like features as masks
2. Use integral images to calculate relative
shading per these masks
3. Use a Cascading Classifier to detect faces
Viola-Jones Technique
@slorello https://www.quora.com/How-can-I-understand-Haar-like-feature-for-face-detection
Haar-like features
@slorello Source https://www.mathworks.com/help/images/integral-image.html
Integral Images or Summed Area table
@slorello Source: Wikipedia
@slorello
● Construct Cascading Classifier
● Run Classification
● Use Rectangles from classification to draw
boxes around faces
@slorello https://github.com/slorello89/FacialDetection
var faceClassifier = new CascadeClassifier(Path.Join("resources",
"haarcascade_frontalface_default.xml"));
var img = CvInvoke.Imread(Path.Join("resources", "imageWithFace.jpg"));
var faces = faceClassifier.DetectMultiScale(img,
minSize: new System.Drawing.Size(300,300));
foreach(var face in faces)
{
CvInvoke.Rectangle(img, face,
new Emgu.CV.Structure.MCvScalar(255, 0, 0), 10);
}
@slorello
@slorello
Face Detection With the Vonage Video API
https://www.vonage.com/communications-apis/video/
@slorello
● Create a WPF app
● Add the OpenTok.Client SDK to it
● Add a new class implementing IVideoRender
called and extending Control
FaceDetectionVideoRenderer
● Add a Control to the Main Xaml file where we’ll
put publisher video - call it “PublisherVideo”
● Add a Detect Faces and Connect button
@slorello https://github.com/opentok-community/wpf-facial-detection
Publisher = new Publisher(Context.Instance,
renderer: PublisherVideo);
Session = new Session(Context.Instance, API_KEY, SESSION_ID);
@slorello https://github.com/opentok-community/wpf-facial-detection
private void Connect_Click(object sender, RoutedEventArgs e)
{
if (Disconnect)
{
Session.Unpublish(Publisher);
Session.Disconnect();
}
else
{
Session.Connect(TOKEN);
}
Disconnect = !Disconnect;
ConnectDisconnectButton.Content = Disconnect ? "Disconnect" : "Connect";
}
@slorello https://github.com/opentok-community/wpf-facial-detection
private void DetectFacesButton_Click(object sender, RoutedEventArgs e)
{
PublisherVideo.ToggleFaceDetection(!PublisherVideo.DetectingFaces);
foreach (var subscriber in SubscriberByStream.Values)
{
((FaceDetectionVideoRenderer)subscriber.VideoRenderer)
.ToggleFaceDetection(PublisherVideo.DetectingFaces);
}
}
@slorello https://github.com/opentok-community/wpf-facial-detection
private void Session_StreamReceived(object sender, Session.StreamEventArgs e)
{
FaceDetectionVideoRenderer renderer = new FaceDetectionVideoRenderer();
renderer.ToggleFaceDetection(PublisherVideo.DetectingFaces);
SubscriberGrid.Children.Add(renderer);
UpdateGridSize(SubscriberGrid.Children.Count);
Subscriber subscriber = new Subscriber(Context.Instance, e.Stream, renderer);
SubscriberByStream.Add(e.Stream, subscriber);
Session.Subscribe(subscriber);
}
@slorello
● Intercept each frame before it’s rendered.
● Run face detection on each frame
● Draw a rectangle on each frame to show
where the face is
● Render the Frame
@slorello https://github.com/opentok-community/wpf-facial-detection
VideoBitmap = new WriteableBitmap(frame.Width,
frame.Height, 96, 96, PixelFormats.Bgr32, null);
if (Background is ImageBrush)
{
ImageBrush b = (ImageBrush)Background;
b.ImageSource = VideoBitmap;
}
@slorello https://romannurik.github.io/SlidesCodeHighlighter/
if (VideoBitmap != null)
{
VideoBitmap.Lock();
IntPtr[] buffer = { VideoBitmap.BackBuffer };
int[] stride = { VideoBitmap.BackBufferStride };
frame.ConvertInPlace(OpenTok.PixelFormat.FormatArgb32, buffer, stride);
if (DetectingFaces)
{
using (var image = new Image<Bgr, byte>(frame.Width, frame.Height, stride[0], buffer[0]))
{
if (_watch.ElapsedMilliseconds > INTERVAL)
{
var reduced = image.Resize(1.0 / SCALE_FACTOR, Emgu.CV.CvEnum.Inter.Linear);
_watch.Restart();
_images.Add(reduced);
}
}
DrawRectanglesOnBitmap(VideoBitmap, _faces);
}
VideoBitmap.AddDirtyRect(new Int32Rect(0, 0, FrameWidth, FrameHeight));
VideoBitmap.Unlock();
}
@slorello https://github.com/opentok-community/wpf-facial-detection
System.Threading.ThreadPool.QueueUserWorkItem(delegate
{
try
{
while (true)
{
using (var image = _images.Take(token))
{
_faces = _profileClassifier.DetectMultiScale(image);
}
}
}
catch (OperationCanceledException)
{
//exit gracefully
}
}, null);
@slorello https://github.com/opentok-community/wpf-facial-detection
public static void DrawRectanglesOnBitmap(WriteableBitmap bitmap, Rectangle[] rectangles)
{
foreach (var rect in rectangles)
{
var x1 = (int)((rect.X * (int)SCALE_FACTOR) * PIXEL_POINT_CONVERSION);
var x2 = (int)(x1 + (((int)SCALE_FACTOR * rect.Width) * PIXEL_POINT_CONVERSION));
var y1 = rect.Y * (int)SCALE_FACTOR;
var y2 = y1 + ((int)SCALE_FACTOR * rect.Height);
bitmap.DrawLineAa(x1, y1, x2, y1, strokeThickness: 5, color: Colors.Blue);
bitmap.DrawLineAa(x1, y1, x1, y2, strokeThickness: 5, color: Colors.Blue);
bitmap.DrawLineAa(x1, y2, x2, y2, strokeThickness: 5, color: Colors.Blue);
bitmap.DrawLineAa(x2, y1, x2, y2, strokeThickness: 5, color: Colors.Blue);
}
}
@slorello
Feature Detection, Tracking, Image
Projection
https://www.vonage.com/communications-apis/video/
@slorello
● What’s a good Feature?
● Detect Features with Orb
● Feature Tracking with a BF
tracker
● Project an image.
@slorello
● A good feature is a part
of the image, where
there are multiple edges
● Thus we often think of
them as Corners
● We can use the ORB
method (Oriented FAST
and rotated BRIEF)
https://www.slideshare.net/slksaad/multiimage-matching-using-multiscale
-oriented-patches
@slorello https://github.com/slorello89/FeatureDetection
var orbDetector = new ORBDetector(10000);
var features1 = new VectorOfKeyPoint();
var descriptors1 = new Mat();
orbDetector.DetectAndCompute(img, null, features1, descriptors1, false);
Features2DToolbox.DrawKeypoints(img, features1, img, new Bgr(255, 0, 0));
@slorello
@slorello
● Now that we have some features we can
match them to features in other images!
● We’ll use K-nearest-neighbors matching
on the Brute-force matcher
@slorello https://github.com/slorello89/FeatureDetection
var bfMatcher = new BFMatcher(DistanceType.L1);
bfMatcher.Add(descriptors1);
bfMatcher.KnnMatch(descriptors2, knnMatches, k:1,mask:null,compactResult:true);
foreach(var matchSet in knnMatches.ToArrayOfArray())
{
if(matchSet.Length>0 && matchSet[0].Distance < 400)
{
matchList.Add(matchSet[0]);
var featureModel = features1[matchSet[0].TrainIdx];
var featureTrain = features2[matchSet[0].QueryIdx];
srcPts.Add(featureModel.Point);
dstPts.Add(featureTrain.Point);
}
}
var matches = new VectorOfDMatch(matchList.ToArray());
var imgOut = new Mat();
Features2DToolbox.DrawMatches(img, features1, img2, features2, matches,
imgOut, new MCvScalar(255, 0, 0), new MCvScalar(0, 0, 255));
@slorello
@slorello
● Image transformations
● 8 degrees of freedom
● Need at least 4 matches
● Homographies
Image Projection
@slorello Source: Szelinksi
@slorello https://inst.eecs.berkeley.edu/~cs194-26/fa17/upload/files/proj6B/cs194-26-aap/h2.png
@slorello https://github.com/slorello89/FeatureDetection
var srcPoints = InputImageToPointCorners(cat);
var dstPoints = FaceToCorners(face);
var homography = CvInvoke.FindHomography(srcPoints, dstPoints,
Emgu.CV.CvEnum.RobustEstimationAlgorithm.Ransac, 5.0);
CvInvoke.WarpPerspective(cat, projected, homography, img.Size);
img.Mat.CopyTo(projected, 1 - projected);
@slorello https://github.com/slorello89/FeatureDetection
@slorello
Wrapping Up
@slorello
A Little More About Me
● .NET Developer & Software Engineer
● .NET Developer Advocate @Vonage
● Computer Science Graduate Student
@GeorgiaTech - specializing in Computer
Perception
● Blog posts: https://dev.to/slorello or
https://www.nexmo.com/blog/author/stevelorello
● Twitter: @slorello
@slorello
https://github.com/slorello89/ShowImage
https://github.com/opentok-community/wpf-facial-detection
https://github.com/slorello89/BasicSobel
https://github.com/slorello89/FacialDetection
http://www.emgu.com/
https://opencv.org/
https://tokbox.com/developer/tutorials/
https://developer.nexmo.com/
https://www.nexmo.com/blog/2020/03/18/real-time-face-detec
tion-in-net-with-opentok-and-opencv-dr
Resources
LinkedIn: https://www.linkedin.com/in/stephen-lorello-143086a9/
Twitter: @slorello
@slorello Attribution if needed
@slorello
An image
with some
text on the
side.
URL ATTRIBUTION GOES HERE
@slorello
An image with some text over it
Attribution if needed
@slorello
“ “A really large quote would
go here so everyone can
read it.
Some Persons Name
https://website.com
@slorello
Code Snippet Examples
@slorello https://romannurik.github.io/SlidesCodeHighlighter/
var faceClassifier = new CascadeClassifier(Path.Join("resources",
"haarcascade_frontalface_default.xml"));
var img = CvInvoke.Imread(Path.Join("resources", "imageWithFace.jpg"));
var faces = faceClassifier.DetectMultiScale(img,
minSize: new System.Drawing.Size(300,300));
foreach(var face in faces)
{
CvInvoke.Rectangle(img, face,
new Emgu.CV.Structure.MCvScalar(255, 0, 0), 10);
}
@slorello https://carbon.now.sh/
@slorello
Example Web Page Slides
@slorello https://developer.nexmo.com
@slorello https://developer.nexmo.com
@slorello https://developer.nexmo.com
Website in a
mobile phone.

More Related Content

PDF
Intro to computer vision in .net update
PDF
Frontends w ithout javascript
PPTX
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
KEY
Design Patterns for Tablets and Smartphones
PPT
Retrofitting
PDF
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
PPTX
Intro to AngularJs
PPTX
jQuery for Sharepoint Dev
Intro to computer vision in .net update
Frontends w ithout javascript
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
Design Patterns for Tablets and Smartphones
Retrofitting
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
Intro to AngularJs
jQuery for Sharepoint Dev

What's hot (18)

PDF
Tips and tricks for building api heavy ruby on rails applications
PPTX
An introduction to Vue.js
PDF
Crossing platforms with JavaScript & React
PDF
Micro app-framework
PDF
Basic Tutorial of React for Programmers
PPTX
Jquery Complete Presentation along with Javascript Basics
PDF
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
PDF
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
PDF
GDG Addis - An Introduction to Django and App Engine
PPTX
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
PDF
Google App Engine in 40 minutes (the absolute essentials)
ODP
Non Conventional Android Programming En
PDF
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
PPTX
A (very) opinionated guide to MSBuild and Project Files
PDF
Real World Dependency Injection - phpugffm13
PDF
jQuery UI and Plugins
PDF
Dependency Injection in PHP - dwx13
Tips and tricks for building api heavy ruby on rails applications
An introduction to Vue.js
Crossing platforms with JavaScript & React
Micro app-framework
Basic Tutorial of React for Programmers
Jquery Complete Presentation along with Javascript Basics
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
GDG Addis - An Introduction to Django and App Engine
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Google App Engine in 40 minutes (the absolute essentials)
Non Conventional Android Programming En
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
A (very) opinionated guide to MSBuild and Project Files
Real World Dependency Injection - phpugffm13
jQuery UI and Plugins
Dependency Injection in PHP - dwx13
Ad

Similar to Intro to computer vision in .net (20)

PPTX
Computer Vision Introduction
PPTX
Primer vistazo al computer vision | 4Sessions Feb17
PDF
Practical Digital Image Processing 4
PPTX
Lecture 06: Features
PPTX
Image feature extraction
PDF
Computer Vision
PPTX
Cahall Final Intern Presentation
PPTX
Introduction_____to______ OpenCV___.pptx
PPTX
Information from pixels
PDF
"Challenges in Object Detection on Embedded Devices," a Presentation from CEVA
PDF
Machine Learning in Computer Vision
PDF
Machine learning & computer vision
PPTX
Wits presentation 6_28072015
PPTX
Evolving a Medical Image Similarity Search
PDF
OpenCV.pdf
PPTX
Rapid object detection using boosted cascade of simple features
PPTX
06 image features
PPTX
Machine Learning with Azure Cognitive Services - Face Recognition and Deep Fa...
PPTX
Artificial Intelligence Day 3 Slides for your Reference Happy Learning
PDF
Driving Assistant Solutions with Android
Computer Vision Introduction
Primer vistazo al computer vision | 4Sessions Feb17
Practical Digital Image Processing 4
Lecture 06: Features
Image feature extraction
Computer Vision
Cahall Final Intern Presentation
Introduction_____to______ OpenCV___.pptx
Information from pixels
"Challenges in Object Detection on Embedded Devices," a Presentation from CEVA
Machine Learning in Computer Vision
Machine learning & computer vision
Wits presentation 6_28072015
Evolving a Medical Image Similarity Search
OpenCV.pdf
Rapid object detection using boosted cascade of simple features
06 image features
Machine Learning with Azure Cognitive Services - Face Recognition and Deep Fa...
Artificial Intelligence Day 3 Slides for your Reference Happy Learning
Driving Assistant Solutions with Android
Ad

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
Teaching material agriculture food technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
PDF
cuic standard and advanced reporting.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Teaching material agriculture food technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
cuic standard and advanced reporting.pdf
Chapter 3 Spatial Domain Image Processing.pdf

Intro to computer vision in .net