对于我的CBIR,我想想象图像上的可视字,以便根据它们的频率绘制它们。与下图类似的东西:
为了可视化“视觉词”,我将它们映射到描述符上,然后映射到相应的关键点上。我的问题是,如果我使用kp.pt
和kp.size
我只获取图像的块,而我知道不同的关键点位于不同的八度,我想这里kp.size
无关像素区域。
有人能给我一些建议吗?
到目前为止,这是我的代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns; sns.set()
import random
from collections import Counter
def MarkerCoordinatesOnImg(imgpath, assignments_indx, assignments_list, coordinate):
img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
#img = img1[::,::-1] #Because OpenCv read the image in BGR and I need a RGB
for index in assignments_indx:
r = random.randint(0,200) #Integer from 0 to 255, endpoints included
g = random.randint(20,255)
b = random.randint(50,255)
CropVisualWords(imgpath, coordinate, index, b, g, r, assignments_list)
for i in index:
xy = tuple(coordinate[i])
cv2.drawMarker(img, xy, (r,g,b), markerType=cv2.MARKER_CROSS, markerSize=10, thickness=2, line_type=cv2.LINE_AA)
#plt.subplot(2,1,1)
plt.figure(figsize=(13,10))
plt.imshow(img)
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
def CropVisualWords(imgpath, coordinate, index, b, g, r, assignments_list):
img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
for i in index:
xy = coordinate[i]
x = xy[0]
y = xy[1]
#diam = xyd[2]
#d = int(diam/2)
d = 4
v_word = img[y-d:y+d, x-d:x+d]
#resize the image
r = 100.0/v_word.shape[1]
dim = (100, int(v_word.shape[0] * r))
resized_v_word = cv2.resize(v_word, dim, interpolation=cv2.INTER_AREA)
#add colour border to the image
bordersize = 10
resized_v_word = cv2.copyMakeBorder(resized_v_word, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[b, g, r])
cv2.imwrite("C:/Users/terig/Desktop/TESI/Thesis/CODE/DATA/VisualWords/Test(63)/ "+str(assignments_list[i])+"_["+str(i)+"].png", resized_v_word)
def plot(assignments):
#select the first 10 Visual Words label sorted on frequency
counts = dict(Counter(assignments).most_common(10))
wordslabel, frequency = zip(*counts.items())
#sort frequency values in descending order
indSort = np.argsort(frequency)[::-1]
#rearrange the data according to indSort
wordslabel = np.array(wordslabel)[indSort]
frequency = np.array(frequency)[indSort]
word_indexes = np.arange(len(wordslabel))
#find assignments index of the visual words:
assignments_indx = []
for w in np.nditer(wordslabel):
temp_indx = []
for i in np.arange(len(assignments)):
if assignments[i] == w:
temp_indx.append(i)
assignments_indx.append(temp_indx)
#Plot
#plt.subplot(2,1,2)
plt.figure(figsize=(13,10))
for i in range(size(word_indexes)):
imm=plt.imread(imgpath)
plt.imshow(imm, extent=[-0.5+1*i,0.5+1*i,-1,0])
bar_width = 0.35
plt.bar(word_indexes, frequency,align='center')
# add labels
plt.xticks(word_indexes + bar_width*0, wordslabel, horizontalalignment='center')
plt.title('Visual words frequency')
plt.show()
return(assignments_indx)
if __name__ == '__main__':
imgpath = "C:/Users/terig/Desktop/TESI/Thesis/CODE/DATA/QueryImg/Test(63).jpg"
#assignments_indx = [[55, 73, 134, 258], [31, 47, 52, 180], [100, 195, 270], [92, 95, 318], [70, 150, 202], [64, 98, 173], [41, 176, 253], [21, 226, 251], [6, 29, 141], [8, 87]]
assignments_list = [514, 289, 163, 377, 225, 253, 314, 410, 20, 738, 784, 149, 488,
528, 58, 362, 35, 537, 334, 20, 745, 418, 776, 163, 608, 579,
399, 69, 791, 58, 735, 314, 673, 651, 579, 414, 555, 627, 674,
781, 308, 114, 139, 129, 503, 31, 789, 314, 503, 369, 378, 738,
341, 353, 589, 304, 360, 502, 634, 729, 430, 631, 204, 364, 48,
342, 174, 638, 197, 585, 210, 513, 531, 455, 683, 591, 619, 420,
202, 42, 291, 707, 415, 274, 371, 515, 287, 495, 452, 50, 703,
336, 365, 142, 781, 571, 410, 74, 707, 554, 676, 588, 162, 600,
246, 246, 516, 138, 452, 791, 421, 4, 208, 697, 336, 292, 78,
652, 318, 431, 457, 103, 644, 462, 574, 727, 206, 629, 50, 524,
89, 37, 521, 549, 402, 586, 104, 92, 710, 461, 213, 281, 189,
158, 573, 167, 559, 626, 457, 555, 223, 73, 147, 420, 710, 215,
624, 560, 619, 721, 476, 223, 334, 266, 176, 211, 756, 754, 330,
220, 79, 407, 222, 645, 688, 685, 406, 789, 151, 89, 314, 388,
796, 692, 444, 420, 631, 318, 412, 588, 746, 681, 154, 438, 759,
676, 343, 696, 248, 556, 733, 156, 223, 797, 496, 783, 353, 218,
449, 98, 44, 435, 125, 76, 456, 144, 744, 490, 91, 126, 212,
158, 626, 789, 764, 402, 418, 483, 753, 65, 123, 663, 534, 580,
119, 51, 764, 105, 483, 616, 449, 469, 477, 456, 441, 708, 129,
562, 510, 362, 597, 418, 423, 580, 331, 619, 276, 563, 219, 545,
230, 297, 228, 574, 156, 724, 643, 203, 236, 20, 589, 122, 487,
759, 194, 573, 102, 226, 762, 672, 573, 137, 565, 142, 266, 497,
799, 287, 512, 768, 653, 641, 253, 398, 289, 12, 449, 39, 91,
746, 154, 452, 205, 613, 68, 466, 237, 142, 429, 458, 751, 243,
679, 319, 298, 58, 609, 294, 365, 746, 101, 738, 631, 646, 20,
565, 700, 125, 603, 377, 114, 33, 527]
assignments = np.asarray(assignments_list)
coordinate = [[6, 39], [8, 70], [8, 70], [8, 44], [8, 44], [9, 48], [10, 35], [10, 106], [11, 93], [12, 130], [12, 68], [13, 84], [13, 84], [14, 50], [14, 59], [14, 114], [14, 114], [15, 85], [15, 85], [15, 117], [16, 108], [16, 41], [16, 57], [17, 89], [17, 116], [18, 76], [18, 32], [18, 32], [18, 137], [18, 145], [19, 54], [19, 149], [19, 94], [19, 93], [21, 70], [22, 111], [22, 132], [22, 132], [22, 83], [22, 128], [22, 122], [23, 60], [23, 139], [23, 117], [24, 38], [26, 134], [26, 57], [26, 14], [26, 38], [26, 34], [26, 34], [27, 107], [27, 14], [27, 84], [27, 84], [27, 47], [28, 68], [28, 23], [29, 54], [29, 29], [29, 61], [32, 22], [33, 34], [34, 139], [34, 65], [34, 112], [34, 112], [34, 59], [34, 92], [34, 92], [35, 121], [35, 125], [35, 106], [35, 68], [35, 68], [37, 134], [37, 35], [37, 109], [37, 109], [37, 121], [37, 71], [37, 105], [37, 120], [38, 24], [38, 18], [38, 18], [38, 18], [38, 69], [39, 9], [39, 66], [39, 66], [39, 31], [39, 102], [39, 102], [39, 147], [39, 103], [39, 103], [40, 108], [40, 57], [41, 52], [41, 39], [42, 79], [42, 69], [42, 84], [43, 46], [43, 18], [43, 99], [43, 99], [44, 8], [44, 8], [44, 27], [44, 121], [44, 12], [44, 155], [45, 20], [45, 22], [45, 35], [45, 69], [45, 69], [45, 20], [45, 79], [46, 129], [46, 150], [47, 16], [48, 86], [49, 91], [49, 144], [49, 10], [50, 52], [50, 52], [50, 36], [50, 63], [51, 137], [51, 137], [51, 15], [51, 131], [51, 49], [52, 68], [53, 154], [54, 77], [54, 119], [55, 10], [55, 54], [55, 137], [55, 133], [55, 133], [56, 31], [56, 31], [56, 128], [56, 128], [56, 26], [56, 39], [56, 13], [56, 13], [57, 154], [57, 51], [57, 51], [57, 21], [58, 66], [58, 66], [58, 117], [59, 36], [59, 49], [60, 134], [60, 154], [60, 19], [61, 123], [61, 47], [61, 150], [62, 139], [62, 89], [62, 89], [62, 58], [62, 58], [63, 38], [63, 135], [64, 109], [64, 67], [65, 28], [65, 148], [66, 80], [67, 39], [68, 103], [68, 159], [68, 46], [69, 77], [69, 130], [70, 70], [70, 51], [70, 114], [71, 19], [71, 19], [71, 19], [71, 147], [71, 25], [72, 31], [72, 40], [73, 60], [73, 12], [74, 77], [74, 77], [74, 156], [74, 135], [74, 149], [74, 149], [74, 145], [74, 105], [75, 84], [75, 50], [75, 88], [75, 155], [77, 160], [77, 99], [77, 21], [78, 135], [78, 69], [79, 35], [80, 130], [80, 147], [81, 159], [81, 159], [81, 115], [81, 152], [81, 71], [82, 38], [82, 38], [82, 26], [83, 45], [83, 85], [83, 132], [84, 49], [85, 82], [86, 158], [86, 36], [86, 87], [87, 13], [87, 13], [87, 30], [87, 97], [88, 84], [88, 54], [89, 37], [89, 16], [89, 144], [89, 106], [89, 94], [89, 141], [89, 124], [90, 20], [90, 76], [90, 113], [91, 7], [91, 7], [91, 68], [91, 28], [91, 36], [92, 18], [93, 82], [94, 13], [94, 84], [95, 7], [95, 103], [95, 90], [96, 125], [97, 152], [97, 109], [98, 43], [98, 165], [98, 90], [99, 43], [99, 62], [99, 21], [99, 80], [99, 80], [101, 44], [102, 78], [103, 89], [103, 89], [103, 49], [103, 16], [104, 86], [104, 86], [106, 133], [106, 92], [107, 52], [107, 102], [107, 81], [107, 72], [108, 36], [108, 20], [108, 27], [109, 66], [110, 79], [111, 25], [112, 16], [112, 53], [113, 90], [113, 37], [113, 24], [113, 87], [113, 87], [114, 96], [114, 19], [115, 33], [115, 33], [115, 53], [115, 50], [115, 50], [116, 27], [116, 58], [116, 10], [117, 83], [117, 83], [118, 19], [118, 19], [118, 12], [118, 40], [120, 80], [121, 37], [121, 69], [121, 69], [123, 51], [124, 81], [124, 21], [124, 21], [124, 58], [124, 66], [125, 88], [129, 27], [129, 27], [131, 81], [132, 74], [132, 38]]
kps_dimension = [[6, 39, 2], [8, 70, 4], [8, 70, 4], [8, 44, 4], [8, 44, 4], [9, 48, 2], [10, 35, 3], [10, 106, 2], [11, 93, 2], [12, 130, 4], [12, 68, 2], [13, 84, 5], [13, 84, 5], [14, 50, 5], [14, 59, 13], [14, 114, 3], [14, 114, 3], [15, 85, 3], [15, 85, 3], [15, 117, 13], [16, 108, 3], [16, 41, 9], [16, 57, 3], [17, 89, 2], [17, 116, 2], [18, 76, 2], [18, 32, 2], [18, 32, 2], [18, 137, 4], [18, 145, 3], [19, 54, 2], [19, 149, 3], [19, 94, 4], [19, 93, 3], [21, 70, 2], [22, 111, 6], [22, 132, 3], [22, 132, 3], [22, 83, 3], [22, 128, 2], [22, 122, 3], [23, 60, 4], [23, 139, 4], [23, 117, 3], [24, 38, 3], [26, 134, 2], [26, 57, 3], [26, 14, 7], [26, 38, 2], [26, 34, 4], [26, 34, 4], [27, 107, 2], [27, 14, 6], [27, 84, 5], [27, 84, 5], [27, 47, 3], [28, 68, 7], [28, 23, 3], [29, 54, 7], [29, 29, 3], [29, 61, 2], [32, 22, 2], [33, 34, 4], [34, 139, 3], [34, 65, 2], [34, 112, 2], [34, 112, 2], [34, 59, 4], [34, 92, 8], [34, 92, 8], [35, 121, 3], [35, 125, 2], [35, 106, 2], [35, 68, 2], [35, 68, 2], [37, 134, 5], [37, 35, 2], [37, 109, 2], [37, 109, 2], [37, 121, 7], [37, 71, 2], [37, 105, 2], [37, 120, 7], [38, 24, 3], [38, 18, 3], [38, 18, 3], [38, 18, 3], [38, 69, 22], [39, 9, 2], [39, 66, 2], [39, 66, 2], [39, 31, 5], [39, 102, 3], [39, 102, 3], [39, 147, 4], [39, 103, 2], [39, 103, 2], [40, 108, 6], [40, 57, 2], [41, 52, 2], [41, 39, 4], [42, 79, 2], [42, 69, 2], [42, 84, 2], [43, 46, 7], [43, 18, 3], [43, 99, 3], [43, 99, 3], [44, 8, 2], [44, 8, 2], [44, 27, 3], [44, 121, 3], [44, 12, 2], [44, 155, 3], [45, 20, 5], [45, 22, 2], [45, 35, 2], [45, 69, 5], [45, 69, 5], [45, 20, 19], [45, 79, 3], [46, 129, 8], [46, 150, 6], [47, 16, 2], [48, 86, 3], [49, 91, 2], [49, 144, 5], [49, 10, 4], [50, 52, 6], [50, 52, 6], [50, 36, 5], [50, 63, 3], [51, 137, 3], [51, 137, 3], [51, 15, 2], [51, 131, 3], [51, 49, 2], [52, 68, 3], [53, 154, 3], [54, 77, 3], [54, 119, 2], [55, 10, 2], [55, 54, 3], [55, 137, 2], [55, 133, 3], [55, 133, 3], [56, 31, 2], [56, 31, 2], [56, 128, 2], [56, 128, 2], [56, 26, 2], [56, 39, 3], [56, 13, 2], [56, 13, 2], [57, 154, 2], [57, 51, 48], [57, 51, 48], [57, 21, 2], [58, 66, 6], [58, 66, 6], [58, 117, 2], [59, 36, 2], [59, 49, 7], [60, 134, 2], [60, 154, 4], [60, 19, 3], [61, 123, 3], [61, 47, 3], [61, 150, 3], [62, 139, 3], [62, 89, 16], [62, 89, 16], [62, 58, 6], [62, 58, 6], [63, 38, 6], [63, 135, 3], [64, 109, 4], [64, 67, 2], [65, 28, 2], [65, 148, 3], [66, 80, 3], [67, 39, 2], [68, 103, 2], [68, 159, 2], [68, 46, 2], [69, 77, 4], [69, 130, 2], [70, 70, 2], [70, 51, 2], [70, 114, 7], [71, 19, 3], [71, 19, 3], [71, 19, 3], [71, 147, 2], [71, 25, 4], [72, 31, 3], [72, 40, 6], [73, 60, 2], [73, 12, 2], [74, 77, 2], [74, 77, 2], [74, 156, 3], [74, 135, 7], [74, 149, 4], [74, 149, 4], [74, 145, 3], [74, 105, 3], [75, 84, 3], [75, 50, 4], [75, 88, 2], [75, 155, 6], [77, 160, 2], [77, 99, 5], [77, 21, 4], [78, 135, 3], [78, 69, 2], [79, 35, 3], [80, 130, 2], [80, 147, 4], [81, 159, 2], [81, 159, 2], [81, 115, 10], [81, 152, 2], [81, 71, 2], [82, 38, 2], [82, 38, 2], [82, 26, 8], [83, 45, 2], [83, 85, 2], [83, 132, 6], [84, 49, 4], [85, 82, 2], [86, 158, 3], [86, 36, 2], [86, 87, 2], [87, 13, 2], [87, 13, 2], [87, 30, 2], [87, 97, 2], [88, 84, 6], [88, 54, 5], [89, 37, 5], [89, 16, 2], [89, 144, 4], [89, 106, 6], [89, 94, 2], [89, 141, 3], [89, 124, 6], [90, 20, 5], [90, 76, 11], [90, 113, 5], [91, 7, 4], [91, 7, 4], [91, 68, 2], [91, 28, 4], [91, 36, 3], [92, 18, 3], [93, 82, 2], [94, 13, 2], [94, 84, 2], [95, 7, 2], [95, 103, 3], [95, 90, 2], [96, 125, 13], [97, 152, 12], [97, 109, 17], [98, 43, 2], [98, 165, 5], [98, 90, 2], [99, 43, 15], [99, 62, 9], [99, 21, 3], [99, 80, 2], [99, 80, 2], [101, 44, 4], [102, 78, 3], [103, 89, 2], [103, 89, 2], [103, 49, 10], [103, 16, 2], [104, 86, 3], [104, 86, 3], [106, 133, 4], [106, 92, 6], [107, 52, 4], [107, 102, 5], [107, 81, 5], [107, 72, 10], [108, 36, 3], [108, 20, 3], [108, 27, 2], [109, 66, 3], [110, 79, 2], [111, 25, 2], [112, 16, 2], [112, 53, 2], [113, 90, 2], [113, 37, 2], [113, 24, 2], [113, 87, 2], [113, 87, 2], [114, 96, 4], [114, 19, 3], [115, 33, 2], [115, 33, 2], [115, 53, 2], [115, 50, 3], [115, 50, 3], [116, 27, 3], [116, 58, 10], [116, 10, 9], [117, 83, 2], [117, 83, 2], [118, 19, 2], [118, 19, 2], [118, 12, 6], [118, 40, 3], [120, 80, 3], [121, 37, 3], [121, 69, 3], [121, 69, 3], [123, 51, 2], [124, 81, 3], [124, 21, 8], [124, 21, 8], [124, 58, 3], [124, 66, 3], [125, 88, 2], [129, 27, 2], [129, 27, 2], [131, 81, 2], [132, 74, 2], [132, 38, 2]]
assignments_indx = plot(assignments)
MarkerCoordinatesOnImg(imgpath, assignments_indx, assignments_list, coordinate)