Esempio n. 1
0
def setupuv(rc):
    """
    Horn Schunck legacy OpenCV function requires we use these old-fashioned cv matrices, not numpy array
    """
    if cv2 is not None:
        (r, c) = rc
        u = cv2.CreateMat(r, c, cv2.CV_32FC1)
        v = cv2.CreateMat(r, c, cv2.CV_32FC1)
        return (u, v)
    else:
        return [None] * 2
Esempio n. 2
0
def getDisparity(imgLeft, imgRight, method="BM"):

    gray_left = cv2.cvtColor(imgLeft, cv2.COLOR_BGR2GRAY)
    gray_right = cv2.cvtColor(imgRight, cv2.COLOR_BGR2GRAY)
    print(gray_left.shape)
    c, r = gray_left.shape
    if method == "BM":
        sbm = cv2.StereoBM_create()
        disparity = cv2.CreateMat(c, r, cv2.CV2_32F)
        sbm.SADWindowSize = 9
        sbm.preFilterType = 1
        sbm.preFilterSize = 5
        sbm.preFilterCap = 61
        sbm.minDisparity = -39
        sbm.numberOfDisparities = 112
        sbm.textureThreshold = 507
        sbm.uniquenessRatio = 0
        sbm.speckleRange = 8
        sbm.speckleWindowSize = 0

        gray_left = cv2.fromarray(gray_left)
        gray_right = cv2.fromarray(gray_right)

        cv2.FindStereoCorrespondenceBM(gray_left, gray_right, disparity, sbm)
        disparity_visual = cv2.CreateMat(c, r, cv2.CV2_8U)
        cv2.Normalize(disparity, disparity_visual, 0, 255, cv2.CV2_MINMAX)
        disparity_visual = np.array(disparity_visual)

    elif method == "SGBM":
        sbm = cv2.StereoSGBM()
        sbm.SADWindowSize = 9
        sbm.numberOfDisparities = 96
        sbm.preFilterCap = 63
        sbm.minDisparity = -21
        sbm.uniquenessRatio = 7
        sbm.speckleWindowSize = 0
        sbm.speckleRange = 8
        sbm.disp12MaxDiff = 1
        sbm.fullDP = False

        disparity = sbm.compute(gray_left, gray_right)
        disparity_visual = cv2.normalize(disparity,
                                         alpha=0,
                                         beta=255,
                                         norm_type=cv2.CV2_MINMAX,
                                         dtype=cv2.CV2_8U)

    return disparity_visual
Esempio n. 3
0
def FFT(image,flag = 0):
    w = image.width
    h = image.height
    iTmp = cv.CreateImage((w,h),cv.IPL_DEPTH_32F,1)
    cv.Convert(image,iTmp)
    iMat = cv.CreateMat(h,w,cv.CV_32FC2)
    mFFT = cv.CreateMat(h,w,cv.CV_32FC2)
    for i in range(h):
        for j in range(w):
            if flag == 0:
                num = -1 if (i+j)%2 == 1 else 1
            else:
                num = 1
            iMat[i,j] = (iTmp[i,j]*num,0)
    cv.DFT(iMat,mFFT,cv.CV_DXT_FORWARD)
    return mFFT
Esempio n. 4
0
def downsize_image(image):
    """ Resize the image to the given size
    Image must be of type cv.cvmat"""
    height_factor = float(image.height / parameter.max_facesize[0])
    width_factor = float(image.width / parameter.max_facesize[1])
    if height_factor > width_factor:
        new_face = cv.CreateMat(image.height / height_factor,
                                image.width / height_factor,
                                cv.GetElemType(image))
        downsize_factor = height_factor
    else:
        new_face = cv.CreateMat(int(image.height / width_factor),
                                int(image.width / width_factor),
                                cv.GetElemType(image))
        downsize_factor = width_factor
    cv.Resize(image, new_face)
    return new_face, downsize_factor
Esempio n. 5
0
def logpolar(src, center, magnitude_scale=40):

    mat1 = cv.fromarray(numpy.float64(src))
    mat2 = cv.CreateMat(src.shape[0], src.shape[1], mat1.type)

    cv.LogPolar(mat1, mat2, center, magnitude_scale, \
                cv.CV_INTER_CUBIC+cv.CV_WARP_FILL_OUTLIERS)

    return numpy.asarray(mat2)
