Example #1
0
def edge_threshold(image, roi=None, debug=0):
    thresholded = cv.CloneImage(image)
    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    vertical = cv.CloneImage(horizontal)
    v_edge = cv.CloneImage(image)
    magnitude = cv.CloneImage(horizontal)

    storage = cv.CreateMemStorage(0)
    mag = cv.CloneImage(image)
    cv.Sobel(image, horizontal, 0, 1, 1)
    cv.Sobel(image, vertical, 1, 0, 1)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)

    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, mag)
    if roi:
        cv.And(mag, roi, mag)
    cv.Normalize(mag, mag, 0, 255, cv.CV_MINMAX, None)
    cv.Threshold(mag, mag, 122, 255, cv.CV_THRESH_BINARY)
    draw_image = cv.CloneImage(image)
    and_image = cv.CloneImage(image)
    results = []

    threshold_start = 17
    for window_size in range(threshold_start, threshold_start + 1, 1):
        r = 20
        for threshold in range(0, r):
            cv.AdaptiveThreshold(image, thresholded, 255, \
                cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, window_size, threshold)
            contour_image = cv.CloneImage(thresholded)
            contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST)
            cv.Zero(draw_image)
            cv.DrawContours(draw_image, contours, (255, 255, 255),
                            (255, 255, 255), 1, 1)
            if roi:
                cv.And(draw_image, roi, draw_image)
            cv.And(draw_image, mag, and_image)
            m1 = np.asarray(cv.GetMat(draw_image))
            m2 = np.asarray(cv.GetMat(mag))
            total = mag.width * mag.height  #cv.Sum(draw_image)[0]

            coverage = cv.Sum(and_image)[0] / (mag.width * mag.height)
            if debug:
                print threshold, coverage
                cv.ShowImage("main", draw_image)
                cv.ShowImage("main2", thresholded)
                cv.WaitKey(0)
            results.append((coverage, threshold, window_size))

    results.sort(lambda x, y: cmp(y, x))
    _, threshold, window_size = results[0]
    cv.AdaptiveThreshold(image, thresholded, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, \
        cv.CV_THRESH_BINARY, window_size, threshold)

    return thresholded
Example #2
0
def ccoeff_normed(img1, img2):
	size = cv.GetSize(img1)
	tmp1 = float_version(img1)
	tmp2 = float_version(img2)

	cv.SubS(tmp1, cv.Avg(tmp1), tmp1)
	cv.SubS(tmp2, cv.Avg(tmp2), tmp2)

	norm1 = cv.CloneImage(tmp1)
	norm2 = cv.CloneImage(tmp2)
	cv.Pow(tmp1, norm1, 2.0)
	cv.Pow(tmp2, norm2, 2.0)

	#cv.Mul(tmp1, tmp2, tmp1)

	return cv.DotProduct(tmp1, tmp2) /  (cv.Sum(norm1)[0]*cv.Sum(norm2)[0])**0.5
Example #3
0
def avgstd_image_list(images):
    mean = None
    std = None
    if len(images) > 0:
        scale = 1. / len(images)
        mean = cv.CreateImage(cv.GetSize(images[0]), cv.IPL_DEPTH_32F,
                              images[0].channels)
        std = cv.CreateImage(cv.GetSize(images[0]), cv.IPL_DEPTH_32F,
                             images[0].channels)
        buf = cv.CreateImage(cv.GetSize(images[0]), cv.IPL_DEPTH_32F,
                             images[0].channels)
        for image in images:
            cv.Add(image, mean, mean)
            cv.Mul(image, image, buf)
            cv.Add(buf, std, std)
        cv.ConvertScale(mean, mean, scale)
        cv.ConvertScale(std, std, scale)
        cv.Mul(mean, mean, buf)
        cv.Sub(std, buf, std)
        cv.Pow(std, std, 0.5)

        meanresult = cv.CreateImage(cv.GetSize(images[0]), images[0].depth,
                                    images[0].channels)
        stdresult = cv.CreateImage(cv.GetSize(images[0]), images[0].depth,
                                   images[0].channels)
        cv.ConvertScale(mean, meanresult)
        cv.ConvertScale(std, stdresult)
        del buf
        del std
        del mean
    return (meanresult, stdresult)
