image_output_1.png
import cv2

import glob

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_labels = pd.read_csv('../input/ham1000-segmentation-and-classification/GroundTruth.csv')
df_labels['image'] = df_labels['image'] + '.jpg'
labels = ['MEL', 'NV', 'BCC', 'AKIEC', 'BKL', 'DF', 'VASC']
label_list = []
for i in range(len(df_labels)):
    row = list(df_labels.iloc[i])
    del row[0]
    index = np.argmax(row)
    label = labels[index]
    label_list.append(label)
df_labels['label'] = label_list
df_labels = df_labels.drop(labels, axis=1)

path = '../input/ham1000-segmentation-and-classification/images/ISIC_0024306.jpg'
img = cv2.imread(path)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = (img2[:, :, 0], img2[:, :, 1], img2[:, :, 2])
hist_h = cv2.calcHist([h], [0], None, [256], [0, 256])
hist_h = cv2.normalize(hist_h, hist_h)
hist_s = cv2.calcHist([s], [0], None, [256], [0, 256])
hist_s = cv2.normalize(hist_s, hist_s)
hist_v = cv2.calcHist([v], [0], None, [256], [0, 256])
hist_v = cv2.normalize(hist_v, hist_v)

class HSVHistogram:
    def __init__(self, bins):
        self.bins = bins
    def describe(self, image):
        image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        hist = cv2.calcHist([image], [0, 1, 2], None, self.bins, [0, 256, 0, 256, 0, 256])
        hist = cv2.normalize(hist, hist)
        return hist.flatten()
desc = HSVHistogram([8, 8, 8])

features_names = ['feature_' + str(i) for i in range(8 ** 3)]
df_features = pd.DataFrame(columns=features_names)

img_list = glob.glob('../input/ham1000-segmentation-and-classification/images/' + "/*.jpg")
for idx,img_path in enumerate(sorted(img_list[:1000])):
    k = img_path[img_path.rfind("/") + 1:]
    image = cv2.imread(img_path)
    features = desc.describe(image)
    df_features.loc[k,:] = features

np_features = df_features[features_names].values

query_path = '../input/ham1000-segmentation-and-classification/images/ISIC_0024306.jpg'
query_image = cv2.imread(query_path)
RGB_im = cv2.cvtColor(query_image, cv2.COLOR_BGR2RGB)
label = df_labels[df_labels['image'] == 'ISIC_0024306.jpg']['label'].iloc[0]
plt.imshow(RGB_im)
plt.title(label)
query_features = desc.describe(query_image)
query_features = np.array(query_features)