cv2.saliency上应用k均值时出现问题 [英] problem while applying k-means on cv2.saliency

查看:68
本文介绍了cv2.saliency上应用k均值时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究能发现人的项目.因此,我在opencv中使用显着性,并对显着性的输出应用k-means聚类.

I'm working on project which detects people. So I'm using saliency in opencv and applying k-means clustering on the output of the saliency.

问题是应用k均值聚类后的输出完全是黑的

The problem is the output after applying k-means clustering is totally black

这是代码:

import cv2
import time
import numpy as np

cap=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'):
        break

cap.release()
cv2.destroyAllWindows()

这是我测试算法的视频的链接. 任何人都可以指出任何错误或更正吗?

This is the link of the video on which I tested the algorithm. Can anyone please point out any mistake or correction?

谢谢.

推荐答案

创建映射后,关键是将格式转换为uint8,并将强度缩放255.您是针对第一种类型的显着性图执行此操作的,而不是针对第二种类型:

The key is converting the format into uint8 and scaling the intensities by 255 after you create the map. You did that for the first type of saliency map but not the second:

saliency = cv2.saliency.StaticSaliencyFineGrained_create()
(success, saliencyMap) = saliency.computeSaliency(image)
### ADDED
saliencyMap = (saliencyMap * 255).astype("uint8")

这篇关于cv2.saliency上应用k均值时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