Example #4
0
def edge_magnitude(image):
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    vertical = cv.CloneImage(horizontal)
    magnitude = cv.CloneImage(horizontal)

    mag = cv.CloneImage(image)
    cv.Sobel(image, horizontal, 0, 1, 1)
    cv.Sobel(image, vertical, 1, 0, 1)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)

    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, mag)
    return mag
Example #5
0
def ssdScore(f1, f2):
    global size  #size o f SSD window
    #subtracts f2 from f1
    sub_f1_f2 = cv.CreateMat(size, size, cv.CV_64FC1)
    cv.Sub(f1, f2, sub_f1_f2)

    #square and add
    f1_f2_square = cv.CreateMat(size, size, cv.CV_64FC1)
    cv.Pow(sub_f1_f2, f1_f2_square, 2)
    score = cv.Sum(f1_f2_square)
    return score[0] / (size * size)
def project_pixels_to_3d_rays(pixels, model):
    x = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1)
    y = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1)
    cv.Split(pixels, x, y, None, None)

    x_squared = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1)
    cv.Pow(x, x_squared, 2)

    y_squared = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1)
    cv.Pow(y, y_squared, 2)

    inverse_norm = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1)
    cv.Add(x_squared, y_squared, inverse_norm)
    cv.AddS(inverse_norm, 1, inverse_norm)
    cv.Pow(inverse_norm, inverse_norm, -0.5)

    cv.Mul(x, inverse_norm, x)
    cv.Mul(y, inverse_norm, y)

    result = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC3)
    cv.Merge(x, y, inverse_norm, None, result)
    return result
Example #7
0
def pixel_distance_matrix(images):
    buf = cv.CreateImage(cv.GetSize(images[0]), images[0].depth,
                         images[0].channels)
    distances = np.zeros((len(images), len(images)))
    for i in xrange(len(images)):
        for j in xrange(i + 1, len(images)):
            cv.Sub(images[i], images[j], buf)
            cv.Pow(buf, buf, 2)
            distance = cv.Sum(buf)[0]
            distances[i, j] = distance
            distances[j, i] = distance
    del buf
    return distances
