我正在研究一个检测人的项目。因此,我在 opencv 中使用显着性,并对显着性的输出应用 k 均值聚类。问题是应用 k-means 聚类后的输出是全黑的这是代码:import cv2import timeimport numpy as npcap=cv2.VideoCapture("video.avi")while(cap.isOpened()): #time.sleep(0.05) _,frame=cap.read() image=frame saliency = cv2.saliency.StaticSaliencySpectralResidual_create() (success, saliencyMap) = saliency.computeSaliency(image) saliencyMap = (saliencyMap * 255).astype("uint8") #cv2.imshow("Image", image) #cv2.imshow("Output", saliencyMap) saliency = cv2.saliency.StaticSaliencyFineGrained_create() (success, saliencyMap) = saliency.computeSaliency(image) threshMap = cv2.threshold(saliencyMap.astype("uint8"), 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] # show the images #cv2.imshow("Image", image) cv2.imshow("saliency", saliencyMap) #cv2.imshow("Thresh", threshMap) ##############implementing k-means clustering####################### kouts=saliencyMap clusters=7 z=kouts.reshape((-1,3)) z=np.float32(z) criteria= (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,10,1.0) ret,label,center=cv2.kmeans(z,clusters,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) center=np.uint8(center) res=center[label.flatten()] kouts=res.reshape((kouts.shape)) cv2.imshow('clustered image',kouts) k = cv2.waitKey(1) & 0xff if k == ord('q'): breakcap.release()cv2.destroyAllWindows()有人可以指出任何错误或更正吗?
1 回答
SMILET
TA贡献1796条经验 获得超4个赞
键是在创建贴图后将格式转换为uint8255,并将强度缩放为 255。您对第一种类型的显着图执行了此操作,但对第二种类型没有执行此操作:
saliency = cv2.saliency.StaticSaliencyFineGrained_create()
(success, saliencyMap) = saliency.computeSaliency(image)
### ADDED
saliencyMap = (saliencyMap * 255).astype("uint8")
添加回答
举报
0/150
提交
取消