def meanshiftUsingILM(path):
    # Load original image given the image path
    im = cv2.LoadImageM(path)
    # Load bank of filters
    filterBank = lmfilters.loadLMFilters()
    # Resize image to decrease dimensions during clustering
    resize_factor = 1
    thumbnail = cv2.CreateMat(im.height / resize_factor,
                              im.width / resize_factor, cv2.CV_8UC3)
    cv2.Resize(im, thumbnail)
    # now work with resized thumbnail image
    response = np.zeros(shape=((thumbnail.height) * (thumbnail.width), 4),
                        dtype=float)
    for f in range(0, 48):
        filter = filterBank[f]
        # Resize the filter with the same factor for the resized image
        dst = cv2.CreateImage(cv2.GetSize(thumbnail), cv2.IPL_DEPTH_32F, 3)
        resizedFilter = cv2.CreateMat(filter.height / resize_factor,
                                      filter.width / resize_factor,
                                      filter.type)
        cv2.Resize(filter, resizedFilter)
        # Apply the current filter
        cv2.Filter2D(thumbnail, dst, resizedFilter)
        featureIndex = getFilterTypeIndex(f)
        for j in range(0, thumbnail.height):
            for i in range(0, thumbnail.width):
                # Select the max. along the three channels
                maxRes = max(dst[j, i])
                if math.isnan(maxRes):
                    maxRes = 0.0
                if maxRes > response[thumbnail.width * j + i, featureIndex]:
                    # Store the max. response for the given feature index
                    response[thumbnail.width * j + i, featureIndex] = maxRes

    # Create new mean shift instance
    ms = MeanShift(bandwidth=10, bin_seeding=True)
    # Apply the mean shift clustering algorithm
    ms.fit(response)
    labels = ms.labels_
    n_clusters_ = np.unique(labels)
    print("Number of clusters: ", len(n_clusters_))
    repaintImage(thumbnail, labels)
    cv2.Resize(thumbnail, im)
    return im
Esempio n. 7
0
 def __init__(self, iplimage):
     # Rough-n-ready but it works dammit
     alpha = cv.CreateMat(iplimage.height, iplimage.width, cv.CV_8UC1)
     cv.Rectangle(alpha, (0, 0), (iplimage.width, iplimage.height),
                  cv.ScalarAll(255), -1)
     rgba = cv.CreateMat(iplimage.height, iplimage.width, cv.CV_8UC4)
     cv.Set(rgba, (1, 2, 3, 4))
     cv.MixChannels(
         [iplimage, alpha],
         [rgba],
         [
             (0, 0),  # rgba[0] -> bgr[2]
             (1, 1),  # rgba[1] -> bgr[1]
             (2, 2),  # rgba[2] -> bgr[0]
             (3, 3)  # rgba[3] -> alpha[0]
         ])
     self.__imagedata = rgba.tostring()
     super(IplQImage, self).__init__(self.__imagedata, iplimage.width,
                                     iplimage.height, QImage.Format_RGB32)
Esempio n. 8
0
def repeat():
    global capture
    global camera_index
    global count
    frame = cv2.GetMat(cv2.QueryFrame(capture))
    framegray = cv2.CreateMat(480, 640, cv2.CV_8UC1)
    cv2.CvtColor(frame, framegray, cv2.CV_BGR2GRAY)
    sys.stdout.write(framegray.tostring())
    c = cv2.WaitKey(1)
    if c == 27:
        print(count)
        sys.exit()
Esempio n. 9
0
def Filter(mat,flag = 0,num = 10):
    mFilter = cv.CreateMat(mat.rows,mat.cols,cv.CV_32FC2)
    for i in range(mat.rows):
        for j in range(mat.cols):
            if flag == 0:
                mFilter[i,j] = (0,0)
            else:
                mFilter[i,j] = mat[i,j]
    for i in range(mat.rows/2-num,mat.rows/2+num):
        for j in range(mat.cols/2-num,mat.cols/2+num):
            if flag == 0:
                mFilter[i,j] = mat[i,j]
            else:
                mFilter[i,j] = (0,0)
    return mFilter