Example #8
0
def segment_rect(image,
                 rect,
                 debug=False,
                 display=None,
                 target_size=None,
                 group_range=(3, 25)):
    global next
    skip = False
    best_chars = []
    best_threshold = None
    thresholded = cv.CloneImage(image)
    contour_image = cv.CloneImage(image)
    edges = cv.CloneImage(image)

    min_x, min_y, width, height = rect
    # cv.SetImageROI(thresholded, rect)
    cv.SetImageROI(contour_image, rect)
    cv.SetImageROI(image, rect)
    cv.SetImageROI(edges, rect)

    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    vertical = cv.CloneImage(horizontal)
    magnitude = cv.CloneImage(horizontal)
    cv.Sobel(image, horizontal, 0, 1, 3)
    cv.Sobel(image, vertical, 1, 0, 3)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)
    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, edges)

    original_rect = rect
    if display:
        cv.SetImageROI(display, rect)
    for threshold in range(1, 20, 1):
        cv.SetImageROI(thresholded, original_rect)
        #for i in range(30, 60, 1):
        if display:
            cv.Merge(image, image, image, None, display)
        cv.Copy(image, thresholded)
        #cv.Threshold(thresholded, thresholded, i, 255, cv.CV_THRESH_BINARY_INV)
        cv.AdaptiveThreshold(thresholded, thresholded, 255,
                             cv.CV_ADAPTIVE_THRESH_MEAN_C,
                             cv.CV_THRESH_BINARY_INV, 17, threshold)
        #cv.AdaptiveThreshold(thresholded, thresholded, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 5, i)
        # skip rects greater than 50% thresholded
        summed = cv.Norm(thresholded, None, cv.CV_L1,
                         None) / 255 / thresholded.width / thresholded.height
        if summed > 0.5:
            continue
        if debug:
            cv.ShowImage("edge", thresholded)
        storage = cv.CreateMemStorage(0)
        cv.Copy(thresholded, contour_image)
        contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST,
                                   cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))
        ext.filter_contours(contours, 20, ext.LESSTHAN)
        groups = []
        rects = []
        edge_counts = []
        overlappings = {}
        if contours:
            seq = contours
            while seq:
                c = ext.as_contour(ext.wrapped(seq))
                r = (c.rect.x, c.rect.y, c.rect.width, c.rect.height)
                rects.append(r)
                seq = seq.h_next()
            similarity = 0.45  #0.3
            rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3]))
            for rect in rects:
                if debug:
                    print
                    print "R", rect, len(groups)
                cv.SetImageROI(edges,
                               (original_rect[0] + rect[0],
                                original_rect[1] + rect[1], rect[2], rect[3]))
                edge_count = cv.Sum(edges)[0] / 255 / (rect[2] * rect[3])
                edge_counts.append(edge_count)
                #                cv.ShowImage("edges", edges)
                #                cv.WaitKey(0)
                if debug and target_size:
                    print "X", target_size, rect
                    print(target_size[0] - rect[2]) / target_size[0]
                    print(target_size[1] - rect[3]) / target_size[1]
                if rect[2] > rect[3] or float(rect[3])/rect[2] < 3./3 or edge_count < 0.1\
                or (rect[2] == image.width and rect[3] == image.height) \
                or (target_size and not 0 < (target_size[0] - rect[2]) / target_size[0] < 0.3 \
                and not 0 < (target_size[1] - rect[3]) / target_size[1] < 0.05):
                    if debug:
                        print "rej", rect[2], ">", rect[3], "edge=", edge_count
                        cv.Rectangle(display, (rect[0], rect[1]),
                                     (rect[0] + rect[2], rect[1] + rect[3]),
                                     (0, 0, 255), 1)
                        cv.ShowImage("main", display)
                        if not skip and not next:
                            c = cv.WaitKey(0)
                            if c == ord("a"):
                                skip = True
                            if c == ord("z"):
                                next = True
                    continue
                added = False
                for group_id, group in enumerate(groups):
                    avg_width, avg_height, avg_y = 0, 0, 0
                    overlap = None
                    c = 0
                    for r in group:
                        avg_y += r[1] + r[3] / 2.0
                        avg_width += r[2]
                        avg_height += r[3]
                        irect = intersect(r, rect)
                        if irect[2] * irect[3] > 0.2 * r[2] * r[3]:
                            overlappings.setdefault(group_id,
                                                    []).append([r, rect])
                    avg_y /= float(len(group))
                    avg_width /= float(len(group))
                    avg_height /= float(len(group))
                    if debug:
                        print group
                    if (abs(avg_width - rect[2]) / avg_width < similarity or \
                     (rect[2] < avg_width)) and \
                    abs(avg_height - rect[3])/ avg_height < similarity and \
                    abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity:
                        group.append(rect)
                        added = True
                    else:
                        pass
                if not added:
                    # first char in group
                    groups.append([rect])
                if debug:
                    print "now:"
                    for g in groups:
                        print g
                    cv.Rectangle(display, (rect[0], rect[1]),
                                 (rect[0] + rect[2], rect[1] + rect[3]),
                                 (255, 0, 0), 1)
                    cv.ShowImage("main", display)
                    if not skip and not next:
                        c = cv.WaitKey(0)
                        if c == ord("a"):
                            skip = True
                        if c == ord("z"):
                            next = True
        if groups:
            #handle overlapping regions, default to average width match
            for group_id, over in overlappings.items():
                group = groups[group_id]
                avg_width = 0
                avg_height = 0
                for r in group:
                    avg_width += r[2]
                    avg_height += r[3]
                avg_width /= float(len(group))
                avg_height /= float(len(group))
                for r1, r2 in over:
                    if r2 not in group or r1 not in group:
                        continue
                    if debug:
                        print "over", r1, r2, r1[2] * r1[3], r2[2] * r2[
                            3], avg_width
                    d1 = abs(r1[2] - avg_width) + abs(r1[3] - avg_height)
                    d2 = abs(r2[2] - avg_width) + abs(r2[3] - avg_height)
                    if d1 < d2:
                        group.remove(r2)
                    else:
                        group.remove(r1)

            #group = max(groups, key=len)
            # from longest groups, find largest area
            groups.sort(key=len)
            groups.reverse()
            max_area = 0
            mad_index = -1
            for i, g in enumerate(groups[:5]):
                area = 0
                for r in g:
                    area += r[2] * r[3]
                if area > max_area:
                    max_area = area
                    max_index = i
            group = groups[max_index]
            # vertical splitting
            avg_width, avg_height, avg_y = 0, 0, 0
            if debug:
                print "G", group
            for r in group:
                avg_y += r[1] + r[3] / 2.0
                avg_width += r[2]
                avg_height += r[3]
            avg_y /= float(len(group))
            avg_width /= float(len(group))
            avg_height /= float(len(group))
            band_rects = []
            bound = bounding_rect(group)
            for i, rect in enumerate(rects):
                if edge_counts[i] < 0.1:
                    continue
                if (abs(avg_width - rect[2]) / avg_width < similarity or \
                 (rect[2] < avg_width)) and \
                 (abs(avg_height - rect[3]) / avg_height < similarity or  \
                 (rect[3] < avg_height)) and \
                abs(avg_y - (rect[1] + rect[3]/2.0)) < avg_height/2:
                    band_rects.append(rect)

            band_rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3]))

            for i, rect_a in enumerate(band_rects[:-1]):
                if rect_a[2] * rect_a[3] < 0.2 * avg_width * avg_height:
                    continue
                merge_rects = []
                for rect_b in band_rects[i + 1:]:
                    w = avg_width
                    m1 = rect_a[0] + rect_a[2] / 2
                    m2 = rect_b[0] + rect_b[2] / 2
                    if abs(m1 - m2) < w:
                        merge_rects.append(rect_b)
                if debug:
                    print "M", merge_rects
                if merge_rects:
                    merge_rects.append(rect_a)
                    rect = bounding_rect(merge_rects)
                    area = 0
                    for r in merge_rects:
                        area += r[2] * r[3]
                    if (abs(avg_width - rect[2]) / avg_width < similarity or \
                    (rect[2] < avg_width)) and \
                    abs(avg_height - rect[3])/ avg_height < similarity and \
                    area > 0.5*(avg_width*avg_height) and \
                    abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity:
                        for r in merge_rects:
                            if r in group:
                                group.remove(r)
                        # merge into group
                        new_group = []
                        merged = False
                        for gr in group:
                            area2 = max(gr[2] * gr[3], rect[2] * rect[3])
                            isect = intersect(gr, rect)
                            if isect[2] * isect[3] > 0.4 * area2:
                                x = min(gr[0], rect[0])
                                y = min(gr[1], rect[1])
                                x2 = max(gr[0] + gr[2], rect[0] + rect[2])
                                y2 = max(gr[1] + gr[3], rect[1] + rect[3])
                                new_rect = (x, y, x2 - x, y2 - y)
                                new_group.append(new_rect)
                                merged = True
                            else:
                                new_group.append(gr)
                        if not merged:
                            new_group.append(rect)
                        group = new_group
                        cv.Rectangle(display, (rect[0], rect[1]),
                                     (rect[0] + rect[2], rect[1] + rect[3]),
                                     (255, 0, 255), 2)
            # avoid splitting
            split = False
            # select higher threshold if innovates significantly
            best_width = 0.0
            if best_chars:
                best_area = 0.0
                for rect in best_chars:
                    best_area += rect[2] * rect[3]
                    best_width += rect[2]
                best_width /= len(best_chars)
                area = 0.0
                overlapped = 0.0
                avg_width = 0.0
                avg_height = 0.0
                for rect in group:
                    area += rect[2] * rect[3]
                    avg_width += rect[2]
                    avg_height += rect[3]
                    for char in best_chars:
                        section = intersect(rect, char)
                        if section[2] * section[3] > 0:
                            overlapped += section[2] * section[3]
                avg_width /= len(group)
                avg_height /= len(group)
                quotient = overlapped / area
                quotient2 = (area - overlapped) / best_area
                if debug:
                    print area, overlapped, best_area
                    print group
                    print "QUO", quotient
                    print "QUO2", quotient2
            else:
                quotient = 0
                quotient2 = 1
                best_area = 0

            group.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2))
            best_chars.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2))
            if group_range[0] <= len(group) <= group_range[1] and avg_width > 5 and avg_height > 10 and \
            ((quotient2 > 0.05 and (best_area == 0 or abs(area - best_area)/best_area < 0.4))
            or (quotient2 > 0.3 and area > best_area)):
                if debug:
                    print "ASSIGNED", group
                best_chars = group
                best_threshold = threshold  #get_patch(thresholded, original_rect)
            else:
                if debug:
                    print "not", quotient2, len(
                        group), avg_width, avg_height, area, best_area

        # best_chars = groups
        if debug:
            for rect in best_chars:
                cv.Rectangle(display, (rect[0], rect[1]),
                             (rect[0] + rect[2], rect[1] + rect[3]),
                             (0, 255, 0), 1)
            cv.ShowImage("main", display)
            if not skip and not next:
                c = cv.WaitKey(0)
                if c == ord("a"):
                    skip = True
                if c == ord("z"):
                    next = True
    best_chars.sort(lambda x, y: cmp(x[0], y[0]))
    cv.ResetImageROI(thresholded)
    cv.ResetImageROI(contour_image)
    cv.ResetImageROI(image)
    cv.ResetImageROI(edges)
    if display:
        cv.ResetImageROI(display)
    return best_chars, best_threshold
