Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mineral Processing Technology - Image Analytics #614

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions projects/Mineral Processing Technology-Image Analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Mineral-Processing-Technology-Image-Analytics



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.
In this problem statement, candidate is required to analyze the mineral particles in the input folder and calculate the following -

The smallest circle that just encapsulates the particle (the circle has to be generated on the image).
Example –


![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/93a433c2-fefb-4412-a3a3-b6b4cf67f1f4)





2. Total surface area of the particle (in pixels) (Has to be generated on the image)
Example –


![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/f922f999-61cd-465a-8676-e9c0998bfd3b)






3. The major axis (longest axis) in the particle that lies entirely inside the particle (in pixels) (Has to be generated on the image)
Example –



![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/fc388074-06ab-4401-b18d-020926baacaf)



4. Total perimeter of the particle (in pixels) (Has to be generated on the image)



![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/e21c7946-06cb-47b8-9e8f-64863ac20cab)



5. Centroid of the particle (Has to be generated on the image)


![image](https://github.com/manishkumar00208/Mineral-Processing-Technology-Image-Analytics/assets/76589863/f01af9b5-3150-4bf4-af53-f6d70242baac)





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.






90 changes: 90 additions & 0 deletions projects/Mineral Processing Technology-Image Analytics/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import cv2
import os
import numpy as np
# Input and output folders
input_folder = "input"
output_folder = "output"

# Ensure the output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)

# Function to process an image
def process_image(filename):
# Read the image
img = cv2.imread(filename)

# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Invert the image to make black figures on a white background
inverted = cv2.bitwise_not(gray)

# Apply thresholding to segment the black figures (adjust threshold value as needed)
_, thresh = cv2.threshold(inverted, 128, 255, cv2.THRESH_BINARY)

# Find contours in the binary image
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw the calculations for each black figure on the image
img_processed = img.copy()
for contour in contours:
# Calculate the minimum enclosing circle
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)

# Calculate the surface area
area = cv2.contourArea(contour)

# Calculate the major axis as the longest distance between any two points
#points = contour.squeeze()
#distances = [((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**0.5 for p1 in points for p2 in points]
#major_axis = max(distances)
# Calculate the major axis points
points = contour.squeeze()
max_distance = 0
major_axis_points = None
for i, p1 in enumerate(points):
for p2 in points[i + 1:]:
distance = np.linalg.norm(p1 - p2)
if distance > max_distance:
max_distance = distance
major_axis_points = p1, p2

# Draw a line representing the major axis
if major_axis_points is not None:
p1, p2 = major_axis_points
cv2.line(img_processed, tuple(p1), tuple(p2), (0, 0, 255), 2) # Red line
# Calculate the perimeter of the contour
perimeter = cv2.arcLength(contour, closed=True)

# Calculate the centroid of the contour
M = cv2.moments(contour)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
centroid = (cx, cy)

# Draw the minimum enclosing circle
cv2.circle(img_processed, center, radius, (0, 0, 255), 2) # Red circle
# Draw a marker at the centroid
cv2.circle(img_processed, centroid, 5, (0, 0, 255), -1) # Red circle


# Display the calculations as text
cv2.putText(img_processed, f"Total surface area: {area:.2f}", (cx - 40, cy - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(img_processed, f"Total Perimeter: {perimeter:.2f}", (cx - 60, cy + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(img_processed, f"Centroid: ({cx},{cy})", (cx - 40, cy + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
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)

# Save the processed image with calculations
output_filename = os.path.join(output_folder, os.path.basename(filename))
cv2.imwrite(output_filename, img_processed)

# Process each image in the input folder
for filename in os.listdir(input_folder):
if filename.endswith((".jpg", ".png")):
input_image_path = os.path.join(input_folder, filename)
process_image(input_image_path)

print("Processing complete.")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.