def hue_histogram_as_image(hist): histimg_hsv = cv.CreateImage((640, 480), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w, h = cv.GetSize(histimg_hsv) hdims = int(cv.GetDims(mybins)[0]) for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1) cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1) histimg = cv.CreateImage((320, 200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) return histimg
def setup_camshift(roi_selection, img): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) # backproject_mode = False histimg_hsv = cv.CreateImage((320, 200), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w, h = cv.GetSize(histimg_hsv) hdims = cv.GetDims(mybins)[0] for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1) cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1) histimg = cv.CreateImage((320, 200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) # making_selection # print roi_selection[0] # print roi_selection[1] point1 = roi_selection[0] point2 = roi_selection[1] xmin = point1[0] ymin = point1[1] xmax = point2[0] ymax = point2[1] widthx = xmax - xmin heighty = ymax - ymin selection = (xmin, ymin, widthx, heighty) return selection, hist
# use nonzero_rows parameter in cv.FT() call below cv.DFT(dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height) cv.NamedWindow("win", 0) cv.NamedWindow("magnitude", 0) cv.ShowImage("win", im) # Split Fourier in real and imaginary parts cv.Split(dft_A, image_Re, image_Im, None, None) # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2) cv.Pow(image_Re, image_Re, 2.0) cv.Pow(image_Im, image_Im, 2.0) cv.Add(image_Re, image_Im, image_Re, None) cv.Pow(image_Re, image_Re, 0.5) # Compute log(1 + Mag) cv.AddS(image_Re, cv.ScalarAll(1.0), image_Re, None) # 1 + Mag cv.Log(image_Re, image_Re) # log(1 + Mag) # Rearrange the quadrants of Fourier image so that the origin is at # the image center cvShiftDFT(image_Re, image_Re) min, max, pt1, pt2 = cv.MinMaxLoc(image_Re) cv.Scale(image_Re, image_Re, 1.0 / (max - min), 1.0 * (-min) / (max - min)) cv.ShowImage("magnitude", image_Re) cv.WaitKey(0)
def atualiza_foto(self): real = cv.CreateImage(cv.GetSize(imagem), cv.IPL_DEPTH_64F, 1) imaginario = cv.CreateImage(cv.GetSize(imagem), cv.IPL_DEPTH_64F, 1) complexo = cv.CreateImage(cv.GetSize(imagem), cv.IPL_DEPTH_64F, 2) cv.Scale(imagem_cinza, real, 1.0, 0.0) cv.Zero(imaginario) cv.Merge(real, imaginario, None, None, complexo) Altura_M = cv.GetOptimalDFTSize(imagem.height - 1) Largura_N = cv.GetOptimalDFTSize(imagem.width - 1) Vetor_dft = cv.CreateMat(Altura_M, Largura_N, cv.CV_64FC2) imagem_Real = cv.CreateImage((Largura_N, Altura_M), cv.IPL_DEPTH_64F, 1) imagem_Imaginaria = cv.CreateImage((Largura_N, Altura_M), cv.IPL_DEPTH_64F, 1) temporario = cv.GetSubRect(Vetor_dft, (0, 0, imagem.width, imagem.height)) cv.Copy(complexo, temporario, None) if (Vetor_dft.width > imagem.width): temporario = cv.GetSubRect( Vetor_dft, (imagem.width, 0, Largura_N - imagem.width, imagem.height)) cv.Zero(temporario) # APLICANDO FOURIER cv.DFT(Vetor_dft, Vetor_dft, cv.CV_DXT_FORWARD, complexo.height) cv.Split(Vetor_dft, imagem_Real, imagem_Imaginaria, None, None) cv.Pow(imagem_Real, imagem_Real, 2.0) cv.Pow(imagem_Imaginaria, imagem_Imaginaria, 2.0) cv.Add(imagem_Real, imagem_Imaginaria, imagem_Real, None) cv.Pow(imagem_Real, imagem_Real, 0.5) cv.AddS(imagem_Real, cv.ScalarAll(1.0), imagem_Real, None) cv.Log(imagem_Real, imagem_Real) cvShiftDFT(imagem_Real, imagem_Real) min, max, pt1, pt2 = cv.MinMaxLoc(imagem_Real) cv.Scale(imagem_Real, imagem_Real, 1.0 / (max - min), 1.0 * (-min) / (max - min)) #APLICANDO FILTRO passa-baixa circular cv.Circle(Vetor_dft, (0, 0), self.raio, [0, 0, 0], -1, 1, 0) cv.Circle(Vetor_dft, (Vetor_dft.cols, 0), self.raio, [0, 0, 0], -1, 1, 0) cv.Circle(Vetor_dft, (0, Vetor_dft.rows), self.raio, [0, 0, 0], -1, 1, 0) cv.Circle(Vetor_dft, (Vetor_dft.cols, Vetor_dft.rows), self.raio, [0, 0, 0], -1, 1, 0) cv.Split(Vetor_dft, imagem_Real, imagem_Imaginaria, None, None) cv.Pow(imagem_Real, imagem_Real, 2.0) cv.Pow(imagem_Imaginaria, imagem_Imaginaria, 2.0) cv.Add(imagem_Real, imagem_Imaginaria, imagem_Real, None) cv.Pow(imagem_Real, imagem_Real, 0.5) cv.AddS(imagem_Real, cv.ScalarAll(1.0), imagem_Real, None) cv.Log(imagem_Real, imagem_Real) cvShiftDFT(imagem_Real, imagem_Real) min, max, pt1, pt2 = cv.MinMaxLoc(imagem_Real) cv.Scale(imagem_Real, imagem_Real, 1.0 / (max - min), 1.0 * (-min) / (max - min)) cv.ShowImage("Transformada de Fourier", imagem_Real) # APLICANDO A INVERSA de Fourier cv.DFT(Vetor_dft, Vetor_dft, cv.CV_DXT_INVERSE_SCALE, Largura_N) cv.Split(Vetor_dft, imagem_Real, imagem_Imaginaria, None, None) min, max, pt1, pt2 = cv.MinMaxLoc(imagem_Real) if ((pt1 < 0) or (pt2 > 255)): cv.Scale(imagem_Real, imagem_Real, 1.0 / (max - min), 1.0 * (-min) / (max - min)) else: cv.Scale(imagem_Real, imagem_Real, 1.0 / 255, 0) cv.ShowImage("Inversa da Fourier", imagem_Real)
def opencvSaliency(self, scaledImageGray): cvImageGray = cv.CreateMat(scaledImageGray.height, scaledImageGray.width, cv.CV_32FC1) cv.Convert(scaledImageGray, cvImageGray) src = cvImageGray dftWidth = cv.GetOptimalDFTSize(src.width - 1) dftHeight = cv.GetOptimalDFTSize(src.height - 1) real = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC1) imaginary = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC1) dft = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC2) tmp = cv.GetSubRect(real, (0, 0, src.width, src.height)) cv.Copy(src, tmp) cv.Zero(imaginary) cv.Merge(real, imaginary, None, None, dft) # do the fft cv.DFT(dft, dft, cv.CV_DXT_FORWARD, src.height) cv.Split(dft, real, imaginary, None, None) cv.CartToPolar(real, imaginary, real, imaginary, 0) cv.Log(real, real) filtered = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC1) cv.Copy(real, filtered) cv.Smooth(filtered, filtered, cv.CV_BLUR) cv.Sub(real, filtered, real, None) cv.Exp(real, real) cv.PolarToCart(real, imaginary, real, imaginary, 0) #cv.PolarToCart( np.ones( shape=(dftHeight,dftWidth), dtype=np.float32 ), imaginary, real, imaginary,0 ) # do inverse fourier transform cv.Merge(real, imaginary, None, None, dft) cv.DFT(dft, dft, cv.CV_DXT_INV_SCALE, src.height) cv.Split(dft, real, imaginary, None, None) # get magnitude cv.CartToPolar(real, imaginary, real, None, 0) cv.Pow(real, real, 2.0) FILTER_RAD = 3 IPL_BORDER_CONSTANT = 0 sfiltered = cv.CreateMat(real.height + FILTER_RAD * 2, real.width + FILTER_RAD * 2, cv.CV_32FC1) cv.CopyMakeBorder(real, sfiltered, (FILTER_RAD, FILTER_RAD), IPL_BORDER_CONSTANT) cv.Smooth(sfiltered, sfiltered, cv.CV_GAUSSIAN, 2 * FILTER_RAD + 1) (min, max, minLoc, maxLoc) = cv.MinMaxLoc(sfiltered) cv.ConvertScale(sfiltered, sfiltered, 1 / (max - min), -min / (max - min)) # copy result to output image tmp = cv.GetSubRect(sfiltered, (FILTER_RAD, FILTER_RAD, src.width, src.height)) cv.Copy(tmp, cvImageGray) #cvReleaseMat(&sfiltered); #cvReleaseMat(&real); #cvReleaseMat(&filtered); #cvReleaseMat(&imaginary); #cvReleaseMat(&dft); saliencyMap = np.array(255.0 * np.array(cvImageGray), dtype=np.uint8) return saliencyMap