**1. Introduction**

Image processing involves certain operations that help improve aesthetics and enhance comprehensibility of what the image conveys. This is widely used in environment, agriculture, military, industry, and medical sciences to extract valuable information. Due to the rapid development of information technology, data handled in image processing have undergone exponential growth. Usage of classical image processing has declined and therefore quantum image processing has emerged as a feasible way to solve the problems. The coherent superposition characteristics of the quantum state and other unique quantum mechanical principles are used for generating data processing capability in quantum image processing, which can accelerate the process significantly compared to classical algorithms.

In the last two decades or so, a large number of productive techniques have emerged for quantum image processing, which serves two purposes. The first is to construct models for representation of the digital image mainly including qubit lattice [1], entangled image [2], flexible representation of the quantum image (FRQI) [3], quantum log-polar image [4], a novel enhanced quantum representation of digital images (NEQR) [5], and Quantum Boolean image processing [6], a simple quantum representation of infrared images (SQR) [7] and some extensions from FRQI or NEQR [8–16]. The other is applications based on the above which vary with types of representation, such as geometric transform [17–19], image scaling [20–23], image scrambling [24,25], image segmentation [26–29], image edge extraction [30–35], image matching [36–38], image watermarking [39,40], and so on.

Although many issues are studied by researchers, as mentioned above, quantum image processing is still an emerging field and, compared with classical image processing, it is still in its infancy. To the best of our knowledge, image component labeling has not ye<sup>t</sup>

**Citation:** Li, Y.; Hao, D.; Xu, Y.; Lai, K.A Fast Quantum Image Component Labeling Algorithm. *Mathematics* **2022**, *10*, 2718. https://doi.org/ 10.3390/math10152718

Academic Editor: Jan Sładkowski

Received: 9 June 2022 Accepted: 28 July 2022 Published: 1 August 2022

**Publisher's Note:** MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations.

**Copyright:** © 2022 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https:// creativecommons.org/licenses/by/ 4.0/).

been extended to the quantum imaging processing domain. Image component labeling is the most fundamental preprocessing required for image understanding, pattern recognition, and computer vision. By use of the labeling operation, a unique label is assigned to each connected region so that higher-level operations can process different regions separately. In some applications, image component labeling is still an active area of research in classical image processing, which has been proved to be one of the most time-consuming tasks in pattern recognition [41]. The parallel processing characteristic is the grea<sup>t</sup> advantage of quantum computation, which is one feasible way to accelerate image component labeling.

This paper proposes a fast quantum image component labeling algorithm based on a modified NEQR representation model for binary images, a quantum counterpart of classical local-operator techniques [42]. The quantum image component labeling algorithm consists of three main steps. Firstly, a binary image is represented by the quantum version using the modified NEQR model. Secondly, all pixels of the image are simultaneously worked upon by the quantum parallel-shrink operator several times until each black pixel changes to white, and the connectivity relations are reserved during the processing. Finally, the quantum label-propagate operator is executed on each pixel to restore the pixels with changed colors and each pixel assigns different numbers to different connected areas at the same time. The process is in reverse order to the image generated by the quantum parallel-shrink operations.

The rest of the paper is organized as follows. Section 2 briefly introduces classical local-operator techniques, giving a detailed example specifying how to operate the binary image to obtain the labels for the connected area. The purposed quantum version of the local-operator technique, as well as circuit design, is described in Section 3. Section 4 analyzes the circuit complexity. Simulation results based on the classical computer's Python software are given in Section 5. Finally, the conclusions are drawn in Section 6.

#### **2. Local-Operator Technique**

A connected region or component in a binary image is a maximal connected set of black pixels. The image component labeling algorithm assigns a unique label to each connected region in the image. Thus, in the labeled image, any two black pixels have the same label if and only if they lie in the same connected region. Local-operator techniques involve two types of local operations used for image region labeling: Parallel-shrink and Label-propagate. The two operators use local information from the neighborhood of a pixel to determine its new value.

## *2.1. Basic Definitions*

