Jump to content

Processamento de imagem - coherence vector

Daniela T

Recommended Posts


Eu estou a tentar fazer um coherence vector (https://citeseerx.ist.psu.edu/viewdoc/download?doi= para um pequeno dataset. Eu já fiz o "principal", mas estou a ter alguns problemas. Por exemplo, eu calculei o número de componentes conexas, mas dá-me sempre que o número dessas componentes é 1, o que não é suposto. Outra coisa que não estou a conseguir é calcular o número e coherent pixels e de incoherent pixels. Abaixo está o meu código atual. Obrigada!

import numpy as np
import matplotlib.pyplot as plt
import cv2
import glob
import os
import PIL
import skimage
from skimage import measure
from PIL import Image
from scipy import ndimage
import seaborn as sns #For the joint plots
from sklearn.cluster import KMeans
# For the 3D color histograms
#import io
#from color_histogram.io_util.image import loadRGB
#from color_histogram.core.hist_3d import Hist3D
#import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#Read the images
data = [cv2.imread(file) for file in glob.glob("CBIR/*.png")]

#ALWAYS remember: vectorize the code
# A function that will help us to compute the similarity between
# two images, to create a color coherence vector
def measure_coherence(con_im1, non_con_im1, con_im2, non_con_im2, num_bins):
    # con_im1: connected components of the query image
    # non_con_im1 = total_im1 - con_im_1 <=> non-connected components with the image to be compared
    # con_im2: connected components of the image to be compared
    # non_con_im2 = total_im1 - con_im_2 <=> non-connected components to the image to be compared
    # num_bins: number of bins of the histogram
    # Distance
    for i in range(num_bins):
        dist = np.abs(con_im1 - non_con_im1) + np.abs(con_im2 - non_con_im2)

def measure_histogram(hist1, hist2):
    return None
def colorCoherenceVector(img, nbins): 
    #percent: The percentage of the image size to consider a component's pixels are coherent (default = 1%)
    # 1) Blur the image by replacing pixel values with the average value
    # in an 8-neighbourhood
    img_blurred = cv2.blur(img, (3, 3)) 
    #color_vector = vector
    # 2) Image shape
    width, height = img.shape[1], img.shape[0]
    print('Width = ', width, 'Height = ', height)
    #Image size
    size = width * height
    print('Size = ', size)
    # 3) Reshape the image
    arr = img_blurred.reshape((-1, 3))
    # 3) Quantize colorspace in a way that there are only 'n' different colors
    #We are going to use KMeans to do this
    n_colors = 10 #number of colors we want in our histogram
    kmeans = KMeans(n_clusters = n_colors, random_state = 0).fit(arr)
    labels = kmeans.labels_
    centers = kmeans.cluster_centers_
    print('labels:', labels.shape, labels)
    print('centers:', centers.shape, centers)
    less_colors = centers[labels].reshape(img_blurred.shape).astype('uint8')
    # se queremos que no final tenha 27 bins, nbins=3
    # -------------------------------8--------------2
    # ------------------------------nbins------n_bins**(1/3)
    h, edges = np.histogramdd(arr, nbins)
    print('edges:', len(edges), edges)
    img2 = np.zeros_like(img_blurred)
    for i in range(nbins): # Image is read as BGR
        img2[:, :, 0][np.logical_and(img_blurred[:, :, 0] >= edges[0][i], img_blurred[:, :, 0] <= edges[0][i+1])] = i
        # green
        img2[:, :, 1][np.logical_and(img_blurred[:, :, 1] >= edges[1][i], img_blurred[:, :, 1] <= edges[1][i+1])] = i
        # red
        img2[:, :, 2][np.logical_and(img_blurred[:, :, 2] >= edges[2][i], img_blurred[:, :, 2] <= edges[2][i+1])] = i
    print(img2.min(), img2.max())
    plt.subplot(1, 2, 1)
    plt.subplot(1, 2, 2)
    Not needed 
    # 4) Classify the pixels as coherent or inchoerent
    # 4.1)Computing a connected component C for each distinct color
    #labeled, nr_objects = [ndimage.label(img_blurred[i] > threshold) for i in range(h.shape)]
    # 4.2) Detemine tau's value
      # Number of levels we want to have in our quantized image
      # Usually it is 1% of image's size, but that's already too much, because
      # the size of our image is 512^2. Let's assume tau = 10
    #tau = 10
    #print('Tau = ', tau)
    # 4.3) C is coherent if the size of C exceeds a tau, otherwise C is incoherent
    #connected_components = 0
    labeled, nr_objects = ndimage.label(img_blurred)
    print('Number of connected components is {}'.format(nr_objects))
    #connect_regions, num = skimage.measure.label(img2, connectivity = 2)
    #print('Number of connected components = ', num)
    # 5) Rank retrieved images according to their similarity with the query image
    # For that we are going to compute a measure of similarity between two images (for each bin)
    # with the help of the function we created at the beginning
    #We'll do this for every image
    for i in range(len(data)):
        similarity = measure_coherence(con_im1, non_con_im1, con_im2, non_con_im2, num_bins)


def colorHistogram(img):
    #w, h = img.shape[1], img.shape[0]
    #colors = img.getcolors(w*h)
    sns.jointplot(data = data, x = "Intensity", y = "Nº of times")
    hist3D = Hist3D(img, num_bins=16, color_space='rgb')
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x, y = np.random.rand(2, 100) * 4
    hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 255], [0, 255]])

    # Construct arrays for the anchor positions of the 16 bars.
    # Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,
    # ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid
    # with indexing='ij'.
    xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
    xpos = xpos.flatten('F')
    ypos = ypos.flatten('F')
    zpos = np.zeros_like(xpos)

    # Construct arrays with the dimensions for the 16 bars.
    dx = 0.5 * np.ones_like(zpos)
    dy = dx.copy()
    dz = hist.flatten()

    ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')

    #Compute the distance 'between histograms'
    # Create a 'vector of similar images' 

#Ask the user for the Query image

while True:
    #descriptor = input('Enter color descriptor or [C]ancel: ')
    query_img = input('Enter query image or [C]ancel: ') #Answer is a string
    colorCoherenceVector(data[int(query_img)], 2)
    #if descriptor.upper() == 'C' or query_img.upper() == 'C':
    if query_img.upper() == 'C':
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.