Update cv.py
Browse files
cv.py
CHANGED
|
@@ -1,46 +1,45 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
# Read the image
|
| 6 |
image = cv2.imread(image_path)
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Find
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Filter
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Example usage
|
| 33 |
-
image_path =
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
import matplotlib.pyplot as plt
|
| 39 |
-
import cv2
|
| 40 |
-
|
| 41 |
-
def display_image(image):
|
| 42 |
-
plt.imshow(image)
|
| 43 |
-
plt.axis('off') # Hide axis
|
| 44 |
-
plt.show()
|
| 45 |
-
|
| 46 |
-
display_image(gray)
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
+
from sklearn.neighbors import NearestNeighbors
|
| 4 |
|
| 5 |
+
def detect_back_patches(image_path, threshold_distance):
|
| 6 |
# Read the image
|
| 7 |
image = cv2.imread(image_path)
|
| 8 |
+
|
| 9 |
+
# Convert image to grayscale if necessary
|
| 10 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 11 |
+
|
| 12 |
+
# Apply nearest neighbor algorithm
|
| 13 |
+
# Assuming patches are represented as feature vectors
|
| 14 |
+
patches = extract_patches(gray_image) # Extract patches from the image
|
| 15 |
+
nn = NearestNeighbors(n_neighbors=2, algorithm='auto')
|
| 16 |
+
nn.fit(patches)
|
| 17 |
+
|
| 18 |
+
# Find nearest neighbors
|
| 19 |
+
distances, indices = nn.kneighbors(patches)
|
| 20 |
+
|
| 21 |
+
# Filter patches based on threshold distance
|
| 22 |
+
filtered_patches_indices = np.where(distances[:,1] > threshold_distance)[0]
|
| 23 |
+
|
| 24 |
+
# Get the ROIs corresponding to the filtered patches
|
| 25 |
+
ROIs = [patches[idx] for idx in filtered_patches_indices]
|
| 26 |
+
|
| 27 |
+
return ROIs
|
| 28 |
+
|
| 29 |
+
def extract_patches(image, patch_size=(32, 32)):
|
| 30 |
+
# Extract patches from the image
|
| 31 |
+
patches = []
|
| 32 |
+
height, width = image.shape[:2]
|
| 33 |
+
patch_height, patch_width = patch_size
|
| 34 |
+
|
| 35 |
+
for y in range(0, height - patch_height + 1, patch_height):
|
| 36 |
+
for x in range(0, width - patch_width + 1, patch_width):
|
| 37 |
+
patch = image[y:y+patch_height, x:x+patch_width]
|
| 38 |
+
patches.append(patch.flatten()) # Flatten patch to create feature vector
|
| 39 |
+
|
| 40 |
+
return np.array(patches)
|
| 41 |
|
| 42 |
# Example usage
|
| 43 |
+
image_path = 'document_image.jpg'
|
| 44 |
+
threshold_distance = 100 # Adjust this threshold based on your requirements
|
| 45 |
+
back_patches_ROIs = detect_back_patches(image_path, threshold_distance)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|