The following basic definitions constitute some of the concepts required for Localoperator techniques [42]. Assume black pixels have value 1 and white pixels have value 0. Let *a* ∈ {0, 1}*<sup>X</sup>* denote the source binary image on the point set *X* = {0 ≤ *i* ≤ *n*, 0 ≤ *j* ≤ *m*} with *<sup>a</sup>*(*<sup>i</sup>*, *j*) being the value at pixel *p*(*<sup>i</sup>*, *j*). Two pixels are said to be *neighbors* if they share one edge, one vertex, or both. The pixels are chosen to be squares, and then a pixel may have either 4 or 8 neighbors in terms of edges or both edges and vertices. Two pixels *p*(*<sup>i</sup>*0, *j*0) and *p*(*<sup>i</sup>*1, *j*1) are called 4-neighbors if |*i*0 − *i*1| + |*j*0 − *j*1| = 1 and 8-neighbors if max{|*<sup>i</sup>*0 − *<sup>i</sup>*1|, |*j*0 − *j*1|} ≤ 1. Let *C* be the *connectivity relation* defined on an image as follows: for all pairs of pixels, *p*, *q* ∈ *a* if and only if *p* and *q* are both black and are connected by a path in *a*. The *internal distance* between two black pixels is defined as the length of a shortest 4- or 8-neighbor path connecting them within the component. The *internal diameter* of a connected component is defined as the maximum of lengths of all internal distances among all pairs of pixels within the component. Local-operator techniques use a part of 8-neighbor components of a given *n* × *m* binary image. The parallel-shrink operator uses neighborhood *Ns* shown in Figure 1 which is the set of points {*p*(*<sup>i</sup>*, *j*), *p*(*<sup>i</sup>*, *j* + <sup>1</sup>), *p*(*<sup>i</sup>* + 1, *j*), *p*(*<sup>i</sup>* + 1, *j* + <sup>1</sup>)}, and the label-propagate operator uses neighborhood *Np* (Figure 1), which is the set of points {*p*(*<sup>i</sup>*, *j*), *p*(*<sup>i</sup>*, *j* − <sup>1</sup>), *p*(*<sup>i</sup>* − 1, *j*), *p*(*<sup>i</sup>* − 1, *j* − <sup>1</sup>)}.

**Figure 1.** Neighborhoods.

#### *2.2. The Parallel-Shrink Operator*

The parallel-shrink operator *ϕs* was first designed by Levialdi [43] and it used the Heaviside operator *H* defined on *Ns* neighborhoods, such that, if *a* = *ϕs*(*a*), then:

$$\mathfrak{a}'(i,j) = H(H(\mathfrak{a}(i,j) + \mathfrak{a}(i,j+1) + \mathfrak{a}(i+1,j) - 1) + H(\mathfrak{a}(i,j) + \mathfrak{a}(i+1,j+1))) \tag{1}$$

where *H* is the Heaviside operator defined by *H*(*t*) = 0 for *t* ≤ 0 and *H*(*t*) = 1 if *t* > 0. Since *a* is a binary image, it is easy to express *ϕs* using the logic operations ∧(*and*) and <sup>∨</sup>(*or*) as follows:

$$\mathfrak{a}'(i,j) = (\mathfrak{a}(i,j) \land (\mathfrak{a}(i,j+1) \lor \mathfrak{a}(i+1,j) \lor \mathfrak{a}(i+1,j+1))) \lor (\mathfrak{a}(i,j+1) \land \mathfrak{a}(i+1,j)) \tag{2}$$

The parallel-shrink operator shrinks the components toward the top left corner of the bounding rectangles of connected components. The two important properties of the shrinking procedure are as follows:


A component with an internal diameter *r* will shrink to a single black pixel after *r* − 1 shrinking steps, and then disappear in the next shrinking step. After each shrinking operation, a different image is obtained and the result of applying the shrinking operation *y* times to the original image is called *partial result y*. Thus, we have a sequence of images *<sup>a</sup>y*, *<sup>a</sup>y*−1, ··· , *a*0, *a*0 representing the initial binary image.

#### *2.3. The Label-Propagate Operator*

The label-propagate operators are applied in the reverse order to the images generated by the parallel-shrink operations. Suppose at a certain time of the label-propagate operator, black pixels of *ar* are labeled with the correct labels and the labels can be used to label the black pixels of *ar*−1. The label-propagate operator then continues to label the black pixels until the image returns to the initial image *a*0.

Let *l* be a global variable that saves the maximum label in the image, the initial number is 0, and *lr*(*<sup>i</sup>*, *j*) is the label of pixel *p*(*<sup>i</sup>*, *j*) in *ar*. The parallel-shrink operator *ϕp* is defined on *Np* neighborhoods, such that, if *lr* = *<sup>ϕ</sup>p*(*lr*+<sup>1</sup>), then:

