Giter Site home page Giter Site logo

imageprocessingopencv's Introduction

Communication between C++ OpenCv and C# bitmap

OpenCv is a great tool for image processing. Often times it's required to processe images using OpenCv while creating the user interface on c#. As C# does not support natively using OpenCv, we require using C++ dlls to make use of the opencv library for C#.

This repository is just a demo of showing how to accomplish that in an elegant manner.

I have seen some complecated ways to do this, by extracting every pixels and then sending them as a byte array.

This sample shows a better way by using cv::imdecode() from c++ end and bitmap Save() method from c# end.

The idea is while saving an image to disk, images are compressed and then save as jpg, bmp, png or any other format. In C# instead of a file stream or writer use a memory stream (which is essentially a byte array of the file). Get a byte array from the stream and pass is to C++ dll. In C++ end this byte array can be converted to a cv:Mat by using cv:imdecode().

Code example

C# (Details)

public Image ConvertImage(Image image)
        {
            MemoryStream convertedImageMemoryStream;
            using (MemoryStream sourceImageStream = new MemoryStream())
            {
                image.Save(sourceImageStream, System.Drawing.Imaging.ImageFormat.Png);
                byte[] sourceImagePixels = sourceImageStream.ToArray();
                ImageInfo imInfo = new ImageInfo();
                AlgorithmCpp.convertToGray(sourceImagePixels, sourceImagePixels.Count(), ref imInfo);

                byte[] imagePixels = new byte[imInfo.size];
                Marshal.Copy(imInfo.data, imagePixels, 0, imInfo.size);
                if (imInfo.data != IntPtr.Zero)
                    AlgorithmCpp.ReleaseMemoryFromC(imInfo.data);
                convertedImageMemoryStream = new MemoryStream(imagePixels);
            }
            Image processed = new Bitmap(convertedImageMemoryStream);
            return processed;
        }

C++ (Details])

DllExport void convertToGray(unsigned char* data, int dataLen, ImageInfo & imInfo)
{
	vector<unsigned char> inputImageBytes(data, data + dataLen);
	Mat image = imdecode(inputImageBytes, CV_LOAD_IMAGE_COLOR);
	Mat processed;
	cvtColor(image, processed, CV_BGR2GRAY);
	vector<unsigned char> bytes;
	imencode(".png", processed, bytes);

	imInfo.size = bytes.size();
	imInfo.data = (unsigned char *)calloc(imInfo.size, sizeof(unsigned char));
	std::copy(bytes.begin(), bytes.end(), imInfo.data);
}

Usage

1. Select x64 or x86 on both project (make sure same).
2. Select Releas configuration.
3. Configure the OpenCv directories in `AlgorithmsCpp` project
4. Build

imageprocessingopencv's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.