【问题标题】:Map similar images in two groups在两组中映射相似的图像
【发布时间】:2022-07-07 09:56:28
【问题描述】:

我有两组从不同来源生成的图像。

第一组图片:Set 1 - Image 1Set 1 - Image 2Set 1 - Image 3 设置 2 图片:Set 2 - Image 1Set 2 - Image 2Set 2 - Image 3

一组中的图像在另一组中具有相似的图像。我想将每个图像映射到相似的图像。我曾尝试使用 OpenCV 函数进行特征点映射,但结果不正确。

【问题讨论】:

标签: python image shapes detection


【解决方案1】:

你必须说出你在每种类型的图像上发现了什么“相似”,才能知道你想用什么标准对它们进行分类。

例如,第二组图像大多是白色,所以如果你检测到最常见的颜色,并且是白色的,它就属于第二类图像。

import numpy as np
import cv2
import matplotlib.pyplot as plt

URL_list = ["https://i.stack.imgur.com/RpTDZ.png",
    "https://i.stack.imgur.com/LXpRr.png",
    "https://i.stack.imgur.com/4mdfs.png",
    "https://i.stack.imgur.com/xEZNl.png",
    "https://i.stack.imgur.com/EattC.png",
    "https://i.stack.imgur.com/A42B9.png"]


def downloadImage(URL):
    '''Downloads the image on the URL, and convers to cv2 BGR format'''
    from io import BytesIO
    from PIL import Image as PIL_Image
    import requests
    response = requests.get(URL)
    image = PIL_Image.open(BytesIO(response.content))
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

def classifierByMostCommonColor(image):
    '''Classifies the image by the most common color'''
    npImage = np.array(image).reshape(image.shape[0]*image.shape[1],3)
    unique, counts = np.unique(npImage, return_counts=True, axis=0)
    idx   = np.argsort(counts)[::-1]#index of sorted counts from largest to smaller
    return unique[idx[:2]]

class imageWithProperties(object):
    image: np.ndarray
    commonColors: np.ndarray
    type:str
    
    def __init__(self,image):
        WHITE_COLOR = [255,255,255]
        self.image = image
        
        #Classify image by most common color
        self.commonColors = classifierByMostCommonColor(image)
        if WHITE_COLOR in self.commonColors:
            self.type="White background"
        else:
            self.type="Gray background"
        
#Download all image from URL        
img=[]
for url in URL_list:
    img.append(imageWithProperties(downloadImage(url)))
    print(f"Image {len(img)}: {img[-1].type}")
    
#plot all images with classified type in title
plt.figure()
f, axarr = plt.subplots(2,3) 

for idx,image in enumerate(img):
    axarr[idx//3,idx%3].imshow(image.image)
    axarr[idx//3,idx%3].set_title(image.type)
    axarr[idx//3,idx%3].axis('off')
plt.show()

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 2021-12-19
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2020-02-20
    相关资源
    最近更新 更多