Skip to content

Commit b03140d

Browse files
Merge pull request Mrinank-Bhowmick#614 from manishkumar00208/main
Mineral Processing Technology - Image Analytics
2 parents 7393081 + cd5a6f2 commit b03140d

File tree

12 files changed

+151
-0
lines changed

12 files changed

+151
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Mineral-Processing-Technology-Image-Analytics
2+
3+
4+
5+
In the field of Mineral Processing Technology, size analysis of the various particles of an extracted sample is of importance in determining the quality of minerals, entropy values and in establishing the degree of liberation of the values from the gangue at various particle sizes.
6+
In this problem statement, candidate is required to analyze the mineral particles in the input folder and calculate the following -
7+
8+
The smallest circle that just encapsulates the particle (the circle has to be generated on the image).
9+
Example –
10+
11+
12+
![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/93a433c2-fefb-4412-a3a3-b6b4cf67f1f4)
13+
14+
15+
16+
17+
18+
2. Total surface area of the particle (in pixels) (Has to be generated on the image)
19+
Example –
20+
21+
22+
![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/f922f999-61cd-465a-8676-e9c0998bfd3b)
23+
24+
25+
26+
27+
28+
29+
3. The major axis (longest axis) in the particle that lies entirely inside the particle (in pixels) (Has to be generated on the image)
30+
Example –
31+
32+
33+
34+
![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/fc388074-06ab-4401-b18d-020926baacaf)
35+
36+
37+
38+
4. Total perimeter of the particle (in pixels) (Has to be generated on the image)
39+
40+
41+
42+
![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/e21c7946-06cb-47b8-9e8f-64863ac20cab)
43+
44+
45+
46+
5. Centroid of the particle (Has to be generated on the image)
47+
48+
49+
![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/f01af9b5-3150-4bf4-af53-f6d70242baac)
50+
51+
52+
53+
54+
55+
Read the input images for the challenge form the “input” folder, process each image based on the calculations mentioned and save the output image in the “output” folder.
56+
57+
58+
59+
60+
61+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import cv2
2+
import os
3+
import numpy as np
4+
# Input and output folders
5+
input_folder = "input"
6+
output_folder = "output"
7+
8+
# Ensure the output folder exists
9+
if not os.path.exists(output_folder):
10+
os.makedirs(output_folder)
11+
12+
# Function to process an image
13+
def process_image(filename):
14+
# Read the image
15+
img = cv2.imread(filename)
16+
17+
# Convert the image to grayscale
18+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19+
20+
# Invert the image to make black figures on a white background
21+
inverted = cv2.bitwise_not(gray)
22+
23+
# Apply thresholding to segment the black figures (adjust threshold value as needed)
24+
_, thresh = cv2.threshold(inverted, 128, 255, cv2.THRESH_BINARY)
25+
26+
# Find contours in the binary image
27+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
28+
29+
# Draw the calculations for each black figure on the image
30+
img_processed = img.copy()
31+
for contour in contours:
32+
# Calculate the minimum enclosing circle
33+
(x, y), radius = cv2.minEnclosingCircle(contour)
34+
center = (int(x), int(y))
35+
radius = int(radius)
36+
37+
# Calculate the surface area
38+
area = cv2.contourArea(contour)
39+
40+
# Calculate the major axis as the longest distance between any two points
41+
#points = contour.squeeze()
42+
#distances = [((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**0.5 for p1 in points for p2 in points]
43+
#major_axis = max(distances)
44+
# Calculate the major axis points
45+
points = contour.squeeze()
46+
max_distance = 0
47+
major_axis_points = None
48+
for i, p1 in enumerate(points):
49+
for p2 in points[i + 1:]:
50+
distance = np.linalg.norm(p1 - p2)
51+
if distance > max_distance:
52+
max_distance = distance
53+
major_axis_points = p1, p2
54+
55+
# Draw a line representing the major axis
56+
if major_axis_points is not None:
57+
p1, p2 = major_axis_points
58+
cv2.line(img_processed, tuple(p1), tuple(p2), (0, 0, 255), 2) # Red line
59+
# Calculate the perimeter of the contour
60+
perimeter = cv2.arcLength(contour, closed=True)
61+
62+
# Calculate the centroid of the contour
63+
M = cv2.moments(contour)
64+
cx = int(M['m10'] / M['m00'])
65+
cy = int(M['m01'] / M['m00'])
66+
centroid = (cx, cy)
67+
68+
# Draw the minimum enclosing circle
69+
cv2.circle(img_processed, center, radius, (0, 0, 255), 2) # Red circle
70+
# Draw a marker at the centroid
71+
cv2.circle(img_processed, centroid, 5, (0, 0, 255), -1) # Red circle
72+
73+
74+
# Display the calculations as text
75+
cv2.putText(img_processed, f"Total surface area: {area:.2f}", (cx - 40, cy - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
76+
cv2.putText(img_processed, f"Total Perimeter: {perimeter:.2f}", (cx - 60, cy + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
77+
cv2.putText(img_processed, f"Centroid: ({cx},{cy})", (cx - 40, cy + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
78+
cv2.putText(img_processed, f"Major Axis Length: {max_distance:.2f}", (cx - 60, cy + 60),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0, 0, 255), 2)
79+
80+
# Save the processed image with calculations
81+
output_filename = os.path.join(output_folder, os.path.basename(filename))
82+
cv2.imwrite(output_filename, img_processed)
83+
84+
# Process each image in the input folder
85+
for filename in os.listdir(input_folder):
86+
if filename.endswith((".jpg", ".png")):
87+
input_image_path = os.path.join(input_folder, filename)
88+
process_image(input_image_path)
89+
90+
print("Processing complete.")
3.6 KB
Loading
2.49 KB
Loading
2.58 KB
Loading
2.49 KB
Loading
3.27 KB
Loading
9.68 KB
Loading
9.06 KB
Loading
9.61 KB
Loading

0 commit comments

Comments
 (0)