Skip to content

Commit

Permalink
Create a key point clustering function
Browse files Browse the repository at this point in the history
Create a key point clustering function. This is necessary to find the area of concentration of points using K-means, the algorithm of which is based on the assumption that descriptors that are close to each other in the feature space represent similar key points. Thus, K-means searches for groups of descriptors that have similar meanings.

Conclusion:  group key points based on their descriptors
  • Loading branch information
Иван Горбунов committed Dec 25, 2024
1 parent 97b3e57 commit 9b47af7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ jobs:
done < requirements.txt
fi
python -m pip install --editable .
- name: List installed packages
run: |
pip list
- name: Lint with flake8
run: |
Expand Down
47 changes: 36 additions & 11 deletions src/pygats/recog.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ def find_fuzzy_text(recognized_list, search: str):
search (str): substring to search
Returns:
(roi,text, substring):
roi(ROI): region of interest
(roi, text, substring):
roi (ROI): region of interest
text (str): full text which resides in rectangle
"""
result = []
Expand Down Expand Up @@ -418,8 +418,8 @@ def find_regexp_text(recognized_list: list, pattern):
pattern (str): regexp pattern to match
Returns:
(roi,text, substring):
roi(ROI): region of interest
(roi, text, substring):
roi (ROI): region of interest
text (str): full text which resides in rectangle
substring (str): substring found in text
"""
Expand All @@ -431,16 +431,41 @@ def find_regexp_text(recognized_list: list, pattern):
return list(set(result))


def find_key_points(image: str):
"""Search for key points in the image
def find_keypoints(image: str):
"""Detects key points and calculates their
descriptors in a given image using the SIFT algorithm
Args:
image (str): image where you need to identify areas with key points
Returns:
(keypoints, descriptors):
keypoints (list): A list of key points found in the image
descriptors (float32): An array of descriptors in float32 format
"""
img = cv.imread(image)
sift = cv.SIFT_create()
keypoints, _ = sift.detectAndCompute(img, None)
image_with_sift = cv.drawKeypoints(img, keypoints, None)
plt.imshow(cv.cvtColor(image_with_sift, cv.COLOR_BGR2RGB))
plt.title('Key points')
plt.show()
keypoints, descriptors = sift.detectAndCompute(img, None)
descriptors = np.float32(descriptors)
return keypoints, descriptors


def clust_keypoints(keypoints, descriptors, clust_count: int):
"""The function clusters descriptors of key points
found in the image using the K-means algorithm
Args:
keypoints (list): A list of key points found in the image
descriptors (float32): An array of descriptors in float32 format
clust_count (int): Number of clusters for clustering key points
Returns:
(labels, centers):
labels (cv2.typing.MatLike): This is the label array where each element marked '0', '1'...
centers (cv2.typing.MatLike): This is array of centers of clusters.
"""
cluster = clust_count
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.2)
_, labels, centers = cv.kmeans(descriptors, cluster, None, criteria, 10, cv.KMEANS_RANDOM_CENTERS)
print(labels)
return labels, centers

0 comments on commit 9b47af7

Please sign in to comment.