def getSaliencyFrame(frame): widthFrame = len(frame[0]) heightFrame = len(frame) cv2.imshow('frame', frame) #compress the frame frame = cv2.resize(frame, (64, 64), interpolation=cv2.INTER_CUBIC) # fourier analyse imageFourier = np.fft.fft2(frame) # show imageFourier showPlot(imageFourier) cv2.imshow('fourier', np.abs(imageFourier)) # we now have the spectrum analyse # by using convolve we can blur the frame imageConvolve = convolutions.convolve(frame, np.abs(kernel(9))) # take the fourier analyse of the convolve fourierConvolve = np.fft.fft2(imageConvolve) showPlot(fourierConvolve) cv2.imshow('averagedSpectrum', np.abs(fourierConvolve)) #substract two images spectralResidual = np.subtract(fourierConvolve, imageFourier) showPlot(spectralResidual) # inverse fourier analyse saliencyImage = np.fft.ifft2(spectralResidual) showPlot(saliencyImage) saliencyImage = cv2.resize(np.abs(saliencyImage), (widthFrame, heightFrame)) cv2.imshow('saliency', saliencyImage) # while True: # if cv2.waitKey(1) & 0xFF == ord('q'): # break return saliencyImage
def main(): image = scipy.misc.imread('image.jpg') width, height, num_colors = image.shape im = np.zeros([num_colors, width, height]) for i in xrange(num_colors): im[i, :, :] = image[:, :, i] w1 = np.array([ [0, -1, 0], [-1, 4, -1], [0, -1, 0], ]) w2 = np.array([ [-1, 0, 1], [-2, 0, 2], [-1, 0, 1], ]) w3 = np.array([ [-1, -2, -1], [0, 0, 0], [1, 2, 1], ]) w = np.zeros([3, 3, 3]) w[0, :, :] = w1 w[1, :, :] = w2 w[2, :, :] = w3 kernel_size = w.shape[1] padding = kernel_size / 2 w_flip = w[:, ::-1, ::-1] result = convolutions.convolve(im[np.newaxis, :, :, :], w_flip[:, np.newaxis, :, :], padding=padding) plt.subplot(2, 2, 1) plt.imshow(image) for k in xrange(3): res = result[k, :, :, :] res -= res.min() res /= res.max() res *= 255 r = np.zeros([width, height, num_colors], dtype=np.uint8) for i in xrange(num_colors): r[:, :, i] = res[i, :, :] plt.subplot(2, 2, 2 + k) plt.imshow(r) plt.show()
scaleFrameX = widthFrame / 64 scaleFrameY = int((64 / widthFrame) * heightFrame) cv2.imshow('frame', frame) frame = cv2.resize(frame, (64, 64), interpolation=cv2.INTER_CUBIC) # frame = convolutions.convolve(frame, np.abs(kernel(5))) showPlot(np.zeros((5, 5))) imageFourier = np.fft.fft2(frame) logImageFourier = np.log(imageFourier) showPlot(imageFourier) cv2.imshow('fourier', np.abs(imageFourier)) # we now have the spectrum analyse # wich needs to be logaritmic: imageConvolve = convolutions.convolve(frame, np.abs(kernel(9))) cv2.imshow('org', np.abs(frame)) cv2.imshow('blur', np.abs(imageConvolve)) fourierConvolve = np.fft.fft2(imageConvolve) logFourierConvolve = np.log(fourierConvolve) showPlot(fourierConvolve) cv2.imshow('averagedSpectrum', np.abs(fourierConvolve)) # averagedSpectrum = np.multiply(imageConvolve, logSpectrum) # showPlot(averagedSpectrum) phase = np.angle(imageFourier) spectralResidual = np.subtract(logFourierConvolve, logImageFourier) spectralResidual = np.exp(spectralResidual + 1j * phase)**2 showPlot(spectralResidual) saliencyImage = np.fft.ifft2(spectralResidual)