Example #9
0
        cv.Zero(tmp)

    # no need to pad bottom part of dft_A with zeros because of
    # 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)
Example #10
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)
Example #11
0
 def normalize_gamma(cls, img, gamma):
     out = cv.CreateMat(img.rows, img.cols, img.type)
     cv.Pow(img, out, gamma)
     cv.ConvertScaleAbs(out, out)
     return out
Example #12
0
def sum_squared(img1, img2):
    tmp = cv.CreateImage(cv.GetSize(img1), 8, 1)
    cv.Sub(img1, img2, tmp)
    cv.Pow(tmp, tmp, 2.0)
    return cv.Sum(tmp)[0]
Example #13
0
def doSSIM(frame1, frame2):
    '''
    The equivalent of Zhou Wang's SSIM matlab code using OpenCV.
    from http://www.cns.nyu.edu/~zwang/files/research/ssim/index.html
    The measure is described in :
    "Image quality assessment: From error measurement to structural similarity"
    C++ code by Rabah Mehdi. http://mehdi.rabah.free.fr/SSIM

    C++ to Python translation and adaptation by Iñaki Úcar
    '''
    def array2cv(a):
        dtype2depth = {
            'uint8': cv.IPL_DEPTH_8U,
            'int8': cv.IPL_DEPTH_8S,
            'uint16': cv.IPL_DEPTH_16U,
            'int16': cv.IPL_DEPTH_16S,
            'int32': cv.IPL_DEPTH_32S,
            'float32': cv.IPL_DEPTH_32F,
            'float64': cv.IPL_DEPTH_64F,
        }
        try:
            nChannels = a.shape[2]
        except:
            nChannels = 1
        cv_im = cv.CreateImageHeader((a.shape[1], a.shape[0]),
                                     dtype2depth[str(a.dtype)], nChannels)
        cv.SetData(cv_im, a.tostring(),
                   a.dtype.itemsize * nChannels * a.shape[1])
        return cv_im

    C1 = 6.5025
    C2 = 58.5225
    img1_temp = array2cv(frame1)
    img2_temp = array2cv(frame2)
    nChan = img1_temp.nChannels
    d = cv.IPL_DEPTH_32F
    size = img1_temp.width, img1_temp.height
    img1 = cv.CreateImage(size, d, nChan)
    img2 = cv.CreateImage(size, d, nChan)
    cv.Convert(img1_temp, img1)
    cv.Convert(img2_temp, img2)
    img1_sq = cv.CreateImage(size, d, nChan)
    img2_sq = cv.CreateImage(size, d, nChan)
    img1_img2 = cv.CreateImage(size, d, nChan)
    cv.Pow(img1, img1_sq, 2)
    cv.Pow(img2, img2_sq, 2)
    cv.Mul(img1, img2, img1_img2, 1)
    mu1 = cv.CreateImage(size, d, nChan)
    mu2 = cv.CreateImage(size, d, nChan)
    mu1_sq = cv.CreateImage(size, d, nChan)
    mu2_sq = cv.CreateImage(size, d, nChan)
    mu1_mu2 = cv.CreateImage(size, d, nChan)
    sigma1_sq = cv.CreateImage(size, d, nChan)
    sigma2_sq = cv.CreateImage(size, d, nChan)
    sigma12 = cv.CreateImage(size, d, nChan)
    temp1 = cv.CreateImage(size, d, nChan)
    temp2 = cv.CreateImage(size, d, nChan)
    temp3 = cv.CreateImage(size, d, nChan)
    ssim_map = cv.CreateImage(size, d, nChan)
    #/*************************** END INITS **********************************/
    #// PRELIMINARY COMPUTING
    cv.Smooth(img1, mu1, cv.CV_GAUSSIAN, 11, 11, 1.5)
    cv.Smooth(img2, mu2, cv.CV_GAUSSIAN, 11, 11, 1.5)
    cv.Pow(mu1, mu1_sq, 2)
    cv.Pow(mu2, mu2_sq, 2)
    cv.Mul(mu1, mu2, mu1_mu2, 1)
    cv.Smooth(img1_sq, sigma1_sq, cv.CV_GAUSSIAN, 11, 11, 1.5)
    cv.AddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq)
    cv.Smooth(img2_sq, sigma2_sq, cv.CV_GAUSSIAN, 11, 11, 1.5)
    cv.AddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq)
    cv.Smooth(img1_img2, sigma12, cv.CV_GAUSSIAN, 11, 11, 1.5)
    cv.AddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12)
    #//////////////////////////////////////////////////////////////////////////
    #// FORMULA
    #// (2*mu1_mu2 + C1)
    cv.Scale(mu1_mu2, temp1, 2)
    cv.AddS(temp1, C1, temp1)
    #// (2*sigma12 + C2)
    cv.Scale(sigma12, temp2, 2)
    cv.AddS(temp2, C2, temp2)
    #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
    cv.Mul(temp1, temp2, temp3, 1)
    #// (mu1_sq + mu2_sq + C1)
    cv.Add(mu1_sq, mu2_sq, temp1)
    cv.AddS(temp1, C1, temp1)
    #// (sigma1_sq + sigma2_sq + C2)
    cv.Add(sigma1_sq, sigma2_sq, temp2)
    cv.AddS(temp2, C2, temp2)
    #// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
    cv.Mul(temp1, temp2, temp1, 1)
    #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
    cv.Div(temp3, temp1, ssim_map, 1)
    index_scalar = cv.Avg(ssim_map)
    #// through observation, there is approximately
    #// 1% error max with the original matlab program
    return index_scalar[0]
