## Recommended Posts

Olá,

Eu estou a tentar fazer um coherence vector (https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.31.1421&rep=rep1&type=pdf) 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.core.hist_3d import Hist3D
#import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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)
print(dist)

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, img.shape
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')
cv2.imshow(less_colors)
'''

# se queremos que no final tenha 27 bins, nbins=3
# -------------------------------8--------------2
# ------------------------------nbins------n_bins**(1/3)

print(img_blurred.shape)
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
#blue
img2[:, :, 0][np.logical_and(img_blurred[:, :, 0] >= edges[i], img_blurred[:, :, 0] <= edges[i+1])] = i
# green
img2[:, :, 1][np.logical_and(img_blurred[:, :, 1] >= edges[i], img_blurred[:, :, 1] <= edges[i+1])] = i
# red
img2[:, :, 2][np.logical_and(img_blurred[:, :, 2] >= edges[i], img_blurred[:, :, 2] <= edges[i+1])] = i

print(img2.min(), img2.max())
plt.subplot(1, 2, 1)
plt.imshow(img_blurred)
plt.subplot(1, 2, 2)
plt.imshow(img2/2)
plt.show()

"""
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(connect_regions)
#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)
'''

print('----------------------------------------------------------------------')

def colorHistogram(img):
'''
#w, h = img.shape, img.shape
#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()
hist3D.plot(ax)
plt.show()
'''
fig = plt.figure()
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')

plt.show()

#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)
colorHistogram(data[int(query_img)])
#if descriptor.upper() == 'C' or query_img.upper() == 'C':
if query_img.upper() == 'C':
break```

## Create an account

Register a new account

×

• #### Revista PROGRAMAR

• Wiki
• IRC
×
• Create New...