$$d\_{r+1} = \begin{cases} \text{lmax}\_{r+1}(i, j) & \text{if } a\_r(i, j) = 1 \text{ and } l \text{max}\_{r+1}(i, j) \neq 0 \\\ l + 1 & \text{if } a\_r(i, j) = 1 \text{ and } l \text{max}\_{r+1}(i, j) = 0 \\\ 0 & \text{otherwise} \end{cases} \tag{3}$$

where *l*max*r*+<sup>1</sup> = *lr*+<sup>1</sup>(*<sup>i</sup>*, *j*) ∨ *lr*+<sup>1</sup>(*<sup>i</sup>*, *j* − 1) ∨ *lr*+<sup>1</sup>(*<sup>i</sup>* − 1, *j*) ∨ *lr*+<sup>1</sup>(*<sup>i</sup>* − 1, *j*) ∨ *lr*+<sup>1</sup>(*<sup>i</sup>* − 1, *j* − <sup>1</sup>), ∨ is a bit-wise logic or operation.

Note that, first, we must ensure that labels of the four neighbors for whom the value is not zero on *Np* remain the same after Equation (3) is executed, then:

$$\begin{cases} \begin{array}{ll} l\_r(i,j-1) = l\_r(i,j) & \mbox{if } \mathfrak{a}\_r(i,j-1) = 1 \\ l\_r(i-1,j) = l\_r(i,j) & \mbox{if } \mathfrak{a}\_r(i-1,j) = 1 \\ l\_r(i-1,j-1) = l\_r(i,j) & \mbox{if } \mathfrak{a}\_r(i-1,j-1) = 1 \end{array} \end{cases} \tag{4}$$

Second, we should backfill the value 1 of pixels based on *partial result y* during propagating.

#### *2.4. A Simple Example of the Local-Operator Technique*

In this subsection, we give a simple example to explain how the label-propagate operator functions. The binary image consists of two connected regions or components as shown in Figure 2a, and the maximum internal diameter of two connected regions is 3. The detailed process is described below.

**Figure 2.** The simple example of local-operator techniques. (**a**) original image. (**b**) adding border. (**c**) partial result 1. (**d**) partial result 2. (**e**) partial result 3. (**f**) propagate 3. (**g**) propagate 2. (**h**) propagate 1.

**Phase 1 Preparation:** When every pixel has its *Ns* and *Np* neighbors, the original image is extended by a 1-pixel outer border that pads the border of the image with white. The white pixels (0 value) are ignored in the board so that we can focus better on changes in the black regions (1 value), as shown in Figure 2b.

**Phase 2 Parallel shrinking:** In Phase 2, the parallel-shrink operator, (1) or (2), is performed on every pixel simultaneously. Since the maximum value of the *internal diameter* of the two connected regions is 3, the operator would be applied three times, and then all pixels of the image will change to 0 and that completes Phase 2. Figure 2c–e depict the performance process. In Figure 2c–e, we only show the pixels that were changed using the red dotted box.

**Phase 3 Label propagating:** As mentioned before, label propagating is a reverse-order process, so Equations (3) and (4) are applied for the same number of times as Phase 2. The process order is indicated in Figure 2f–h using the left arrow, and the purple dotted boxes are used for marking the label change. In Figure 2h, *p*(1, <sup>1</sup>), *p*(2, <sup>5</sup>), and *p*(3, 4) are labeled with different numbers based on (3) at the same time. We use the row–column criterion to label the pixels. In Figure 2f, serial numbers of the pixels that have been labeled are represented in the upper left corner box, *p*(1, <sup>2</sup>), *p*(2, <sup>1</sup>), *p*(3, <sup>5</sup>), *p*(4, 4) need new labels based on Equation (3), and *p*(2, 5) needs a change of the labeled number based on Equation (4) to keep consistent with *p*(3, 5)'s *Np* neighbors. In Figure 2f, the last two pixels, *p*(2, 2) and *p*(4, <sup>5</sup>), are labeled, which means that label propagating is finished, and two different label numbers are obtained.

The example shows that the local-operator technique is a parallel method that is performed simultaneously on every pixel, so it only takes *<sup>O</sup>*(*n*) time and is a type of fast image component labeling method.