Example #14
0
def line_line_intersections(P_0, u, Q_0, v):
    rows = P_0.height
    cols = P_0.width
    w_0 = cv.CreateMat(rows, cols, cv.CV_32FC3)

    cv.Sub(P_0, Q_0, w_0)

    a = element_wise_dot_product(u, u)
    b = element_wise_dot_product(u, v)
    c = element_wise_dot_product(v, v)
    d = element_wise_dot_product(u, w_0)
    e = element_wise_dot_product(v, w_0)

    a_mul_c = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Mul(a, c, a_mul_c)

    b_squared = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Pow(b, b_squared, 2)

    denominator = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Sub(a_mul_c, b_squared, denominator)

    b_mul_e = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Mul(b, e, b_mul_e)

    c_mul_d = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Mul(c, d, c_mul_d)

    b_mul_d = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Mul(b, d, b_mul_d)

    a_mul_e = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Mul(a, e, a_mul_e)

    s_c = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Sub(b_mul_e, c_mul_d, s_c)
    cv.Div(s_c, denominator, s_c)

    t_c = cv.CreateMat(rows, cols, cv.CV_32FC1)
    cv.Sub(a_mul_e, b_mul_d, t_c)
    cv.Div(t_c, denominator, t_c)

    u_x = cv.CreateMat(rows, cols, cv.CV_32FC1)
    u_y = cv.CreateMat(rows, cols, cv.CV_32FC1)
    u_z = cv.CreateMat(rows, cols, cv.CV_32FC1)

    cv.Split(u, u_x, u_y, u_z, None)

    su_x = cv.CreateMat(rows, cols, cv.CV_32FC1)
    su_y = cv.CreateMat(rows, cols, cv.CV_32FC1)
    su_z = cv.CreateMat(rows, cols, cv.CV_32FC1)

    cv.Mul(s_c, u_x, su_x)
    cv.Mul(s_c, u_y, su_y)
    cv.Mul(s_c, u_z, su_z)

    su = cv.CreateMat(rows, cols, cv.CV_32FC3)
    cv.Merge(su_x, su_y, su_z, None, su)

    tu_x = cv.CreateMat(rows, cols, cv.CV_32FC1)
    tu_y = cv.CreateMat(rows, cols, cv.CV_32FC1)
    tu_z = cv.CreateMat(rows, cols, cv.CV_32FC1)

    cv.Mul(t_c, u_x, tu_x)
    cv.Mul(t_c, u_y, tu_y)
    cv.Mul(t_c, u_z, tu_z)

    tu = cv.CreateMat(rows, cols, cv.CV_32FC3)
    cv.Merge(tu_x, tu_y, tu_z, None, tu)

    closest_point = cv.CreateMat(rows, cols, cv.CV_32FC3)
    cv.Add(P_0, su, closest_point)
    return closest_point
