-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageProcessing2023_07_05(2).py
127 lines (94 loc) · 4.89 KB
/
ImageProcessing2023_07_05(2).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import cv2
import numpy as np
# Set the paths to your directories containing positive and negative images
positive_directory = "D:\\Research\\ComputerVision\\positiveImages"
negative_directory = "D:\\Research\\ComputerVision\\negativeImages"
# Load the positive images
positive_files = sorted(os.listdir(positive_directory))
positive_images = [cv2.imread(os.path.join(positive_directory, file)) for file in positive_files]
# Load the negative images
negative_files = sorted(os.listdir(negative_directory))
negative_images = [cv2.imread(os.path.join(negative_directory, file)) for file in negative_files]
# Define the maximum width and height
max_width = 1400
max_height = 750
# Resize images to a maximum width of 1600 pixels
# Resize the images while preserving aspect ratio
def resize_images(images):
resized_images = []
for image in images:
# Get the original image dimensions
height, width = image.shape[:2]
# Calculate the aspect ratio
aspect_ratio = width / float(height)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Determine the new dimensions based on the maximum width or height
if width > max_width:
width = max_width
height = int(width / aspect_ratio)
if height > max_height:
height = max_height
width = int(height * aspect_ratio)
# Resize the image
resized_image = cv2.resize(gray, (width, height))
resized_images.append(resized_image)
return resized_images
# Perform defect detection using blob detection
def detect_defects(images):
total_defects_detected = 0
for image in images:
# Convert grayscale image to 3-channel image
color_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
# Perform thresholding to obtain binary image
_, binary = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Set up the blob detector parameters
params = cv2.SimpleBlobDetector_Params()
# Modify parameters as needed
params.minThreshold = 1 # Minimum threshold value to consider a pixel as part of a blob
params.maxThreshold = 25 # Maximum threshold value to consider a pixel as part of a blob
params.filterByArea = True # Filter blobs based on area
params.minArea = 80 # Minimum blob area in pixels
params.maxArea = 280 # Maximum blob area in pixels
params.filterByCircularity = True # Filter blobs based on circularity
params.minCircularity = 0.4 # Minimum circularity value (0.0 - 1.0)
params.filterByConvexity = True # Filter blobs based on convexity
params.minConvexity = 0.85 # Minimum convexity value (0.0 - 1.0)
params.filterByInertia = True # Filter blobs based on inertia
params.minInertiaRatio = 0.2
# Create the blob detector
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs in the binary image
keypoints = detector.detect(binary)
if len(keypoints) > 0:
total_defects_detected += 1
# Draw detected blobs on the color image
image_with_keypoints = cv2.drawKeypoints(color_image, keypoints, np.array([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Display or save the image with detected blobs
# cv2.imshow("Defect Detection", image_with_keypoints)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return total_defects_detected
# Calculate accuracy of defect detection
def calculate_accuracy(total_defects_detected, total_images):
accuracy = (total_defects_detected / total_images) * 100
return accuracy
# Resize positive images to a maximum width of 1600 pixels
positive_images_resized = resize_images(positive_images)
# Resize negative images to a maximum width of 1600 pixels
negative_images_resized = resize_images(negative_images)
# Detect defects in positive images
positive_defects_detected = detect_defects(positive_images_resized)
positive_defects_accuracy = positive_defects_detected/len(positive_images_resized)*100
# Detect defects in negative images
negative_defects_detected = detect_defects(negative_images_resized)
negative_defects_accuracy = (1 - negative_defects_detected/len(negative_images_resized))*100
# Calculate total number of images
# total_images = len(positive_images_resized) + len(negative_images_resized)
# Calculate total defects detected
# total_defects_detected = positive_defects_detected + negative_defects_detected
# Calculate accuracy
# accuracy = calculate_accuracy(total_defects_detected, total_images)
print("Positive Defect Detection Accuracy: {:.2f}%".format(positive_defects_accuracy))
print("Negative Defect Detection Accuracy: {:.2f}%".format(negative_defects_accuracy))