## 2 Materials and Methods--Porosity Classification and Fracture Identification
### Calculating Similarity among the Images  
More often than not, it is quintessential to measure the similarity/dissimilarity between different image samples within the dataset under consideration to scrutinize andunderstand the reasons behind every specific unusual or unsatisfactory outcome. The resulting accuracy of various image analysis methods (including deep learning) is often found relying on their adopted learning techniques and might suffer because of the similarity/dissimilarity between the individual image samples in the training and testing sets. For example, a machine vision system fails to recognize a set of cars if it is trained to identify a set of a few specific animals. Apart from that, a reasonable similarity measure between image samples is often considered as one of the essential parts of most of the image classification systems [26], and since, in the current work, our prime objective is to identify the pores and fractures within the grayscale rock images, we have presented our similarity measure as below. The present subsection describes the adopted similarity evaluation technique in detail. We assume \(\mathcal{M}=\{\mathcal{G}_{n}\}\); \(1\leq i\leq N\) is the image dataset, and each image contains \(m\times n\) pixels. Each image of \(\mathcal{M}\) is represented by the pixel intensity vector \(\mathcal{L}^{i}=\{\mathcal{L}^{i}_{p,q}\}\subset\mathbb{R}^{mm}\); \(1\leq p\leq m\); \(1\leq q\leq n\), where \(\mathcal{L}^{i}_{p,q}\in\mathbb{R}\) is the intensity value (scalar) of the pixel (\(p\), \(q\)) of an image \(\mathcal{G}_{i}\), which is evaluated as in Section 2.1, Algorithm 1. Next, our job is to find the \(k\) most similar images to a given image \(\mathcal{G}_{i}\) based on some defined similarity criterion. We further assume that all images have equal prior probability, and the query image (\(\mathcal{G}_{i}\)) is represented by the pixel intensity vector \(x_{i}\). From the probabilistic point of view, each pixel intensity vector contains \(m\times n\) realizations of _i.i.d_ (independent and identically distributed) random variables \(\mathcal{L}^{i}_{1}\);... ; \(\mathcal{L}^{i}_{mm}\), which follow a parametric distribution with probability density function \(p(\mathcal{L}|\theta)\); \(\theta\in\mathbb{R}^{d}\). Given that \(\hat{\theta}\) is a consistent estimator of the parameter vector \(\theta\), it is quite evident that for an image \(\mathcal{G}_{j}\) to be the most similar to \(\mathcal{G}_{i}\), whose parameter vector \(\theta_{j}\) leads to the maximization of the following log-likelihood function, then:  
\[j=\arg\!\max_{\tau}\frac{1}{mn}\sum_{p=1}^{m}\sum_{q=1}^{n}p\Big{(}\mathcal{L} ^{r}_{pq}\Big{|}\theta_{r}\Big{)} \tag{1}\]  
By applying the weak law of large numbers to the Equation (1) with \(m\), \(n\rightarrow\infty\), we obtain the following:  
\[j=\arg\!\max_{\tau}\mathbb{E}_{p(\mathcal{L}|\theta_{i})}\log p(\mathcal{L}| \theta_{r}) \tag{2}\]  
\[=\arg\!\max_{\tau}\int_{L}p(\mathcal{L}|\theta_{i})\log p(\mathcal{L}|\theta_ {i})d\mathcal{L} \tag{3}\]  
where \(\mathbb{E}_{p(\mathcal{L}|\theta_{i})}(\cdot)\) is the expectation with respect to \(p(\mathcal{L}|\theta_{l})\) and \(L\) is the domain of \(p(\mathcal{L}\mid\cdot)\). By observing \(p(\mathcal{L}|\theta_{i})\) as an independent term for the maximization, Equation (3) can be rewritten as follows:  
\[j=\arg\!\min_{\tau}\{-\int_{L}p(\mathcal{L}|\theta_{i})\log p(\mathcal{L}| \theta_{r})d\mathcal{L}\} \tag{4}\]  
\[=\arg\!\min_{\tau}\int_{L}p(\mathcal{L}|\theta_{i})log\frac{p(\mathcal{L}| \theta_{i})}{p(\mathcal{L}|\theta_{r})}d\mathcal{L} \tag{5}\]  
In Equation (5) \(\mathcal{D}_{\mathrm{KL}}(.||. )\) is between \(p(\mathcal{L}|\theta_{i})\) and \(p(\mathcal{L}|\theta_{r})\) or \(\mathrm{D}_{\mathrm{KL}}(p(\mathcal{L}|\theta_{i})\mid\ \ \ p(\mathcal{L}|\theta_{r}))\). Hence, in the asymptotic case, maximum likelihood selection is equivalent to the minimization of the \(\mathcal{D}_{\mathrm{KL}}(.||.