The corresponding coordinate positions of each boundary can be detected by the IGAVD algorithm to calculate the water and oil contents in the crude oil sample. The algorithm includes three steps: Image preprocessing, accumulating the gray values, and finding first derivative of the accumulations.
2.2.1. Image Preprocessing
A low-cost, general-purpose image sensor was used to acquire RGB image of the transparent container containing layered crude oil sample, as shown in
Figure 2. A frame of image was stored in FIFO (first in first out). MCU synchronized FIFO timing sequences to read the image data collected by the sensor, and then performed image data processing [
31]. Using FIFO to buffer image data can reduce the frequency requirement of the image processing unit, so that general MCU can be used.
The RGB image contains the information of luminance, and chrominance, each color is composed of red, green, and blue superimposed in unequal intensities [
32,
33,
34]. Each pixel can be regarded as a three-dimensional vector. RGB image has some disadvantages, such as cumbersome processing, long operation cycle, and large storage memory. In practical applications, the spatial uniformity of RGB images is inferior, and changes in color are difficult to detect by image sensors [
20,
35,
36,
37]. In the actual measurement, it was found that the image in RGB was affected by environmental factors greatly, and the edges could not be identified accurately and quickly. It was obvious that encoding image data in RGB format could not fulfill the need of efficient and reliable requirements in practical application of oil fields.
YUV is a color space commonly used in computer vision. Some studies proved that YUV color space is more suitable for computer vision than RGB color space [
34,
38]. Y channel in YUV color space represents luminance, U and V represent chrominance and chroma and carry color information. YUV color space separates luminance and chrominance [
39,
40,
41]. For a human visual system, luminance is often more important than chrominance and chroma, and so is computer vision. In RGB color space, luminance, chrominance, and chroma are combined together, and some small changes are difficult to be recognized by the computer. In order to have a better application about computer vision, based on the variation relationship between RGB and YUV color space, the correspondence between Y, U, V, and R, G, B color components of pixels is established [
40,
42].
Li et al. [
43] indicated that Y component in YUV color space can describe the grayscale value of the image. The grayscale value is a constrained linear combination of R, G, and B channels of the input color image. The weights of R, G, and B are 0.299, 0.587, and 0.114, respectively, and their summation is 1.
Grayscale image contains no chrominance and chroma information, only luminance, each pixel is equivalent to a one-dimensional vector [
20,
44]. From the perspective of the overall brightness distribution feature of the image, the grayscale image and the RGB image describes the consistent feature. Compared with RGB image, the amounts of redundant information and image data to be processed in grayscale image are reduced, and the storage memory is lower, so that the general-purpose MCU can quickly and accurately process the collected water–oil layered mixture image.
MCU Arithmetic Logic Unit calculating speed about floating-point is slow. Equation (3) enlarges the floating-point coefficients of Equation (2) by 1000 times and converts them into integer, and then reduces the coefficients by 1000 times to ensure the same conversion relationship between RGB and gray images. For Equation (3) to be rounded, 500 is added to the divisor.
However, processing data with Equation (3) could cause some problems, such as slow data calculating speed and low accuracy, which could not meet the basic requirements of water content testing. The MCU Arithmetic Logic Unit has a better mastery of handling integer and shift operations than division. As shown in Equation (4), firstly, the coefficients 0.299, 0.587, and 0.114 of the three components R, G, and B in Equation (2) are expanded by 65,536 (2
16) times and then rightly shifted by 16 bits to achieve the reduction of the corresponding coefficient, which is substantially divided by 65,536 (2
16). The symbol
denotes right-shift operation.
In relation with the features of Arithmetic Logic Unit, Equation (4) combines integer operation with shift operation, and it is used in MCU program running on the prototype. The operations in Equation (4) could shorten the conversion time from RGB to gray, save the memory of MCU, and reduce the energy consumption of the device. These advantages could accord with the requirements of fast speed, high precision, and low energy consumption in measuring process to a certain extent.
The gray image is shown in
Figure 3. In the RGB image, the upper oil part of the water–oil layered mixture is darker and the lower water part is lighter in color. In
Figure 3, it can be seen that the oil is brighter and it is distinguished more easily. For a 256-level grayscale image, the white grayscale value is the largest, which is 255, and the black grayscale value is the smallest, which is 0 [
20]. Converting RGB image to gray image makes MCU easier to process the data.
2.2.2. Accumulating the Gray Values
As can be seen in
Figure 3, the gray image shows that the oil–water interface is still fuzzy. This is due to the influence of electronic and environmental noise.
IGAVD algorithm accumulates gray value by Equation (5) for the purpose of reducing the impact on image.
C(y) denotes the total summation of the accumulated gray value of the horizontal pixels, F(x, y) represents the grayscale value of each pixel in the two-dimensional image, and m is the total number of the horizontal pixels.
The algorithm accumulates the grayscale of each horizontal pixel according to the same Y-axis in
Figure 3, the data curve of accumulated value is shown in
Figure 4. Finally, each Y-axis corresponding grayscale accumulated value is stored in RAM of the MCU. The influence of noise is weakened to some extent by accumulating the grayscale value, the edges of the image are enhanced in this way as well.
Where Ya is the coordinate position of the air–oil boundary, Yb is the coordinate position of the oil–water boundary, Yc is the coordinate position of the container’s bottom. Ca, Cb, and Cc are the grayscale accumulated values of the corresponding positions.
2.2.3. Finding First Derivative of the Accumulations
It can be seen in
Figure 4 that the grayscale accumulated value of the upper oil part is lower and the grayscale accumulated value of the water part is higher. The corresponding coordinates of the concave portion in
Figure 4 are the positions of oil in the two-dimensional image. The coordinates of each component in the water–oil layered mixture can be obtained by finding the mutations of the grayscale accumulated values.
This paper proposed the IGAVD algorithm. According to the sharp changes of the gray value at the edges of the image and gradient operators [
20,
45] on the basis of one-dimensional gray accumulated value data curve, the accumulated grayscale value of adjacent coordinates is processed by calculating first derivative in turn, and the absolute value of the result is obtained. Difference processing as:
where
y = 1, 2, …
N−1 and
N are the number of vertical pixels in a two-dimensional plane. Because the subtraction cannot be zero, the gray accumulated value at
y = 0 is removed and
y = 1 is used to replace
y = 0 as the first subtraction in our algorithm.
After calculating the difference results, three local maxima are found by multiple traversing and polling. Operations like these can effectively prevent non-edge pixels from being detected. According to the principle of gray image edge detection, the gray difference values of edge pixels are larger than that of other regions in the image. In the entire range, the coordinates of three local maxima are the coordinates of the air–oil boundary, the oil–water boundary, and the bottom of the container. The related difference values are
Da,
Db, and
Dc. The water content and oil content in the mixture can be calculated according to the corresponding coordinates:
where
Ratio1 represents the water content in the mixture and
Ratio2 represents the oil content.