My experience in translating between C++ and C# types

The existing production facility was tasked with developing specialized software. The following technology stack was chosen to implement the software:

  • Windows Forms is a user interface platform for creating desktop Windows applications from Microsoft. The platform uses a visual designer built into Visual Studio and the C# programming language;

  • OpenCV is a computer vision library that will be used to work with images. In this case, the C++ programming language was chosen to work with the updated library.

Let's consider the problem using the example of interaction between the OpenCV library in C++ and Windows Forms technology based on the C# programming language. Let's look at interactions of different types. Namely, let's consider transferring an image from the Mat type from OpenCV to the Bitmap type from Windows Forms. Data transfer can be done in three ways:

  • By saving to a file;

  • Correct reading from another type;

  • The simplest parts.

The first way is to save via file. The essence of this method is to save the image on the user's device in a file with a specific name, and then consider it a different type. In this example, it is possible to save the data of the cv::Mat class with the cv::imwrite function in the file img.jpg, and then read it with the Bitmap::FromFile(“img.jpg”) function, transferring the data into an object of the Bitmap class. Based on the example, the main benefit of this method is the high speed of development using this method. The disadvantages are the high consumption of resources when converting data through a file. There is also a possible loss of some data during translation, in this example, if the scalar in the cv::Mat class object was four-dimensional, then when saved to a file the scalar will become three-dimensional. This method is best used as a temporary method to test key library functions for a specific choice of technology stack.

The second method is to correctly read data from another type. This method is achieved by finding appropriate formats, since types store essentially the same information, so reading and writing data can be achieved in the same ways. This method may not always work, but in this example it is possible. You can reproduce this method using the Bitmap constructor. This constructor takes the width and height of the image in pixels, an integer byte offset between lines, a pixel format, and ultimately a pointer to an array of bytes containing data about the points. The advantage of this method is the low resource consumption due to the fact that reading occurs from an already existing data object without copying. But at the same time, this method has an average development speed, which consists of finding the appropriate format. A significant drawback of this method is the impossibility of changing data through a data object of a readable type, as well as when changing data parameters of the original type, searching for the format of a readable type.

The third method is called translation of data in the simplest parts. In this case, the simplest parts of the image will be the data scalar of each pixel. Simply put, translation in this method occurs for each pixel value of the image. Therefore, it is labor-intensive to develop an application and consume resources. But the main advantage is that after translation, the object data can be changed without harming the original object.

From the existing methods, the second one was chosen, which fully covers the needs of the project. Because the first method has a high consumption of resources, and the third method involves redundant actions. And since I only need Windows Forms to display the image without changing it. The second option turned out to be the most preferable in this project.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *