Search code examples
pythonopencvk-means

problem while applying k-means on cv2.saliency


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.

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

Here is the code:

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?

Thanks in advance.


Solution

  • 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")