Tag Archives: C++

1 comment

C# and AngularJS - Receive Post data

AngularJS is awesome, C# also. But AngularJS has slightly unstandardized way of sending data over networks. Data is packet body instead of header, which may cause quite some problems  if you're unaware of this.

MVC Framework will automatically deserialize data into object when it receives, you only need to tell it that it should look for data in body instead of header.

public int method([FromBody]Model m)
        // ...


No comments

ASP.NET 5 Change Default Views Directory

Testing out new asp.net 5 MVC project template and integrating it into an existing project, predefined project structure didn't really suite my needs. Most of the things work if you move them into a subdirectory, Views directory isn't one of those. You need to create IViewLocationExpander and add it to appropriate service. Example bellow remaps everything into Server subdirectory.

public class ViewsLocationRemapper : IViewLocationExpander
    public void PopulateValues(ViewLocationExpanderContext context)
        // Do nothing

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        return viewLocations.Select(loc => "Server" + loc);

And then in Startup.cs, ConfigureServices. method, simply call:

services.Configure<RazorViewEngineOptions>(o =>
    o.ViewLocationExpanders.Add(new ViewsLocationRemapper());

Of course it can be used for simple tasks like prefixing Shader directory with _ (underscore), so that it can be easily found in Solution Explorer.

No comments

Building OpenCV Stereo Vision - Calibration

Once the hardware is set up, entire rig must be calibrated. Cheap cameras have a lot of distortion. Two major distortions are radial and tangential. Radial is usually noticed on the edge of the screen where image is slightly curved. Edges that should be perfectly straight are suddenly slightly bended. With tangential distortion the problem is somewhat similar, only that certain points on the image appear closer than their neighbors, even though they are not.

In order to overcome this problems cameras must be calibrated. Therefore, a series of images is taken with a well know pattern. OpenCV uses chessboard as shown below.


Continue reading

No comments

Building OpenCV Stereo Vision - Setup

For my most recent project I decided to build Stereo Vision for depth detection. The idea is simple. Two cameras working together similar to human eyes and detecting depth in the image. There are a few nice practical usages such as 3D pictures or point clouds, measuring distance to a point on an image and many more.

I used two Logitech's S7500 cameras. Pretty old cameras supporting resolution of 640x480px for video capture, but were enough to build a basic setup. It is not completely necessary, that cameras are of the same model, but it is highly recommended for better results. Different lens distortion, quality etc. can affect calibration, resulting in larger errors.


I duct taped both cameras on a piece of wood so that they were completely stable. The distance between camera lenses was approximately 7 cm, similar to the distance between human eyes. I additionally aligned camera heads so they were as parallel as possible.

At this point everything is ready for the last step before making depth maps - calibration.

No comments

Measuring Decibels from Microphone

This year was my third year as a mentor at a summer computer camp. We all had lots of fun, the only problem was that the participants - mostly teenage kids at the beginning of the high school - were quite talkative. Loud. To solve this problem a colleague and I came up with a nice solution for the next year.

Continue reading

No comments

Playing Video from WebCam with OpenCV and C++

Cross platform video grabbing can sometimes be really painful. Another great thing about OpenCV is that it solves such problems for you with a couple of clean lines of code.

#include <iostream>
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

int main()
    VideoCapture cam(0);

    if (!cam.isOpened())
        return -1;

    cv::Mat frame;

    while (true)
        imshow("Cam:", frame);

    return 0;
No comments

Install, compile and link OpenCV from Terminal on Linux

Almost all my development is done in Visual Studio. Currently the best IDE in my opinion, but such tools can quickly spoil you. After being thrown in front of a Linux with only a terminal and an OpenCV dependent program that needed to compile, it was a struggle, but I found the following line to compile everything:

g++ `pkg-config --cflags --libs opencv` source.cpp -o program

And in case you haven't got OpenCV yet installed, you can use simple apt-get command:

sudo apt-get install libopencv-dev
No comments

C++ - Optimizing away vtable with metaprogramming

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

In every object oriented language class inheritance is a life saver when it comes to program architecture. A rather nasty side effect that comes with it is usually performance degradation. And in C++ that penalty can be quite noticeable. When working with virtual methods in derived classes compiler generates vtable (Virtual Method Table), where address for each overridden method is specified. Vtable is then used every time virtual method is called to lookup that address, jump to it and execute code at that location. That penalty can quickly increase execution time by \(\). There is a neat trick that allows us to resolve those addresses compile-time allowing compiler to optimize vtable away using metaprogramming.

Continue reading

No comments

C++ - Unicode Strings! Say Goodbye to std::string

During my years of programming in C++, the biggest pain were probably strings. There is no simple standard cross platform way for Unicode strings. std::string class by itself is also lacking a lot of methods for every day work. Comparing it to C#, Java or any other language is almost useless. Features as starts withends withto lower, to upper, trim and similar are all missing. It has ==, while Java doesn't. Yes, all of the functions above can be easily written with templates but it adds an extra load of work and maintenance, which we all try to avoid.

After a couple of years I finally decided to try ICU - International Components for Unicode. It's a C++ library for Unicode strings. It weights roughly 20MB when shipped with executable, but it's totally worth it. Linux, Apple, Google Chrome and other mayor companies or products use it. All of the above functions are provided and a couple more, all mayor code tables are supported, file access is with already familiar C like interface and as a bonus it's fully integrated with C++ streams.

icu::UnicodeString str = L"Hi ICU with öäüšćž!";
str = str.toLower();
str = str.append(" Foo!   ").trim();

cout << str << endl;

I urge you, give ICU a try!

No comments

C# - Dependency Injections and Automatic Registration with Autofac

Dependency Injections are an awesome design pattern for adding a layer of abstraction into your application for various services and providers. Those can be quickly changed by registering another class and an instance is simply acquired by specifying desired interface as a parameter in constructor therefore decreasing or completely removing factory design pattern.

The only thing I find disturbing is whenever a new dependent class is added it must be registered with dependency injection provider, manual by default. As I consider everything that must be changed or added on a completely different place bad practice, I help myself with the following snippet of code:

var builder = new Autofac.ContainerBuilder();
var assembly = Assembly.GetExecutingAssembly();
var assemblyTypes = assembly.GetTypes();

foreach (var type in assemblyTypes)
    // Ignore interfaces
    if (type.IsInterface)

    var typeInterfaces = type.GetInterfaces();

    // Class should implement IDependecy or ISingletonDependency
    if (!typeInterfaces.Any(i => i.IsAssignableFrom(typeof(IDependency))))

    var registration = builder.RegisterType(type).AsImplementedInterfaces();

    if (typeInterfaces.Any(i => i.IsAssignableFrom(typeof(ISingletonDependency))))

Where IDependency and ISingletonDependency are:

public interface IDependency {}

public interface ISingletonDependency : IDependency {}