#### **3. Quantum Version of the Local-Operator Technique**

In this section, a series of specific quantum circuits are designed to realize the localoperator technique.

#### *3.1. Modified NEQR Model Representation of a Binary Image*

The NEQR is a deterministic image retrieval model that facilitates the operations on an image, so we modified the model to represent the quantum image as required [5]. In the modified model, one qubit sequence is employed for storing the position information on the Cartesian coordinate system and another sequence represents the information required, including color, number of partial results, and label number. Two entangled qubit sequences are in superposition states.

The binary image with size 2*n* × 2*m* can be represented by modified NEQR as the following equation:

$$\begin{split} |I\rangle = \frac{1}{\sqrt{2^{m+n}}} \Sigma\_{Y=0}^{2^m - 1} \Sigma\_{X=0}^{2^n - 1} |f(Y, X)\rangle \otimes |YX\rangle \\ = \frac{1}{\sqrt{2^{m+n}}} \Sigma\_{Y=0}^{2^m - 1} \Sigma\_{X=0}^{2^n - 1} |LPB\rangle \otimes |YX\rangle \end{split} \tag{5}$$

$$\begin{aligned} \left| YX \right> &= \left| Y \right> \left| X \right> = \left| Y\_m Y\_{m-1} \cdot \cdots \cdot Y\_0 \right> \left| X\_n X\_{n-1} \cdot \cdots \cdot X\_0 \right> \\ \left| LPB \right> &= \left| L \right| \left| P \right> \left| B \right> = \left| L\_j L\_{j-1} \cdot \cdots L\_0 \right> \left| P\_i P\_{i-1} \cdot \cdots P\_0 \right> \left| B\_0 \right> \end{aligned} \tag{6}$$

where |*YX* address qubits represent the position information, *X* is row number, *Y* is column number, |*LPB* work qubits represent the computation information, *B* is color, *P* is number of partial results, *L* is label number, and *Yi*, *Xi*, *Li*, *Pi*, *B*0 ∈ {0, <sup>1</sup>}.

A quantum circuit equivalent to Equation (5) can be used to prepare the initial image state. Figure 3 is an example that presents the quantum circuit of Figure 2b, the address qubits are entangled using six Hadamard gates, and the information circuit of two connected regions are in dotted boxes.

#### *3.2. Basic Quantum Functional Circuits*

To realize the complex quantum circuit, a series of quantum functional operation modules are prepared for local-operator, including address shift operation module, logic operation module, assignment operation module, compare operation, full addition module, and full subtraction module.

**Figure 3.** The quantum circuit of Figure 2b.

#### 3.2.1. Address Shift Operation Circuits

It is an important operation for a large number of quantum algorithms to obtain the neighborhood information [34,35]. Shift transformation is a geometric operation that can be used to shift the whole image and it skillfully helps us to obtain the information of every pixel's adjacent information simultaneously. For example, make a one-unit shift right for an image, the pixel *p*(*<sup>x</sup>*, *y*) will be transformed to *p*(*x* + 1, *y*) and the value *a*(*x* − 1, *y*) can be visited. Shift − and + are defined in Equations (7) and (8). When it is applied on *x*, the image moves left and right, and when it is applied on *y*, the image moves up and down.

$$\begin{split} S\_{\mathbf{X}\pm}(|I\rangle) &= \frac{1}{\sqrt{2^{n+u}}} \sum\_{Y=1}^{2^{n}-1} \sum\_{X=1}^{2^{n}-1} |f(Y,X)\rangle \otimes S\_{\mathbf{X}\pm}(|YX\rangle) \\ &= \frac{1}{\sqrt{2^{n+u}}} \sum\_{Y=1}^{2^{n}-1} \sum\_{X=1}^{2^{n}-1} |f(Y,X)\rangle \otimes |Y\rangle|(X\pm 1)\mathrm{mod}2^{n}\rangle \end{split} \tag{7}$$

$$\begin{split} S\_{\mathcal{Y}\pm}(|I\rangle) &= \frac{1}{\sqrt{2^{n+n}}} \Sigma\_{Y=1}^{2^n - 1} \Sigma\_{X=1}^{2^n - 1} |f(Y, X)\rangle \otimes S\_{\mathcal{Y}\pm}(|YX\rangle) \\ &= \frac{1}{\sqrt{2^{n+n}}} \Sigma\_{Y=1}^{2^n - 1} \Sigma\_{X=1}^{2^n - 1} |f(Y, X)\rangle \otimes |(Y \pm 1)\text{mod}2^n\rangle|X\rangle \end{split} \tag{8}$$

