Continuing from I tried to sort out objects from steak set meal images-similar image detection, this time I tried clustering the images compared with the histogram. It was.
grouping_image.py
image_dir = "{Directory path}"
target_files = os.listdir(image_dir)
files = os.listdir(image_dir)
group_images = {}
for target_file in target_files:
if target_file == '.DS_Store':
continue
#Load the image file.
target_image_path = image_dir + target_file
target_image = cv2.imread(target_image_path)
#Generate histogram data.
target_hist = cv2.calcHist([target_image], [0], None, [256], [0, 256])
similarity_images = {}
for file in files:
if file == '.DS_Store' or file == target_file:
continue
#Load the image file.
comparing_image_path = image_dir + file
comparing_image = cv2.imread(comparing_image_path)
#Generate histogram data.
comparing_hist = cv2.calcHist([comparing_image], [0], None, [256], [0, 256])
#Histogram comparison is performed to calculate the similarity of images.
ret = cv2.compareHist(target_hist, comparing_hist, 0)
#Converts the result to probability notation.
probability = ret * 100
#Outputs the similarity.
# print("target file: " + target_file, "file: " + file, "similarity: " + str(probability) + "%")
#Similarity is 90%Group only the above.
if probability > 90:
if len(target_file) not in group_images:
group_images[target_file] = {}
similarity_images[len(similarity_images)] = file
group_images[target_file].update(similarity_images)
#Group similar images.
similarity_group = {}
for target_images in group_images:
similarity_image = {}
count = 0
#Check for images that are already grouped.
if is_distinct_image(similarity_group, target_images):
for similarity_images in group_images:
if target_images != similarity_images:
distinct_images = {v:k for k, v in group_images[similarity_images].items()}
if target_images in distinct_images:
similarity_image[count] = similarity_images
count = count + 1
if target_images not in similarity_group:
similarity_group[target_images] = {}
similarity_group[target_images].update(similarity_image)
#Outputs the grouping result.
for file in similarity_group:
print file, similarity_group[file]
#If it is grouped, False will be returned.
def is_distinct_image(similarity_group, target_images):
for images in similarity_group:
distinct_images = {v:k for k, v in similarity_group[images].items()}
if target_images in distinct_images:
return False
return True
634.jpg {0: '632.jpg', 1: '657.jpg'}
575.jpg {0: '603.jpg', 1: '581.jpg', 2: '659.jpg', 3: '758.jpg', 4: '754.jpg', 5: '629.jpg'}
932.jpg {0: '799.jpg', 1: '659.jpg', 2: '815.jpg', 3: '492.jpg', 4: '921.jpg', 5: '658.jpg', 6: '920.jpg', 7: '974.jpg', 8: '629.jpg', 9: '1018.jpg', 10: '806.jpg', 11: '972.jpg'}
547.jpg {0: '559.jpg', 1: '463.jpg'}
480.jpg {0: '432.jpg'}
389.jpg {0: '432.jpg', 1: '250.jpg', 2: '369.jpg'}
It was classified into 6 types. The following two groups were divided into those that were successful and wanted to take.
634.jpg
632.jpg
575.jpg
603.jpg
In addition, pickles, miso soup, and condiments have been made difficult. Perhaps the reason is that there is an image of the entire yakiniku set meal being transferred, and I think that it is being pulled by it.
-I tried object detection using Python and OpenCV -I tried to sort out objects from the image of steak set meal-① Object detection -I tried to sort out the objects from the image of the steak set meal-② Overlap number sorting -I tried to sort out the objects from the image of the steak set meal-③ Similar image heat map detection -I tried to sort out the objects from the image of the steak set meal-④ Clustering -I tried to sort out objects from the image of steak set meal-⑤ Similar image feature point detection edition
Recommended Posts