问题描述
我一直在尝试使用多种方法进行图像聚类。我已经能够使用基于颜色的功能,并通过Keras和多种方式使用特征提取。我无法使用基于ORB的功能描述符成功完成此操作。
在下面提到的第二个代码段中,我尝试将其重塑为平坦的np.Array,但我无法。
如果我愿意
descriptors.append(np.array([orb_desc]))
然后我得到
ValueError: Could not broadcast input array from shape (1027,32) into shape (1)
如果我做descriptors.append(np.array(orb_desc))
,我会得到
TypeError: only size-1 arrays can be converted to Python scalars
ValueError: setting an array element with a sequence.
在flat_descriptors = np.array(np.float32(descriptors).reshape(len(descriptors),-1)/255)
行中都是
不确定如何格式化/重塑npArray以使其具有可接受的正确格式。
我的主要群集代码
def cluster(db_image_list,clusters=2):
# load the image and convert it from BGR to RGB so that
# we can display it with matplotlib
images = [cv.resize(cv.imread(file),(224,224)) for file in db_image_list]
images = [cv.cvtColor(image,cv.COLOR_BGR2RGB) for image in images]
# predicted_images = get_color_images(images) # Only Colour based matrices do not get very satisfactory results
# predicted_images = get_keras_prediction(images)
# predicted_images = orb_feature_descriptor_detection(images)
predicted_images = hog_histogram_images(images)
clt = KMeans(n_clusters = clusters)
clt.fit(predicted_images)
kpredictions = clt.predict(predicted_images)
for i in range(clusters):
os.makedirs("output\cluster" + str(i))
for i in range(len(db_image_list)):
print("clustering copying",i)
shutil.copy2(db_image_list[i],"output\cluster"+str(kpredictions[i]))
ORB功能
def orb_feature_descriptor_detection(images):
# WIP. Can't figure out to reshape the descriptors properly
descriptors = []
for image in images:
_,orb_desc = fd.orb_descriptor(image,False)
if orb_desc is not None:
descriptors.append(np.array([orb_desc]))
flat_descriptors = np.array(np.float32(descriptors).reshape(len(descriptors),-1)/255)
return flat_descriptors
基本ORB KP,描述符检索器
def orb_descriptor(img,bounding_rm=True):
img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
orb = cv.ORB_create(2000)
if bounding_rm:
bounding = text_Boxes.detect_text_Box(img)
bounding_mask = np.zeros(img_gray.shape,np.uint8)
bounding_mask[bounding[1]:bounding[3],bounding[0]:bounding[2]] = 255
bounding_mask = (255-bounding_mask)
kp,des=orb.detectAndCompute(img_gray,mask = bounding_mask)
else:
kp,des = orb.detectAndCompute(img_gray,mask = None)
return (kp,des)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)