ਕੇ-ਮੀਨਸ ਕਲੱਸਟਰਿੰਗ ਦੇ ਨਾਲ ਚਿੱਤਰ ਵਿਭਾਜਨ ਦੀ ਜਾਣ-ਪਛਾਣ
ਚਿੱਤਰ ਵੰਡਣਾ ਚਿੱਤਰ ਪ੍ਰਕਿਰਿਆ ਵਿੱਚ ਇੱਕ ਮਹੱਤਵਪੂਰਣ ਕਦਮ ਹੈ, ਅਤੇ ਇਹ ਹਰ ਜਗ੍ਹਾ ਲਗਦਾ ਹੈ ਜੇ ਅਸੀਂ ਵਿਸ਼ਲੇਸ਼ਣ ਕਰਨਾ ਚਾਹੁੰਦੇ ਹਾਂ ਕਿ ਚਿੱਤਰ ਦੇ ਅੰਦਰ ਕੀ ਹੈ. ਉਦਾਹਰਣ ਦੇ ਲਈ, ਜੇ ਅਸੀਂ ਕਿਸੇ ਅੰਦਰੂਨੀ ਚਿੱਤਰ ਦੇ ਅੰਦਰ ਕੋਈ ਕੁਰਸੀ ਜਾਂ ਵਿਅਕਤੀ ਲੱਭਣ ਦੀ ਕੋਸ਼ਿਸ਼ ਕਰਦੇ ਹਾਂ, ਤਾਂ ਸਾਨੂੰ ਵਸਤੂਆਂ ਨੂੰ ਵੱਖਰਾ ਕਰਨ ਅਤੇ ਹਰੇਕ ਵਸਤੂ ਦਾ ਵੱਖਰੇ ਤੌਰ 'ਤੇ ਵਿਸ਼ਲੇਸ਼ਣ ਕਰਨ ਲਈ ਚਿੱਤਰ ਦੇ ਵਿਭਾਜਨ ਦੀ ਜ਼ਰੂਰਤ ਹੋ ਸਕਦੀ ਹੈ ਕਿ ਇਹ ਕੀ ਹੈ. ਚਿੱਤਰ ਵਿਭਾਜਨ ਆਮ ਤੌਰ ਤੇ ਪੈਟਰਨ ਪਛਾਣ, ਵਿਸ਼ੇਸ਼ਤਾ ਕੱctionਣ ਅਤੇ ਚਿੱਤਰ ਦੇ ਸੰਕੁਚਨ ਤੋਂ ਪਹਿਲਾਂ ਪੂਰਵ-ਪ੍ਰਕਿਰਿਆ ਦੇ ਤੌਰ ਤੇ ਕੰਮ ਕਰਦਾ ਹੈ.
ਚਿੱਤਰ ਵਿਭਾਜਨ ਇੱਕ ਚਿੱਤਰ ਦਾ ਵੱਖ -ਵੱਖ ਸਮੂਹਾਂ ਵਿੱਚ ਵਰਗੀਕਰਨ ਹੈ. ਕਲਸਟਰਿੰਗ ਦੀ ਵਰਤੋਂ ਕਰਦੇ ਹੋਏ ਚਿੱਤਰ ਵਿਭਾਜਨ ਦੇ ਖੇਤਰ ਵਿੱਚ ਬਹੁਤ ਸਾਰੀਆਂ ਕਿਸਮਾਂ ਦੀ ਖੋਜ ਕੀਤੀ ਗਈ ਹੈ. ਇੱਥੇ ਵੱਖੋ ਵੱਖਰੇ ਤਰੀਕੇ ਹਨ ਅਤੇ ਸਭ ਤੋਂ ਮਸ਼ਹੂਰ ਤਰੀਕਿਆਂ ਵਿੱਚੋਂ ਇੱਕ ਹੈ K- ਮਤਲਬ ਕਲੱਸਟਰਿੰਗ ਐਲਗੋਰਿਦਮ .
ਇਸ ਲਈ ਇੱਥੇ ਇਸ ਲੇਖ ਵਿੱਚ, ਅਸੀਂ ਇੱਕ ਚਿੱਤਰ ਨੂੰ ਪੜ੍ਹਨ ਅਤੇ ਚਿੱਤਰ ਦੇ ਵੱਖ ਵੱਖ ਖੇਤਰਾਂ ਨੂੰ ਕਲੱਸਟਰ ਕਰਨ ਦੇ ਇੱਕ exploreੰਗ ਦੀ ਪੜਚੋਲ ਕਰਾਂਗੇ. ਪਰ ਕਰਨ ਤੋਂ ਪਹਿਲਾਂ ਪਹਿਲਾਂ ਇਸ ਬਾਰੇ ਗੱਲ ਕਰੀਏ:
- ਚਿੱਤਰ ਵਿਭਾਜਨ
- ਚਿੱਤਰ ਵਿਭਾਜਨ ਕਿਵੇਂ ਕੰਮ ਕਰਦਾ ਹੈ
- ਕੇ-ਮੀਨਸ ਕਲੱਸਟਰਿੰਗ ਐਮਐਲ ਐਲਗੋਰਿਦਮ
- ਚਿੱਤਰ ਵਿਭਾਜਨ ਦੇ ਨਾਲ ਕੇ-ਮੀਨਸ ਕਲਸਟਰਿੰਗ ਐਲਗੋਰਿਦਮ ਨੂੰ ਮਿਲਾਓ.
- ਕੈਨੀ ਐਜ ਖੋਜ
ਚਿੱਤਰ ਵਿਭਾਜਨ
laravel 8 auth ਟਿਊਟੋਰਿਅਲ
ਚਿੱਤਰ ਵਿਭਾਜਨ ਇੱਕ ਡਿਜੀਟਲ ਚਿੱਤਰ ਨੂੰ ਵੱਖੋ ਵੱਖਰੇ ਖੇਤਰਾਂ ਵਿੱਚ ਵੰਡਣ ਦੀ ਪ੍ਰਕਿਰਿਆ ਹੈ ਜਿਸ ਵਿੱਚ ਹਰੇਕ ਪਿਕਸਲ (ਪਿਕਸਲ ਦੇ ਸਮੂਹ, ਜਿਨ੍ਹਾਂ ਨੂੰ ਸੁਪਰਪਿਕਸਲ ਵੀ ਕਿਹਾ ਜਾਂਦਾ ਹੈ) ਸਮਾਨ ਵਿਸ਼ੇਸ਼ਤਾਵਾਂ ਵਾਲੇ ਹੁੰਦੇ ਹਨ.
ਚਿੱਤਰ ਵਿਭਾਜਨ ਦਾ ਉਦੇਸ਼ ਕਿਸੇ ਚਿੱਤਰ ਦੀ ਪ੍ਰਤੀਨਿਧਤਾ ਨੂੰ ਅਜਿਹੀ ਚੀਜ਼ ਵਿੱਚ ਬਦਲਣਾ ਹੈ ਜੋ ਵਧੇਰੇ ਅਰਥਪੂਰਨ ਅਤੇ ਵਿਸ਼ਲੇਸ਼ਣ ਵਿੱਚ ਅਸਾਨ ਹੋਵੇ.
ਚਿੱਤਰ ਵਿਭਾਜਨ ਆਮ ਤੌਰ ਤੇ ਵਸਤੂਆਂ ਦਾ ਪਤਾ ਲਗਾਉਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ ਅਤੇ ਹੱਦਾਂ (ਰੇਖਾਵਾਂ, ਕਰਵ, ਆਦਿ) ਚਿੱਤਰਾਂ ਵਿੱਚ. ਵਧੇਰੇ ਸੰਖੇਪ ਵਿੱਚ, ਚਿੱਤਰ ਵਿਭਾਜਨ ਇੱਕ ਚਿੱਤਰ ਦੇ ਹਰੇਕ ਪਿਕਸਲ ਨੂੰ ਲੇਬਲ ਨਿਰਧਾਰਤ ਕਰਨ ਦੀ ਪ੍ਰਕਿਰਿਆ ਹੈ ਜਿਵੇਂ ਕਿ ਉਸੇ ਲੇਬਲ ਵਾਲੇ ਪਿਕਸਲ ਕੁਝ ਵਿਸ਼ੇਸ਼ਤਾਵਾਂ ਨੂੰ ਸਾਂਝਾ ਕਰਦੇ ਹਨ.
ਬੇਸ਼ੱਕ, ਇੱਕ ਆਮ ਪ੍ਰਸ਼ਨ ਉੱਠਦਾ ਹੈ:
ਚਿੱਤਰ ਵੰਡਣ ਨਾਲ ਵੀ ਕੋਈ ਫ਼ਰਕ ਕਿਉਂ ਪੈਂਦਾ ਹੈ?
ਜੇ ਅਸੀਂ ਖੁਦਮੁਖਤਿਆਰ ਵਾਹਨਾਂ ਦੀ ਉਦਾਹਰਣ ਲੈਂਦੇ ਹਾਂ, ਤਾਂ ਉਨ੍ਹਾਂ ਨੂੰ ਡਿਜੀਟਲ ਨਕਸ਼ਾ ਬਣਾਉਣ ਦੇ ਨਾਲ, ਕਾਰ ਨੂੰ ਆਪਣੇ ਆਲੇ ਦੁਆਲੇ ਦੇ ਸੰਸਾਰ ਨੂੰ ਵੇਖਣ ਦੀ ਆਗਿਆ ਦੇਣ ਲਈ ਕੈਮਰੇ, ਰਾਡਾਰ ਅਤੇ ਲੇਜ਼ਰਸ ਵਰਗੇ ਸੰਵੇਦੀ ਇਨਪੁਟ ਉਪਕਰਣਾਂ ਦੀ ਜ਼ਰੂਰਤ ਹੁੰਦੀ ਹੈ. ਆਬਜੈਕਟ ਦੀ ਖੋਜ ਕੀਤੇ ਬਗੈਰ ਆਟੋਨੋਮਸ ਡਰਾਈਵਿੰਗ ਸੰਭਵ ਨਹੀਂ ਹੈ ਜਿਸ ਵਿੱਚ ਖੁਦ ਚਿੱਤਰ ਵਰਗੀਕਰਨ/ਵਿਭਾਜਨ ਸ਼ਾਮਲ ਹੁੰਦਾ ਹੈ.
ਆਟੋਨੋਮਸ ਵਾਹਨ ਦੁਆਰਾ ਆਬਜੈਕਟ ਖੋਜ ਅਤੇ ਚਿੱਤਰ ਵਰਗੀਕਰਣ
ਹੋਰ ਉਦਾਹਰਣਾਂ ਵਿੱਚ ਹੈਲਥਕੇਅਰ ਇੰਡਸਟਰੀ ਸ਼ਾਮਲ ਹੈ ਜਿੱਥੇ ਜੇ ਅਸੀਂ ਕੈਂਸਰ ਬਾਰੇ ਗੱਲ ਕਰਦੇ ਹਾਂ, ਇੱਥੋਂ ਤਕ ਕਿ ਅੱਜ ਦੇ ਤਕਨੀਕੀ ਵਿਕਾਸ ਦੇ ਯੁੱਗ ਵਿੱਚ ਵੀ, ਕੈਂਸਰ ਘਾਤਕ ਹੋ ਸਕਦਾ ਹੈ ਜੇ ਅਸੀਂ ਇਸਨੂੰ ਸ਼ੁਰੂਆਤੀ ਪੜਾਅ 'ਤੇ ਨਹੀਂ ਪਛਾਣਦੇ. ਕੈਂਸਰ ਵਾਲੇ ਸੈੱਲਾਂ ਦਾ ਜਿੰਨੀ ਜਲਦੀ ਸੰਭਵ ਹੋ ਸਕੇ ਖੋਜ ਕਰਨਾ ਲੱਖਾਂ ਲੋਕਾਂ ਦੀ ਜਾਨ ਬਚਾ ਸਕਦਾ ਹੈ. ਕੈਂਸਰ ਦੇ ਸੈੱਲਾਂ ਦੀ ਸ਼ਕਲ ਕੈਂਸਰ ਦੀ ਗੰਭੀਰਤਾ ਨੂੰ ਨਿਰਧਾਰਤ ਕਰਨ ਵਿੱਚ ਮਹੱਤਵਪੂਰਣ ਭੂਮਿਕਾ ਅਦਾ ਕਰਦੀ ਹੈ ਜਿਸਦੀ ਪਛਾਣ ਚਿੱਤਰ ਵਰਗੀਕਰਣ ਐਲਗੋਰਿਦਮ ਦੀ ਵਰਤੋਂ ਨਾਲ ਕੀਤੀ ਜਾ ਸਕਦੀ ਹੈ.
ਇਸ ਤਰ੍ਹਾਂ, ਕਈ ਖਾਸ ਐਲਗੋਰਿਦਮ ਸਨ ਅਤੇ ਇਮੇਜ ਸੈਗਮੈਂਟੇਸ਼ਨ ਦੀਆਂ ਤਕਨੀਕਾਂ ਸਾਲਾਂ ਤੋਂ ਡੋਮੇਨ-ਵਿਸ਼ੇਸ਼ ਗਿਆਨ ਦੀ ਵਰਤੋਂ ਕਰਦੇ ਹੋਏ ਉਸ ਖਾਸ ਐਪਲੀਕੇਸ਼ਨ ਖੇਤਰ ਵਿੱਚ ਵਿਭਾਜਨ ਸਮੱਸਿਆਵਾਂ ਨੂੰ ਪ੍ਰਭਾਵਸ਼ਾਲੀ solveੰਗ ਨਾਲ ਹੱਲ ਕਰਨ ਲਈ ਵਿਕਸਤ ਕੀਤੀਆਂ ਗਈਆਂ ਹਨ ਜਿਸ ਵਿੱਚ ਸ਼ਾਮਲ ਹਨ ਮੈਡੀਕਲ ਇਮੇਜਿੰਗ , ਵਸਤੂ ਖੋਜ , ਆਇਰਿਸ ਦੀ ਮਾਨਤਾ , ਵੀਡੀਓ ਨਿਗਰਾਨੀ , ਮਸ਼ੀਨ ਦਰਸ਼ਨ ਅਤੇ ਹੋਰ ਬਹੁਤ ਸਾਰੇ….
ਆਓ ਪਾਈਥਨ ਮੈਟਪਲਾਟਲੀਬ ਲਾਇਬ੍ਰੇਰੀ ਦੀ ਵਰਤੋਂ ਕਰਦਿਆਂ 3 ਡੀ ਸਪੇਸ ਵਿੱਚ ਇੱਕ ਚਿੱਤਰ ਤਿਆਰ ਕਰੀਏ.
ਹੇਠਾਂ ਉਹ ਚਿੱਤਰ ਹੈ ਜੋ ਅਸੀਂ 3 ਡੀ ਸਪੇਸ ਵਿੱਚ ਪਲਾਟ ਕਰਾਂਗੇ ਅਤੇ ਅਸੀਂ 3 ਵੱਖਰੇ ਰੰਗਾਂ ਨੂੰ ਸਪਸ਼ਟ ਤੌਰ ਤੇ ਵੇਖ ਸਕਦੇ ਹਾਂ ਜਿਸਦਾ ਅਰਥ ਹੈ ਕਿ 3 ਸਮੂਹ/ਸਮੂਹ ਤਿਆਰ ਕੀਤੇ ਜਾਣੇ ਚਾਹੀਦੇ ਹਨ.
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import cv2img = cv2.imread('/Users/nageshsinghchauhan/Documents/images10.jpg'> From the plot one can easily see that the data points are forming groups — some places in a graph are more dense, which we can think as different colors’ dominance on the image.
How Image Segmentation works
Image Segmentation involves converting an image into a collection of regions of pixels that are represented by a mask or a labeled image. By dividing an image into segments, you can process only the important segments of the image instead of processing the entire image.
A common technique is to look for abrupt discontinuities in pixel values, which typically indicate edges that define a region.
Another common approach is to detect similarities in the regions of an image. Some techniques that follow this approach are region growing, clustering, and thresholding.
A variety of other approaches to perform image segmentation have been developed over the years using domain-specific knowledge to effectively solve segmentation problems in specific application areas.
So let us start with one of the clustering-based approaches in Image Segmentation which is K-Means clustering.
K-Means clustering algorithm
Ok first What are Clustering algorithms in Machine Learning?
Clustering algorithms are unsupervised algorithms but are similar to Classification algorithms but the basis is different.
In Clustering, you don’t know what you are looking for, and you are trying to identify some segments or clusters in your data. When you use clustering algorithms in your dataset, unexpected things can suddenly pop-up like structures, clusters, and groupings you would have never thought otherwise.
K -Means clustering algorithm is an unsupervised algorithm and it is used to segment the interest area from the background. It clusters, or partitions the given data into K-clusters or parts based on the K-centroids.
The algorithm is used when you have unlabeled data(i.e. data without defined categories or groups). The goal is to find certain groups based on some kind of similarity in the data with the number of groups represented by K.

In the above figure, Customers of a shopping mall have been grouped into 5 clusters based on their income and spending score. Yellow dots represent the Centroid of each cluster.
The objective of K-Means clustering is to minimize the sum of squared distances between all points and the cluster center.

Steps in K-Means algorithm: 1. Choose the number of clusters K.
2. Select at random K points, the centroids(not necessarily from your dataset).
3. Assign each data point to the closest centroid → that forms K clusters.
4. Compute and place the new centroid of each cluster.
5. Reassign each data point to the new closest centroid. If any reassignment . took place, go to step 4, otherwise, the model is ready.
How to choose the optimal value of K?
For a certain class of clustering algorithms (in particular K-Means, K -medoids , and expectation-maximization algorithm), there is a parameter commonly referred to as K that specifies the number of clusters to detect. Other algorithms such as DBSCAN and OPTICS algorithm do not require the specification of this parameter; Hierarchical Clustering avoids the problem altogether but that’s beyond the scope of this article.
If we talk about K-Means then the correct choice of K is often ambiguous, with interpretations depending on the shape and scale of the distribution of points in a data set and the desired clustering resolution of the user. In addition, increasing K without penalty will always reduce the amount of error in the resulting clustering, to the extreme case of zero error if each data point is considered its own cluster (i.e., when K equals the number of data points, n ). Intuitively then, the optimal choice of K will strike a balance between maximum compression of the data using a single cluster, and maximum accuracy by assigning each data point to its own cluster .
If an appropriate value of K is not apparent from prior knowledge of the properties of the data set, it must be chosen somehow. There are several categories of methods for making this decision and Elbow method is one such method.
Elbow method
The basic idea behind partitioning methods, such as K-Means clustering, is to define clusters such that the total intra-cluster variation or in other words, total within-cluster sum of square (WCSS) is minimized. The total WCSS measures the compactness of the clustering and we want it to be as small as possible.

The Elbow method looks at the total WCSS as a function of the number of clusters: One should choose a number of clusters so that adding another cluster doesn’t improve much better the total WCSS.
Steps to choose the optimal number of clusters K:(Elbow Method) 1. Compute K-Means clustering for different values of K by varying K from 1 to 10 clusters.
2. For each K, calculate the total within-cluster sum of square (WCSS).
3. Plot the curve of WCSS vs the number of clusters K.
4. The location of a bend (knee) in the plot is generally considered as an indicator of the appropriate number of clusters.
There is a catch!!!
In spite of all the advantages K-Means have got but it fails sometimes due to the random choice of centroids which is called The Random Initialization Trap.
To solve this issue we have an initialization procedure for K-Means which is called K-Means++ (Algorithm for choosing the initial values for K-Means clustering).
In K-Means++, We pick a point randomly and that’s your first centroid, then we pick the next point based on the probability that depends upon the distance of the first point, the further apart the point is the more probable it is.
Then we have two centroids, repeat the process, the probability of each point is based on its distance to the closest centroid to that point. Now, this introduces an overhead in the initialization of the algorithm, but it reduces the probability of a bad initialization leading to bad clustering result.
ਵੀਜ਼ਾ ਪ੍ਰੋਵਿਜ਼ਨਿੰਗ ਸਾਡੀ ਸੇਵਾ
Visual Representation of K-Means Clustering: Starting with 4 leftmost points.

Enough of theory lets implement what we have discussed in a real-world scenario.
In this section, we will explore a method to read an image and cluster different regions of the image using the K-Means clustering algorithm and OpenCV .
So basically we will perform Color clustering and Canny Edge detection.
Color Clustering:
Load all the required libraries:
import numpy as np import cv2 import matplotlib.pyplot as plt
ਅਗਲਾ ਕਦਮ ਹੈ ਚਿੱਤਰ ਨੂੰ ਆਰਜੀਬੀ ਕਲਰ ਸਪੇਸ ਵਿੱਚ ਲੋਡ ਕਰਨਾ
original_image = cv2.imread('/Users/nageshsinghchauhan/Desktop/image1.jpg'>
We need to convert our image from RGB Colours Space to HSV to work ahead. But the question is why ??
According to wikipedia the R, G, and B components of an object’s color in a digital image are all correlated with the amount of light hitting the object, and therefore with each other, image descriptions in terms of those components make object discrimination difficult. Descriptions in terms of hue/lightness/chroma or hue/lightness/saturation are often more relevant.
If you don’t convert your image to HSV, your image will look something like this:

img=cv2.cvtColor(original_image,cv2.COLOR_BGR2RGB)
ਅੱਗੇ, MxNx3 ਚਿੱਤਰ ਨੂੰ ਇੱਕ Kx3 ਮੈਟ੍ਰਿਕਸ ਵਿੱਚ ਬਦਲਦਾ ਹੈ ਜਿੱਥੇ K = MxN ਅਤੇ ਹਰ ਕਤਾਰ ਹੁਣ RGB ਦੇ 3-D ਸਪੇਸ ਵਿੱਚ ਇੱਕ ਵੈਕਟਰ ਹੈ.
vectorized = img.reshape((-1,3))
ਅਸੀਂ ਯੂਨਿਟ 8 ਦੇ ਮੁੱਲ ਨੂੰ ਫਲੋਟ ਵਿੱਚ ਬਦਲਦੇ ਹਾਂ ਕਿਉਂਕਿ ਇਹ ਓਪਨਸੀਵੀ ਦੇ ਕੇ-ਮੀਨਸ ਵਿਧੀ ਦੀ ਜ਼ਰੂਰਤ ਹੈ.
vectorized = np.float32(vectorized)
ਅਸੀਂ k = 3 ਦੇ ਨਾਲ ਕਲੱਸਟਰ ਕਰਨ ਜਾ ਰਹੇ ਹਾਂ ਕਿਉਂਕਿ ਜੇ ਤੁਸੀਂ ਉਪਰੋਕਤ ਚਿੱਤਰ ਨੂੰ ਵੇਖਦੇ ਹੋ ਤਾਂ ਇਸਦੇ 3 ਰੰਗ ਹਨ, ਹਰੇ ਰੰਗ ਦਾ ਘਾਹ ਅਤੇ ਜੰਗਲ, ਨੀਲਾ ਸਮੁੰਦਰ ਅਤੇ ਹਰੇ-ਨੀਲੇ ਸਮੁੰਦਰੀ ਕੰoreੇ.
ਮਾਪਦੰਡ ਨਿਰਧਾਰਤ ਕਰੋ, ਸਮੂਹਾਂ ਦੀ ਗਿਣਤੀ (ਕੇ) ਅਤੇ ਕੇ-ਮੀਨਸ () ਲਾਗੂ ਕਰੋ
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ਓਪਨਸੀਵੀ ਪ੍ਰਦਾਨ ਕਰਦਾ ਹੈ cv2.kmeans ( ਨਮੂਨੇ, nclusters (K), ਮਾਪਦੰਡ, ਕੋਸ਼ਿਸ਼ਾਂ, ਝੰਡੇ ) ਰੰਗ ਕਲੱਸਟਰਿੰਗ ਲਈ ਫੰਕਸ਼ਨ.
- ਨਮੂਨੇ: ਇਸ ਦਾ ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ ਜਿਵੇਂ ਕਿ ਫਲੋਟ 32 ਡਾਟਾ ਕਿਸਮ, ਅਤੇ ਹਰੇਕ ਵਿਸ਼ੇਸ਼ਤਾ ਨੂੰ ਇੱਕ ਸਿੰਗਲ ਕਾਲਮ ਵਿੱਚ ਪਾਉਣਾ ਚਾਹੀਦਾ ਹੈ.
2. nclusters (ਕੇ) : ਅੰਤ ਵਿੱਚ ਲੋੜੀਂਦੇ ਸਮੂਹਾਂ ਦੀ ਸੰਖਿਆ
3. ਮਾਪਦੰਡ: ਇਹ ਦੁਹਰਾਓ ਸਮਾਪਤੀ ਮਾਪਦੰਡ ਹੈ. ਜਦੋਂ ਇਹ ਮਾਪਦੰਡ ਸੰਤੁਸ਼ਟ ਹੋ ਜਾਂਦਾ ਹੈ, ਐਲਗੋਰਿਦਮ ਦੁਹਰਾਉਣਾ ਬੰਦ ਹੋ ਜਾਂਦਾ ਹੈ. ਅਸਲ ਵਿੱਚ, ਇਹ 3 ਪੈਰਾਮੀਟਰਾਂ ਦਾ ਇੱਕ ਟੁਪਲ ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ. ਉਹ ਹਨ | _+_ |:
ਸਮਾਪਤੀ ਮਾਪਦੰਡ ਦੀ ਕਿਸਮ. ਇਸ ਦੇ ਹੇਠਾਂ 3 ਝੰਡੇ ਹਨ:
- cv.TERM_CRITERIA_EPS - ਜੇ ਨਿਰਧਾਰਤ ਸ਼ੁੱਧਤਾ, ਐਲਗੋਰਿਦਮ ਦੀ ਦੁਹਰਾਓ ਨੂੰ ਰੋਕੋ, ਐਪੀਸਿਲਨ , ਪਹੁੰਚ ਗਿਆ ਹੈ.
- cv.TERM_CRITERIA_MAX_ITER - ਨਿਰਧਾਰਤ ਸੰਖਿਆਵਾਂ ਦੇ ਬਾਅਦ ਐਲਗੋਰਿਦਮ ਨੂੰ ਰੋਕੋ, ਅਧਿਕਤਮ_ਇਟਰ .
- cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER - ਉਪਰੋਕਤ ਸ਼ਰਤਾਂ ਵਿੱਚੋਂ ਕੋਈ ਵੀ ਪੂਰਾ ਹੋਣ 'ਤੇ ਦੁਹਰਾਉ ਨੂੰ ਰੋਕੋ.
4. ਕੋਸ਼ਿਸ਼ਾਂ: ਵੱਖ -ਵੱਖ ਸ਼ੁਰੂਆਤੀ ਲੇਬਲਿੰਗਸ ਦੀ ਵਰਤੋਂ ਕਰਦੇ ਹੋਏ ਐਲਗੋਰਿਦਮ ਨੂੰ ਕਿੰਨੀ ਵਾਰ ਚਲਾਇਆ ਜਾਂਦਾ ਹੈ ਨੂੰ ਨਿਰਧਾਰਤ ਕਰਨ ਲਈ ਫਲੈਗ ਕਰੋ. ਐਲਗੋਰਿਦਮ ਉਨ੍ਹਾਂ ਲੇਬਲਾਂ ਨੂੰ ਵਾਪਸ ਕਰਦਾ ਹੈ ਜੋ ਵਧੀਆ ਸੰਕੁਚਿਤਤਾ ਪ੍ਰਦਾਨ ਕਰਦੇ ਹਨ. ਇਹ ਸੰਕੁਚਿਤਤਾ ਆਉਟਪੁੱਟ ਦੇ ਰੂਪ ਵਿੱਚ ਵਾਪਸ ਕੀਤੀ ਜਾਂਦੀ ਹੈ.
5. ਝੰਡੇ: ਇਹ ਝੰਡਾ ਇਹ ਦੱਸਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ ਕਿ ਸ਼ੁਰੂਆਤੀ ਕੇਂਦਰ ਕਿਵੇਂ ਲਏ ਜਾਂਦੇ ਹਨ. ਆਮ ਤੌਰ ਤੇ ਇਸਦੇ ਲਈ ਦੋ ਝੰਡੇ ਵਰਤੇ ਜਾਂਦੇ ਹਨ: cv.KMEANS_PP_CENTERS ਅਤੇ cv.KMEANS_RANDOM_CENTERS .
( type, max_iter, epsilon )
ਹੁਣ ਵਾਪਸ uint8 ਵਿੱਚ ਬਦਲੋ.
K = 3 attempts=10 ret,label,center=cv2.kmeans(vectorized,K,None,criteria,attempts,cv2.KMEANS_PP_CENTERS)
ਅੱਗੇ, ਸਾਨੂੰ ਕਲੱਸਟਰਡ ਚਿੱਤਰ ਨੂੰ ਦੁਬਾਰਾ ਬਣਾਉਣ ਲਈ ਲੇਬਲਾਂ ਤੱਕ ਪਹੁੰਚ ਕਰਨੀ ਪਏਗੀ
center = np.uint8(center)
| _+_ | ਫਰੇਮ ਦਾ ਨਤੀਜਾ ਹੈ ਜਿਸ ਵਿੱਚ k- ਮਤਲਬ ਕਲੱਸਟਰਿੰਗ ਹੋਈ ਹੈ.
ਹੁਣ ਆਓ ਆਉਟਪੁਟ ਨਤੀਜਾ K = 3 ਨਾਲ ਵੇਖੀਏ
res = center[label.flatten()] result_image = res.reshape((img.shape))

ਇਸ ਲਈ ਐਲਗੋਰਿਦਮ ਨੇ ਸਾਡੀ ਮੂਲ ਤਸਵੀਰ ਨੂੰ ਤਿੰਨ ਪ੍ਰਮੁੱਖ ਰੰਗਾਂ ਵਿੱਚ ਸ਼੍ਰੇਣੀਬੱਧ ਕੀਤਾ ਹੈ.
ਫਾਇਰਫਾਕਸ ਗੂਗਲ ਨੂੰ ਲੋਡ ਨਹੀਂ ਕਰੇਗਾ
ਆਓ ਦੇਖੀਏ ਕਿ ਕੀ ਹੁੰਦਾ ਹੈ ਜਦੋਂ ਅਸੀਂ K = 5 ਦਾ ਮੁੱਲ ਬਦਲਦੇ ਹਾਂ:

K = 7 ਦਾ ਮੁੱਲ ਬਦਲੋ:

ਜਿਵੇਂ ਕਿ ਤੁਸੀਂ ਕੇ ਦੇ ਮੁੱਲ ਵਿੱਚ ਵਾਧੇ ਦੇ ਨਾਲ ਵੇਖ ਸਕਦੇ ਹੋ, ਚਿੱਤਰ ਸਪੱਸ਼ਟ ਹੋ ਜਾਂਦਾ ਹੈ ਕਿਉਂਕਿ ਕੇ-ਮਤਲਬ ਐਲਗੋਰਿਦਮ ਰੰਗਾਂ ਦੇ ਵਧੇਰੇ ਵਰਗਾਂ/ਸਮੂਹਾਂ ਨੂੰ ਸ਼੍ਰੇਣੀਬੱਧ ਕਰ ਸਕਦਾ ਹੈ.
ਅਸੀਂ ਵੱਖਰੇ ਚਿੱਤਰਾਂ ਲਈ ਆਪਣਾ ਕੋਡ ਅਜ਼ਮਾ ਸਕਦੇ ਹਾਂ:


ਆਓ ਆਪਣੇ ਅਗਲੇ ਭਾਗ ਵੱਲ ਚਲੀਏ ਜੋ ਕਿ ਕੈਨੀ ਐਜ ਡਿਟੈਕਸ਼ਨ ਹੈ.
ਕੈਨੀ ਐਜ ਦੀ ਖੋਜ: ਇਹ ਇੱਕ ਚਿੱਤਰ ਪ੍ਰੋਸੈਸਿੰਗ ਵਿਧੀ ਹੈ ਜੋ ਸ਼ੋਰ ਨੂੰ ਦਬਾਉਂਦੇ ਹੋਏ ਇੱਕ ਚਿੱਤਰ ਦੇ ਕਿਨਾਰਿਆਂ ਦਾ ਪਤਾ ਲਗਾਉਣ ਲਈ ਵਰਤੀ ਜਾਂਦੀ ਹੈ.
ਕੈਨੀ ਐਜ ਡਿਟੈਕਸ਼ਨ ਐਲਗੋਰਿਦਮ 5 ਕਦਮਾਂ ਨਾਲ ਬਣਿਆ ਹੈ: 1. ਸ਼ੋਰ ਘਟਾਉਣਾ
2. ਗਰੇਡੀਐਂਟ ਗਣਨਾ
3. ਗੈਰ-ਅਧਿਕਤਮ ਦਮਨ
4. ਡਬਲ ਥ੍ਰੈਸ਼ਹੋਲਡ
5. ਹਿਸਟਰੇਸਿਸ ਦੁਆਰਾ ਐਜ ਟ੍ਰੈਕਿੰਗ
ਓਪਨਸੀਵੀ ਪ੍ਰਦਾਨ ਕਰਦਾ ਹੈ cv2. ਕੈਨੀ (ਚਿੱਤਰ, ਥ੍ਰੈਸ਼ਹੋਲਡ 1, ਥ੍ਰੈਸ਼ਹੋਲਡ 2) ਕਿਨਾਰੇ ਦੀ ਖੋਜ ਲਈ ਕਾਰਜ.
ਪਹਿਲੀ ਦਲੀਲ ਸਾਡੀ ਇਨਪੁਟ ਤਸਵੀਰ ਹੈ. ਦੂਜੀ ਅਤੇ ਤੀਜੀ ਦਲੀਲ ਕ੍ਰਮਵਾਰ ਸਾਡੀ ਘੱਟੋ ਘੱਟ ਅਤੇ ਅਧਿਕਤਮ ਸੀਮਾ ਹੈ.
ਫੰਕਸ਼ਨ ਇਨਪੁਟ ਚਿੱਤਰ (8-ਬਿੱਟ ਇਨਪੁਟ ਚਿੱਤਰ) ਵਿੱਚ ਕਿਨਾਰੇ ਲੱਭਦਾ ਹੈ ਅਤੇ ਉਨ੍ਹਾਂ ਨੂੰ ਕੈਨੀ ਐਲਗੋਰਿਦਮ ਦੀ ਵਰਤੋਂ ਕਰਦਿਆਂ ਆਉਟਪੁੱਟ ਨਕਸ਼ੇ ਦੇ ਕਿਨਾਰਿਆਂ ਤੇ ਨਿਸ਼ਾਨ ਲਗਾਉਂਦਾ ਹੈ. ਥ੍ਰੈਸ਼ਹੋਲਡ 1 ਅਤੇ ਥ੍ਰੈਸ਼ਹੋਲਡ 2 ਦੇ ਵਿਚਕਾਰ ਸਭ ਤੋਂ ਛੋਟਾ ਮੁੱਲ ਕਿਨਾਰੇ ਨੂੰ ਜੋੜਨ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ. ਸਭ ਤੋਂ ਵੱਡਾ ਮੁੱਲ ਮਜ਼ਬੂਤ ਕਿਨਾਰਿਆਂ ਦੇ ਸ਼ੁਰੂਆਤੀ ਹਿੱਸਿਆਂ ਨੂੰ ਲੱਭਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ.
result_image

ਨਤੀਜਾ -1: ਕੈਨੀ ਐਲਗੋਰਿਦਮ ਦੀ ਵਰਤੋਂ ਕਰਦਿਆਂ ਕਿਨਾਰੇ ਦੀ ਖੋਜ!