Based on the above analysis, Equations (7) and (8) are equivalent to Equations (9) and (10), respectively.

$$S\_{\mathbf{x}\pm}(|I\rangle) = \frac{1}{\sqrt{2^{m+n}}} \sum\_{Y=1}^{2^m - 1} \sum\_{X=1}^{2^n - 1} |f(Y, X')\rangle \otimes \mathbb{S}\_{\mathbf{x}\pm}(|YX\rangle) \tag{9}$$

$$S\_{y\pm}(|I\rangle) = \frac{1}{\sqrt{2^{m+n}}} \sum\_{Y=1}^{2^m - 1} \sum\_{X=1}^{2^n - 1} |f(Y', X)\rangle \otimes S\_{y\pm}(|YX\rangle) \tag{10}$$

where *X* = (*X* ∓ 1)mod2*n* and *Y* = (*Y* ∓ <sup>1</sup>)mod2*<sup>n</sup>*.

For a two-dimensional digital image, Figure 4 is an example for *Sx*<sup>±</sup>, and *Sy*± is similar to *Sx*<sup>±</sup>**.** We call the shift transformation circuit an address shift operation circuit just like the address-of operator in classical programming.

**Figure 4.** Address shift operation circuit. (**a**) Shift + operator circuit. (**b**) Shift − operator circuit.

#### 3.2.2. Logic Operation Circuit

In reversible computing, the TOFFOLI gate is universal gate, and it can be used for simulating classical irreversible standard gates with ancilla qubits. Figure 5 is the circuit of the necessary logic operations for computation of Equations (2) and (3) for convenience, and the inputs and results are connected by black blocks. The following figures are used for the same notation.

**Figure 5.** Logic operation circuit. (**a**) Logic operation ∧ (AND). (**b**) Logic operation ∨ (OR).

#### 3.2.3. Control Assignment Operation Circuit

Assignment operation is the most basic statement in classical programming. To achieve the function, we design the control assignment operation circuit (Figure 6). The circuit consists of two parts, the first part is shown in a dotted block that is to clear the original data of the quantum wires |*X* by using a group of swap gates, the original data are saved in the ancilla qubits. The second part assigns the value of |*Y* to |*X* using a group of CNOT gates. The whole circuits are controlled by using a CNOT gate, such that, if the control wire is 1, then assignment operation is executed. As described, the designed circuit is a reversible circuit, and, by using ancilla qubits, both |*X* and |*Y* are stored during the operation.

**Figure 6.** Assignment operation circuit.

3.2.4. Compare Operation Circuit

The compare operation circuit can be used to compare two n-qubits sequences |*X* and |*Y* illustrated in Figure 7 [32], where |*X* = |*Xn*−<sup>1</sup>*Xn*−<sup>2</sup> ··· *<sup>X</sup>*1*X*0 , |*Y* = |*Yn*−<sup>1</sup>*Yn*−<sup>2</sup> ··· *<sup>Y</sup>*1*Y*0 , *Xi*, *Yi* ∈ {0, <sup>1</sup>}. The output two-qubit |*<sup>e</sup>*1*e*0 can be used to represent the result of comparison: if *e*1*e*0 = 10, then *X* > *Y*; if *e*1*e*0 = 01, then *X* < *Y*; if *e*1*e*0 = 00, then *X* = *Y*.

**Figure 7.** Compare operation circuit.

3.2.5. Full Addition Circuit

Full adder is an adder with carry, a common arithmetic unit in various algorithms. In this paper, 1-bit full addition circuit is composed of quantum CNOT and CCNOT gate, and n-bit full addition circuit is a cascade of 1-bit full addition circuit as shown in Figure 8 [44]. |*Ci*−<sup>1</sup> is the (*i* − 1)th carry qubit, |*ai* is the *i*th augend number, |*bi* is the *i*th addend number, |*Ci* is the *i*th carry qubit, and |*Si* is the sum of *a* + *b*. The relationships among |*ai* , |*bi* , |*Ci*−<sup>1</sup> , |*Ci* and |*Si* are given by

$$|S\_i\rangle = |a\_i \oplus b\_i \oplus \mathbb{C}\_{i-1}\rangle\tag{11}$$

$$|C\_i\rangle = |a\_i b\_i + (a\_i \oplus b\_i) \mathbb{C}\_{i-1}\rangle = |a\_i b\_i \oplus (a\_i \oplus b\_i) \mathbb{C}\_{i-1}\rangle \tag{12}$$

(**b**) 

**Figure 8.** Full addition circuit. (**a**) 1-bit full addition circuit. (**b**) n-bit full addition circuit.

3.2.6. Full Subtraction Circuit

The full subtractor circuit is similar to full addition circuit, the n-bit full subtraction circuit is a cascade of the 1-bit full subtraction circuit, as shown in Figure 9 [44]. |*Bi*−<sup>1</sup> is the (*i* − 1)th borrow qubit, |*ai* is the *i*th minuend number, |*bi* is the *i*th subtrahend number, |*Bi* is the *i*th borrow qubit, and |*Di* is the *i*th qubit of difference *a* − *b*. The relationships among |*ai* , |*bi* , |*Bi*−<sup>1</sup> , |*Bi* , and |*Di* are given by

$$|D\_i\rangle = |a\_i \oplus b\_i \oplus B\_{i-1}\rangle\tag{13}$$

$$|B\_i\rangle = |b\_i B\_{i-1} + \overline{a}\_i (b\_i + B\_{i-1})\rangle = |b\_i B\_{i-1} \oplus \overline{a}\_i (b\_i \oplus B\_{i-1})\rangle \tag{14}$$

(**b**) 

**Figure 9.** Full subtractor circuit. (**a**) 1-bit full subtractor circuit. (**b**) n-bit full subtractor circuit.

#### *3.3. Implementing Quantum Image Component Labeling*

The entire workflow of the proposed procedure can be accomplished in four steps. First, the classical color digital image is converted into binary format and a 1-pixel outer border as described in Section 2.4 is added. Next, the quantum image |*I* is prepared as the input image using a modified NEQR model. The quantum circuit is similar to Figure 3. Then, the quantum counterpart of parallel shrinking is executed repeatedly until color value of all pixels is zero in the image. Finally, the quantum counterpart of label propagating is executed repeatedly for labeling regions or components in the image, and the number of executions is the same as in Step 3. Similar to its classical counterpart, the most important steps of quantum image component labeling are quantum parallel shrinking and quantum label propagating.

#### 3.3.1. Quantum Parallel Shrinking

Quantum parallel shrinking is to repeat the following three steps until all pixels in the image are zero where pixel zero is the background color.

In the first step, neighborhood *Ns* is obtained through address shift operation circuits, the designed circuit is shown in Figure 10, and the color information is saved in the ancilla qubits. Note that the state of |*I* should return to the initial value after using the address shift operation circuit four times, for later use.

**Figure 10.** Obtaining of information of *N<sup>s</sup>*.

The second step, the Levialdi operator, is implemented through Logic operation circuits, and the designed circuit is shown in Figure 11. The number of ancilla qubits used is the same as the number of Logic operators.

The third step, the number of *partial results*, is stored in |*P* through the full addition circuit, and the designed circuit is shown in Part 3 of Figure 12. If |*result* = |0 and |*B* = |1 , then *P* = *P* + 1. Fourthly, the value of pixels |*B* is updated through CNOT gate (Part 4 of Figure 12). Finally, the number of loops *i* = *i* + 1 through the full addition circuit is shown in Part 5 of Figure 12. The circuit of Equation (2) is shown in Figure 12.

**Figure 12.** The circuit of Equation (2).

#### 3.3.2. Quantum Label Propagating

Quantum label propagating is to repeat the following four steps until the manipulated image becomes the original image.

The first step, neighborhood *<sup>N</sup>p*, is obtained through address shift operation circuits, the designed circuit is shown in Figure 13, and label numbers |*Li* are saved in the ancilla qubits.

**Figure 13.** Obtaining of information of *<sup>N</sup>p*.

The second step, the computation of *l*max, is implemented through Logic operation circuits, and the designed circuit is shown in Figure 14.

**Figure 14.** The computation of *l*max.

In the third step, the global variable i − 1 and Equation (3) are implemented through the designed circuit shown in Figure 15. The condition of Equation (3) is realized in part 1, |*Pi* , and |*i* as input to comparer CM1, and *Pi* = 1 means that the color of the pixel changes from 1 to 0, so *Pi*−<sup>1</sup> = 1 is equivalent to *<sup>a</sup>*(*<sup>i</sup>*, *j*) = 1. Comparer CM2 is used to determine if *l*max*i* = 0. If *Pi*−<sup>1</sup> = *i* and *l*max*i* = 0, part 2 is executed, the global variable *l* = *l* + 1, then assign *l* to *li*−<sup>1</sup>(*<sup>i</sup>*, *j*) using control assignment operator AO. If *Pi*−<sup>1</sup> = *i* and *l*max*i* = 0, part 3 is executed, and the control assignment operator AO is used to implement *li*−<sup>1</sup>(*<sup>i</sup>*, *j*) = *l*max*i*.

In the fourth step, Equation (4) is implemented through the designed circuit shown in Figure 16. *li*−<sup>1</sup>(*<sup>i</sup>*, *j*) is stored in ancilla qubits using CNOT gate in part 1. In part 2, *li*−<sup>1</sup>(*<sup>i</sup>*, *j* − 1) is obtained using address shift operator *SY*−1, the condition of *ai*−<sup>1</sup>(*<sup>i</sup>*, *j* − 1) is realized using comparer CM, and then equation *lr*(*<sup>i</sup>*, *j* − 1) = *lr*(*<sup>i</sup>*, *j*) is implemented using control assignment operator AO. Part 3 and part 4 use the same circuits as part 2 to achieve the same assignment function. In part 5, the initial state is restored.

**Figure 15.** The quantum circuit of Equation (3).

**Figure 16.** The quantum circuit of Equation (4).

#### **4. Quantum Circuit Complexity Analysis**

Section 2 introduced the classical local-operator technique, label propagating, and parallel shrinking executed the same number of times, depending on the Manhattan diameter of the largest component, so its time complexity is just *<sup>O</sup>*(*n*). Thanks to Levialdi's pioneering work, the classical local-operator technique is much faster than scan type algorithms *<sup>O</sup>*(*n*<sup>2</sup>) [41].

In this section, we focus on the quantum circuit complexity. The time complexity depends on the number of elementary gates used. The elementary gates include the NOT gate, Hadamard gate, CNOT gate, and 2 × 2 unitary operator. The time complexity of elementary gates is 1. The spatial complexity mainly refers to the ancilla qubits employed in the circuits.

### *4.1. Time Complexity*

From [45], one Toffoli gate can be further approximately simulated by six CNOT gates and [46] points out that an n-controlled NOT (n-CNOT) gate is equivalent to <sup>2</sup>(*n* − 1) Toffoli gates and 1 CNOT gate. One SWAP gate is equivalent to three CNOT gates.

Section 3.2 introduced six basic quantum functional circuits, and label propagating and parallel shrinking circuits are composed of the functional circuits. One address shift operation circuit costs the time complexity of *<sup>O</sup>*(*n*<sup>2</sup>). Logic operation ∧ is one Toffoli gate, and the time complexity is *<sup>O</sup>*(6). Logic operation ∨ needs five NOT gates and one Toffoli gate, and the time complexity is *<sup>O</sup>*(11). Control Assignment Operation needs 2*n* SWAP gates and *n* Toffoli gates, and the time complexity is *<sup>O</sup>*(<sup>6</sup>*n* + <sup>6</sup>*n*) = *<sup>O</sup>*(<sup>12</sup>*n*). Compare Operation needs 4 Toffoli gates and no more than 4n n-CNOT gates, and the time complexity is *O*(24 + 4*n* × (2(*n* − 1))) ≈ *<sup>O</sup>*(*n*<sup>2</sup>). The 1-bit full addition circuit needs two Toffoli gates and two CNOT gates, and the n-bit full addition circuit needs (*n* − 1) 1-bit full addition circuit, so the n-bit full addition circuit's time complexity is *<sup>O</sup>*((*n* − 1) × (12 + 2)) = *<sup>O</sup>*(<sup>14</sup>*n* − <sup>14</sup>). The 1-bit full subtractor circuit needs two NOT gates, four CNOT gates, and two Toffoli gates, the n-bit full subtractor circuit needs (*n* − 1) 1-bit full subtractor circuits, and the time complexity of the n-bit full subtractor circuit is *<sup>O</sup>*((*n* − 1) × (2 + 4 + 12)) = *<sup>O</sup>*(<sup>18</sup>*n* − <sup>18</sup>).

Considering a 2*n* ×2*<sup>m</sup>* binary image, *n* > *m*, the time complexity is analyzed as follows:

#### 4.1.1. Quantum Parallel Shrinking

From Figures 10 and 11, the quantum parallel shrinking circuit is composed of four address shift operation circuits, three Logic operation ∧, two Logic operation ∨, and four CNOT, so the time complexity is *<sup>O</sup>*(<sup>4</sup>*n*<sup>2</sup> + 18 + 2 × 11 + 4) ≈ *<sup>O</sup>*(<sup>4</sup>*n*<sup>2</sup>).

#### 4.1.2. Quantum Label Propagating

From Figures 13–15, the quantum label propagating circuit is composed of four address shift operations, four Logic operation ∧, one n-bit full subtractors, two compare operation, one n-bit full addition circuit, and two assignment operations, so the time complexity is:

$$O(4n^2 + 24 + (18n - 18) + 2n^2 + (14n - 14) + 24n) = O(6n^2 + 56n - 8) \approx O(6n^2)$$

According to the above analysis, time complexity of the proposed quantum image component labeling algorithm is *<sup>O</sup>*(<sup>10</sup>*n*<sup>2</sup>) ≈ *<sup>O</sup>*(*n*<sup>2</sup>), which is only the second-order polynomial function of image size.

#### *4.2. Spatial Complexity*

Table 1 shows that the number of ancilla qubits in basic quantum functional circuits is a linear function of image size. Therefore, ancilla qubits of the proposed quantum image component labeling algorithm is also a linear function of image size. That is, the spatial complexity of the quantum circuits designed in this paper is *<sup>O</sup>*(*cn*).


**Table 1.** Number of ancilla qubits used by the basic quantum functional circuits.

Although quantum complexity is discussed, lots of quantum algorithms still cannot be applied to the quantum computer or a quantum simulator. Quantum computers and quantum simulations are in their infancy, quantum computing will be limited to about 10 qubits in quantum computer, and a quantum simulator can only operate at most 30 qubits. LaRose [47] has given a detailed explanation of quantum software platforms. Therefore, in the next section, we give the use of matrix calculation to complete the algorithm simulation, which is also a common practice at present.

#### **5. Simulation on Classical Computer**

This section describes simulations of the quantum image component labeling algorithm on a classical computer, while quantum computers are currently not at hand. The simulations were run on a classical computer with Inter (R) Core (TM) i7-7500U @2.70 GHz 8.0 GB RAM and 64-bit operating system. The simulations are based on linear algebra with complex vectors as quantum states and unitary matrices as unitary transforms with calculations performed using Python 3.9.

In order to compare with classical algorithms, YACCLAB [48] (Yet Another Connected Components Labeling Benchmark) is used, which is an open-source C++ benchmarking framework for component labeling. YACCLAB allows researchers to test classical component labeling algorithms under the same environment and with the same collection of datasets, which provides a rich and varied dataset that includes both synthetic and real images and lots of well-written programs for classical algorithms.

In the experiments, the library Boost.python is used for Python calling C++, and the quantum image component labeling algorithm is compared with CT [49] (Contour Tracing approach), SAUF [50] (Scan plus Array-based Union-Find algorithm), and NULL, which are three classical component labeling types in YACCLAB. The NULL is a fake algorithm that performs the basic assignment operation defining a lower bound limit of the execution time. The experimental results (Figure 17) show that the proposed algorithm is better than the two classical algorithms, and the execution time is the average time for labeling on the image dataset.

To visually present the result of the proposed algorithm, the two images in traffic scenarios are used for testing. The first image is a traffic sign (Figure 18), the size of the image is 256 × 265, the second image (Figure 19) is the license plate held by the author, the size is 654 × 220. In the experiment, we used different colors to distinguish different components. The traffic sign and the license plate have execution time of 0.21 ms and 0.26 ms, respectively. Two tests in the experiment verify the correctness of the quantum image component labeling algorithm in this paper.

**Figure 17.** Experimental comparison of CF, SAUF, and NULL.

**Figure 18.** The example of traffic signs. (**a**) original image. (**b**) binary image. (**c**) component labeling.

**Figure 19.** The example of license plates, "陕" in figure means Shanxi Province. (**a**) original image. (**b**) binary image. (**c**) component labeling.