Esempio n. 10
0
def npArray2cvMat(inputMat, dataType=cv.CV_32FC1):
    """
    This function is a utility for converting numpy arrays to the cv.cvMat format.

    Returns: cvMatrix
    """
    if (type(inputMat) == np.ndarray):
        sz = len(inputMat.shape)
        temp_mat = None
        if (dataType == cv.CV_32FC1 or dataType == cv.CV_32FC2
                or dataType == cv.CV_32FC3 or dataType == cv.CV_32FC4):
            temp_mat = np.array(inputMat, dtype='float32')
        elif (dataType == cv.CV_8UC1 or dataType == cv.CV_8UC2
              or dataType == cv.CV_8UC3 or dataType == cv.CV_8UC3):
            temp_mat = np.array(inputMat, dtype='uint8')
        else:
            logger.warning(
                "MatrixConversionUtil: the input matrix type is not supported")
            return None
        if (sz == 1):  #this needs to be changed so we can do row/col vectors
            retVal = cv.CreateMat(inputMat.shape[0], 1, dataType)
            cv.SetData(retVal, temp_mat.tostring(),
                       temp_mat.dtype.itemsize * temp_mat.shape[0])
        elif (sz == 2):
            retVal = cv.CreateMat(temp_mat.shape[0], temp_mat.shape[1],
                                  dataType)
            cv.SetData(retVal, temp_mat.tostring(),
                       temp_mat.dtype.itemsize * temp_mat.shape[1])
        elif (sz > 2):
            logger.warning(
                "MatrixConversionUtil: the input matrix type is not supported")
            return None
        return retVal
    else:
        logger.warning(
            "MatrixConversionUtil: the input matrix type is not supported")
Esempio n. 11
0
def enlarge_image(image, factor):
    """ Enlarge the image to the given size
    Image must be of type cv.cvmat"""

    if type(image).__name__ == 'cvmat':
        new_image = cv.CreateMat(int(round(image.height * factor)),
                                 int(round(image.width * factor)),
                                 cv.GetElemType(image))
        cv.Resize(image, new_image)
        image = new_image
        logging.debug(
            'Image has been enlarged with factor %.3f (face-detector.py)' %
            (factor))
        return image
    else:
        logging.error('Unkown Image Type (tools.py)')
