OpenCVForUnity (four) how to use the OpenCV image processing mechanism


foreword

In this lesson, let's take a look at how OpenCV handles such a large pixel cluster.


How to sweep every pixel of a channel

In each pixel, there can be up to 256 different values. And each pixel can have four channels, which means that each pixel can form 16.84 million colors. This is undoubtedly a heavy blow to performance. However, OpenCV can use an optimized way to reduce the workload to achieve the same effect.

Usually by reducing the color space, such as 0 ~ 9, take 0, 10 ~ 19, take 10. Round down. Each value is between ten and ten. When splitting uCher values ​​with Int values ​​the result will be char. Therefore any fraction will also be down-scaled, expressed as the formula:
insert image description here

The simple space reduction algorithm can be implemented by using matrix formulas. For example, multiplication and division consume more performance, so OpenCV will use addition and subtraction first, and the upper limit of the value is 256. In order to improve the processing speed, for larger images, it may be allocated through pre-calculation and table record lookup
.


How are images stored in memory?

The size of the image matrix depends on the color system and the number of channels, in the case of grayscale it looks like this:
insert image description here
for multi-channel columns contain as many sub-columns as there are channels. This is the case with the BGR color system for example:
insert image description here
it is worth noting that the channel order in openCV is ** BGRinstead of ** RGB.
Usually the memory is large enough and is a contiguous structure which helps faster read and write scans.


interview method

The most efficient way is to use pointers, when working with matrices we need to get a pointer to the beginning of each row and iterate over each item. For color images, since there are three channels, we need to iterate over each row more than three times. The data member of the Mat object returns a pointer to the first element. If it is empty, it means that the image has not been loaded successfully. In the case of grayscale images, the traversal is continuous.

performance difference

In order to compare better results, we run the program and compare the differences. To make the difference clearer, we use a 2560 X 1600 image. The properties presented here are color images. For a more accurate value, our data is the average value after calling 100 times.

method time
efficient way 79.4717 milliseconds
iterative procedure 83.7201 milliseconds
in flight RA 93.7878 milliseconds
LUT function 32.5759 milliseconds

Through the above table we can summarize some content. If possible, use OpenCV's existing methods as much as possible, and the efficiency will be higher. It can be seen that the most memorable way is to use the LUT function. This is because the OpenCV library enables multithreading through Intel Threaded Building Blocks.

But if you need to write a simple image scan, you can consider using pointers.

Guess you like

Origin blog.csdn.net/ww1351646544/article/details/131632734