Example #15
0
 def __SSIM(self, frame1, frame2):
     """
         The equivalent of Zhou Wang's SSIM matlab code using OpenCV.
         from http://www.cns.nyu.edu/~zwang/files/research/ssim/index.html
         The measure is described in :
         "Image quality assessment: From error measurement to structural similarity"
         C++ code by Rabah Mehdi. http://mehdi.rabah.free.fr/SSIM
         
         C++ to Python translation and adaptation by Iñaki Úcar
     """
     C1 = 6.5025
     C2 = 58.5225
     img1_temp = self.__array2cv(frame1)
     img2_temp = self.__array2cv(frame2)
     nChan = img1_temp.nChannels
     d = cv.IPL_DEPTH_32F
     size = img1_temp.width, img1_temp.height
     img1 = cv.CreateImage(size, d, nChan)
     img2 = cv.CreateImage(size, d, nChan)
     cv.Convert(img1_temp, img1)
     cv.Convert(img2_temp, img2)
     img1_sq = cv.CreateImage(size, d, nChan)
     img2_sq = cv.CreateImage(size, d, nChan)
     img1_img2 = cv.CreateImage(size, d, nChan)
     cv.Pow(img1, img1_sq, 2)
     cv.Pow(img2, img2_sq, 2)
     cv.Mul(img1, img2, img1_img2, 1)
     mu1 = cv.CreateImage(size, d, nChan)
     mu2 = cv.CreateImage(size, d, nChan)
     mu1_sq = cv.CreateImage(size, d, nChan)
     mu2_sq = cv.CreateImage(size, d, nChan)
     mu1_mu2 = cv.CreateImage(size, d, nChan)
     sigma1_sq = cv.CreateImage(size, d, nChan)
     sigma2_sq = cv.CreateImage(size, d, nChan)
     sigma12 = cv.CreateImage(size, d, nChan)
     temp1 = cv.CreateImage(size, d, nChan)
     temp2 = cv.CreateImage(size, d, nChan)
     temp3 = cv.CreateImage(size, d, nChan)
     ssim_map = cv.CreateImage(size, d, nChan)
     #/*************************** END INITS **********************************/
     #// PRELIMINARY COMPUTING
     cv.Smooth(img1, mu1, cv.CV_GAUSSIAN, 11, 11, 1.5)
     cv.Smooth(img2, mu2, cv.CV_GAUSSIAN, 11, 11, 1.5)
     cv.Pow(mu1, mu1_sq, 2)
     cv.Pow(mu2, mu2_sq, 2)
     cv.Mul(mu1, mu2, mu1_mu2, 1)
     cv.Smooth(img1_sq, sigma1_sq, cv.CV_GAUSSIAN, 11, 11, 1.5)
     cv.AddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq)
     cv.Smooth(img2_sq, sigma2_sq, cv.CV_GAUSSIAN, 11, 11, 1.5)
     cv.AddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq)
     cv.Smooth(img1_img2, sigma12, cv.CV_GAUSSIAN, 11, 11, 1.5)
     cv.AddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12)
     #//////////////////////////////////////////////////////////////////////////
     #// FORMULA
     #// (2*mu1_mu2 + C1)
     cv.Scale(mu1_mu2, temp1, 2)
     cv.AddS(temp1, C1, temp1)
     #// (2*sigma12 + C2)
     cv.Scale(sigma12, temp2, 2)
     cv.AddS(temp2, C2, temp2)
     #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
     cv.Mul(temp1, temp2, temp3, 1)
     #// (mu1_sq + mu2_sq + C1)
     cv.Add(mu1_sq, mu2_sq, temp1)
     cv.AddS(temp1, C1, temp1)
     #// (sigma1_sq + sigma2_sq + C2)
     cv.Add(sigma1_sq, sigma2_sq, temp2)
     cv.AddS(temp2, C2, temp2)
     #// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
     cv.Mul(temp1, temp2, temp1, 1)
     #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
     cv.Div(temp3, temp1, ssim_map, 1)
     index_scalar = cv.Avg(ssim_map)
     #// through observation, there is approximately
     #// 1% error max with the original matlab program
     return index_scalar[0]
    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