def meanshiftUsingPCA(path):
    # Load original image given the image path
    im = cv2.LoadImageM(path)
    #convert image to YUV color space
    cv.CvtColor(im, im, cv2.CV_BGR2YCrCb)
    # Load bank of filters
    filterBank = lmfilters.loadLMFilters()
    # Resize image to decrease dimensions during clustering
    resize_factor = 1
    thumbnail = cv2.CreateMat(im.height / resize_factor,
                              im.width / resize_factor, cv2.CV_8UC3)
    cv2.Resize(im, thumbnail)
    # now work with resized thumbnail image
    response = np.zeros(shape=((thumbnail.height) * (thumbnail.width), 51),
                        dtype=float)
    for f in range(0, 48):
        filter = filterBank[f]
        # Resize the filter with the same factor for the resized image
        dst = cv2.CreateImage(cv2.GetSize(thumbnail), cv2.IPL_DEPTH_32F, 3)
        resizedFilter = cv2.CreateMat(filter.height / resize_factor,
                                      filter.width / resize_factor,
                                      filter.type)
        cv2.Resize(filter, resizedFilter)
        # Apply the current filter
        cv2.Filter2D(thumbnail, dst, resizedFilter)
        for j in range(0, thumbnail.height):
            for i in range(0, thumbnail.width):
                # Select the max. along the three channels
                maxRes = max(dst[j, i])
                if math.isnan(maxRes):
                    maxRes = 0.0
                if maxRes > response[thumbnail.width * j + i, f]:
                    # Store the max. response for the given feature index
                    response[thumbnail.width * j + i, f] = maxRes

    #YUV features
    count = 0
    for j in range(0, thumbnail.height):
        for i in range(0, thumbnail.width):
            response[count, 48] = thumbnail[j, i][0]
            response[count, 49] = thumbnail[j, i][1]
            response[count, 50] = thumbnail[j, i][2]
            count += 1

    #get the first 4 primary components using pca
    pca = PCA(response)
    pcaResponse = zeros([thumbnail.height * thumbnail.width, 4])

    for i in range(0, thumbnail.height * thumbnail.width):
        pcaResponse[i] = pca.getPCA(response[i], 4)

    # Create new mean shift instance
    ms = MeanShift(bandwidth=10, bin_seeding=True)
    # Apply the mean shift clustering algorithm
    ms.fit(pcaResponse)
    labels = ms.labels_
    n_clusters_ = np.unique(labels)
    print("Number of clusters: ", len(n_clusters_))
    repaintImage(thumbnail, labels)
    cv2.Resize(thumbnail, im)
    return im
 def buildMat(self, width, height, matType=cv2.CV_8UC1):
     matTmp = cv2.CreateMat(width, height, matType)
     return cv2.zeros(matTemp, matTmp.size(), matTmp.type())
    def process_image(self, slider_pos):
        global cimg, source_image1, ellipse_size, maxf, maxs, eoc, lastcx,lastcy,lastr
        """
        This function finds contours, draws them and their approximation by ellipses.
        """
        stor = cv.CreateMemStorage()

        # Create the destination images
        cimg = cv.CloneImage(self.source_image)
        cv.Zero(cimg)
        image02 = cv.CloneImage(self.source_image)
        cv.Zero(image02)
        image04 = cv.CreateImage(cv.GetSize(self.source_image), cv.IPL_DEPTH_8U, 3)
        cv.Zero(image04)

        # Threshold the source image. This needful for cv.FindContours().
        cv.Threshold(self.source_image, image02, slider_pos, 255, cv.CV_THRESH_BINARY)

        # Find all contours.
        cont = cv.FindContours(image02,
            stor,
            cv.CV_RETR_LIST,
            cv.CV_CHAIN_APPROX_NONE,
            (0, 0))

        maxf = 0
        maxs = 0
        size1 = 0

        for c in contour_iterator(cont):
            if len(c) > ellipse_size:
                PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)
                for (i, (x, y)) in enumerate(c):
                    PointArray2D32f[0, i] = (x, y)


                # Draw the current contour in gray
                gray = cv.CV_RGB(100, 100, 100)
                cv.DrawContours(image04, c, gray, gray,0,1,8,(0,0))

                if iter == 0:
                    strng = segF + '/' + 'contour1.png'
                    cv.SaveImage(strng,image04)
                color = (255,255,255)

                (center, size, angle) = cv.FitEllipse2(PointArray2D32f)

                # Convert ellipse data from float to integer representation.
                center = (cv.Round(center[0]), cv.Round(center[1]))
                size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))

                if iter == 1:
                    if size[0] > size[1]:
                        size2 = size[0]
                    else:
                        size2 = size[1]

                    if size2 > size1:
                        size1 = size2
                        size3 = size

                # Fits ellipse to current contour.
                if eoc == 0 and iter == 2:
                    rand_val = abs((lastr - ((size[0]+size[1])/2)))
                    if rand_val > 20 and float(max(size[0],size[1]))/float(min(size[0],size[1])) < 1.5:
                        lastcx = center[0]
                        lastcy = center[1]
                        lastr = (size[0]+size[1])/2

                    if rand_val > 20 and float(max(size[0],size[1]))/float(min(size[0],size[1])) < 1.4:
                        cv.Ellipse(cimg, center, size,
                                  angle, 0, 360,
                                  color,2, cv.CV_AA, 0)
                        cv.Ellipse(source_image1, center, size,
                                  angle, 0, 360,
                                  color,2, cv.CV_AA, 0)

                elif eoc == 1 and iter == 2:
                    (int,cntr,rad) = cv.MinEnclosingCircle(PointArray2D32f)
                    cntr = (cv.Round(cntr[0]), cv.Round(cntr[1]))
                    rad = (cv.Round(rad))
                    if maxf == 0 and maxs == 0:
                        cv.Circle(cimg, cntr, rad, color, 1, cv.CV_AA, shift=0)
                        cv.Circle(source_image1, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        maxf = rad
                    elif (maxf > 0 and maxs == 0) and abs(rad - maxf) > 30:
                        cv.Circle(cimg, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        cv.Circle(source_image1, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        maxs = len(c)
        if iter == 1:
            temp3 = 2*abs(size3[1] - size3[0])
            if (temp3 > 40):
                eoc = 1
Esempio n. 15
0
def IFFT(mat):
    mIFFt = cv.CreateMat(mat.rows,mat.cols,cv.CV_32FC2)
    cv.DFT(mat,mIFFt,cv.CV_DXT_INVERSE)
    return mIFFt