我正在嘗試使用cv2.matchShapes我正在使用影像資料集的位置,所以當我比較它們的輪廓時,然后將所有ret值存盤在一個浮點陣列中,并將影像存盤在另一個陣列中,所以我想要的最終結果是根據它們的 ret 值排序的影像陣列. 嘗試if rets[i] < rets[i 1]但出錯TypeError: list indices must be integers or slices, not float,谷歌搜索浮點比較但沒有解決我的問題。
import argparse
import pickle
import glob
import sys
from PIL import Image
import os
import cv2
import numpy as np
dataset = "dataset/*.jpg"
# print('image_array shape:', np.array(imageArray).shape)
# cv2.imshow('frame', imageArray[1])
# cv2.waitKey(0)
queryImage = cv2.imread("query/12034.jpg", 0)
ret, thresh = cv2.threshold(queryImage, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
queryContour = contours[0]
min_dist = sys.maxsize
images = []
rets = []
for imagePath in glob.glob(dataset):
image = cv2.imread(imagePath, 0)
ret, th2 = cv2.threshold(image, 127, 255, 0)
contours, hierarchy = cv2.findContours(th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
ret = cv2.matchShapes(queryContour, cnt, 1, 0.0)
images.append(image)
rets.append(ret)
# if ret < min_dist:
# min_dist = ret
# images.append(image)
print(ret)
for i in rets:
if abs(rets[i] - rets[i 1]):
tmp = images[i]
images[i] = images[i 1]
images[i 1] = tmp
retTmp = rets[i]
rets[i] = rets[i 1]
rets[i 1] = retTmp
print(rets)
uj5u.com熱心網友回復:
您需要將“ret”值與其關聯的影像“配對”。使用元組執行此操作并構建這些元組的串列。然后,您可以使用內置的對該串列進行排序。這是您適當編輯的代碼:
import glob
import sys
from PIL import Image
import cv2
dataset = "dataset/*.jpg"
queryImage = cv2.imread("query/12034.jpg", 0)
ret, thresh = cv2.threshold(queryImage, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
queryContour = contours[0]
min_dist = sys.maxsize
rets_images = []
for imagePath in glob.glob(dataset):
image = cv2.imread(imagePath, 0)
ret, th2 = cv2.threshold(image, 127, 255, 0)
contours, hierarchy = cv2.findContours(th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
ret = cv2.matchShapes(queryContour, cnt, 1, 0.0)
rets_images.append((ret, image))
print(ret)
rets_images.sort(key=lambda x: x[0])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482231.html
上一篇:按xy坐標排序
