Exemple #1
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    desc = """%prog is a tool performs mean shift clustering and a phase symmetry transfromation\n
     on a given input image, in that order."""

    parser = optparse.OptionParser(description=desc,
                                   usage='Usage: ex) %prog imageFile.png')

    (opts, args) = parser.parse_args(argv)
    args = args[1:]

    #check to see if the user provided an output directory
    if len(args) == 0:
        print "\nNo input file provided"
        parser.print_help()
        sys.exit(-1)

    #check to see if the file system image is provided
    if len(args) > 1:
        print "\n invalid argument(s) %s provided" % (str(args))
        parser.print_help()
        sys.exit(-1)

    #begin transform
    filename = args[0]
    img = meanShift(filename)
    cv2.imwrite("MS_" + filename, img)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    pha, ori, tot, T = phasesym(img)
    pha *= 255 / (np.max(np.max(pha)))
    cv2.imwrite("MSPS_Serial_" + filename, pha)
    pha = np.uint8(pha)
    blobDetect(pha)
Exemple #2
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    desc = """%prog is a tool performs mean shift clustering and a phase symmetry transfromation\n
     on a given input image, in that order."""

    parser = optparse.OptionParser(description=desc, usage='Usage: ex) %prog imageFile.png')

    (opts, args) = parser.parse_args(argv)
    args = args[1:]

    #check to see if the user provided an output directory
    if len(args) == 0:
        print "\nNo input file provided"
        parser.print_help()
        sys.exit(-1)

    #check to see if the file system image is provided
    if len(args) > 1:
        print "\n invalid argument(s) %s provided" % (str(args))
        parser.print_help()
        sys.exit(-1)

    #begin transform
    filename = args[0]
    img = meanShift(filename)
    cv2.imwrite("MS_" + filename, img)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    pha, ori, tot, T = phasesym(img)
    pha *= 255/(np.max(np.max(pha)))
    cv2.imwrite("MSPS_Serial_" + filename, pha)
    pha = np.uint8(pha)
    blobDetect(pha)
Exemple #3
0
def phaseTuning(filename,
                scaleMin=5,
                scaleMax=10,
                orientationMin=6,
                orientationMax=16,
                kMax=30):

    #Performs phase symmetry transformation on a give for a given range across all parameters
    img = cv2.imread(filename)
    #img = meanShift(img)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    sigmaMult = [(0.85, 1.3), (0.75, 1.6), (0.65, 2.1), (0.55, 3), (0.45, 3.4)]

    #so ugly
    startTime = time.time()
    for scale in range(scaleMin, scaleMax):
        #scales loop
        for orient in range(orientationMin, orientationMax):
            #orientations loop
            for thisTuple in sigmaMult:
                sigOnF = thisTuple[0]
                mul = thisTuple[1]
                for kVal in range(1, kMax):
                    #k loop
                    itStartTime = time.time()
                    print "scale:" + str(scale) + " orient:" + str(orient) + " mult:" + str(mul) + \
                          " sigmaOnF:" + str(sigOnF) + " k:" + str(kVal)

                    pha, ori, tot, T = phasesym(img,
                                                nscale=scale,
                                                norient=orient,
                                                mult=mul,
                                                sigmaOnf=sigOnF,
                                                k=kVal)

                    cv2.normalize(pha, pha, 0, 255, cv2.NORM_MINMAX)
                    pha = np.uint8(pha)
                    cv2.imwrite(
                        filename + "_phase_symmetry_nscale:" + str(scale) +
                        "_norient:" + str(orient) + "_mult:" + str(mul) +
                        "_sigmaOnF:" + str(sigOnF) + "_k:" + str(kVal) +
                        "_.png", pha)
                    itEndTime = time.time()
                    print "Loop Time: " + str(itEndTime - itStartTime)

    endTime = time.time()
    print "Finished. \nTotal runtime: " + str(endTime - startTime)
Exemple #4
0
def phaseTuning(filename, scaleMin=5, scaleMax=10, orientationMin=6,orientationMax=16, kMax=30):

    #Performs phase symmetry transformation on a give for a given range across all parameters
    img = cv2.imread(filename)
    #img = meanShift(img)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    sigmaMult = [(0.85, 1.3),
                (0.75, 1.6),
                (0.65, 2.1),
                (0.55, 3),
                (0.45, 3.4)]

    #so ugly
    startTime = time.time()
    for scale in range(scaleMin, scaleMax):
        #scales loop
        for orient in range(orientationMin, orientationMax):
            #orientations loop
            for thisTuple in sigmaMult:
                sigOnF = thisTuple[0]
                mul = thisTuple[1]
                for kVal in range(1, kMax):
                    #k loop
                    itStartTime = time.time()
                    print "scale:" + str(scale) + " orient:" + str(orient) + " mult:" + str(mul) + \
                          " sigmaOnF:" + str(sigOnF) + " k:" + str(kVal)
                    
                    pha, ori, tot, T = phasesym(img, nscale=scale, norient=orient, mult=mul, sigmaOnf=sigOnF, k=kVal)

                    cv2.normalize(pha, pha, 0, 255, cv2.NORM_MINMAX)
                    pha = np.uint8(pha)
                    cv2.imwrite(filename + "_phase_symmetry_nscale:" + str(scale) + "_norient:" + str(orient)
                                + "_mult:" + str(mul) +"_sigmaOnF:" + str(sigOnF) + "_k:" + str(kVal) + "_.png", pha)
                    itEndTime = time.time()
                    print "Loop Time: " + str(itEndTime - itStartTime)

    endTime = time.time()
    print "Finished. \nTotal runtime: " + str(endTime - startTime)
Exemple #5
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    desc = """%prog is a tool performs mean shift clustering and a phase symmetry transfromation\n
     on a given input image, in that order."""

    parser = optparse.OptionParser(description=desc, usage='Usage: ex) %prog imageFile.png')
    parser.add_option('--scale', help='specify number of phasesym scales', dest='NSCALE', default=4, type="int")
    parser.add_option('--ori', help='specify number of phasesym orientations', dest='NORIENT', default=6, type="int")
    parser.add_option('--mult', help='specify multiplier for phasesym', dest='MULT', default=3.0, type="float")
    parser.add_option('--sig', help='specify sigma on frequncy for phasesym', dest='SIGMAONF', default=0.55, type="float")
    parser.add_option('--k', help='specify k value for phasesym', dest='K', default=1, type="int")
    parser.add_option('--blur', help='specify blur width value N for NxN blur operation', dest='BLUR', default=3, type="int")
    parser.add_option('--srad', help='specify spatial radius for mean shift', dest='SRAD', default=5, type="int")
    parser.add_option('--rrad', help='specify radiometric radius for mean shift', dest='RRAD', default=6, type="int")
    parser.add_option('--den', help='specify pixel density value for mean shift', dest='DEN', default=10, type="int")
    parser.add_option('--amin', help='specify blob minimum area for boxing', dest='AMIN', default=5, type="int")
    parser.add_option('--amax', help='specify blob maximum area for boxing', dest='AMAX', default=400, type="int")
    parser.add_option('--wmin', help='specify box minimum width acceptance', dest='WMIN', default=3, type="int")
    parser.add_option('--wmax', help='specify box maximum width acceptance', dest='WMAX', default=35, type="int")
    parser.add_option('--hmin', help='specify box minimum height acceptance', dest='HMIN', default=2, type="int")
    parser.add_option('--hmax', help='specify box maximum height acceptance', dest='HMAX', default=55, type="int")
    parser.add_option('--arat', help='specify minimum box aspect ratio for acceptance', dest='ARATIO', default=0.25, type="float")
    parser.add_option('--edgeMin', help='specify minimum hysteresis value for edge detection', dest='EDGEMIN', default=100, type="int")
    parser.add_option('--edgeMax', help='specify maximum hysteresis value for edge detection', dest='EDGEMAX', default=200, type="int")
    parser.add_option('--ref', help='specify reference car image', dest='MASTERIMG', default="")
    parser.add_option('--win', help='specify search window size', dest='WINSZ', default=55, type="int")
    parser.add_option('--tol', help='specify shape description tolerance', dest='TOL', default=0.07, type="float")
    parser.add_option('--eps', help='specify maximum epsilon value for DBSCAN clustering algorithm', dest='EPS', default=15, type="int")
    parser.add_option('--min_samples', help='specify the numer fo minimum samples that constitute a cluster during DBSCAN',
                      dest='MINSAMPLES', default=1, type="int")


    (opts, args) = parser.parse_args(argv)
    args = args[1:]

    #check to see if the user provided an output directory
    if len(args) == 0:
        print "\nNo input file provided"
        parser.print_help()
        sys.exit(-1)

    #check to see if the file system image is provided
    if len(args) > 1:
        print "\n invalid argument(s) %s provided" % (str(args))
        parser.print_help()
        sys.exit(-1)


    NSCALE = opts.NSCALE
    NORIENT = opts.NORIENT
    MULT = opts.MULT
    SIGMAONF = opts.SIGMAONF
    K = opts.K
    BLUR = (opts.BLUR, opts.BLUR)
    SRAD = opts.SRAD
    RRAD = opts.RRAD
    DEN = opts.DEN
    AMIN = opts.AMIN
    AMAX = opts.AMAX
    WMAX = opts.WMAX
    WMIN = opts.WMIN
    HMAX = opts.HMAX
    HMIN = opts.HMIN
    ARATIO = opts.ARATIO
    EDGEMIN = opts.EDGEMIN
    EDGEMAX = opts.EDGEMAX
    WINSZ = opts.WINSZ 
    TOL = opts.TOL
    EPS = opts.EPS
    MINSAMPLES = opts.MINSAMPLES
    masterImg = ""
    masterHist = None


    if len(opts.MASTERIMG) > 0:
        masterImg = cv2.imread(opts.MASTERIMG)

        h, w, z = masterImg.shape
        if w%2:
            x = int(np.ceil(w/float(2)))
        else:
            x = w/2
        if h%2:
            y = int(np.ceil(h/float(2)))
        else:
            y = h/2
        print y, x
        masterHist = RadAngleHist(masterImg, 0, y, x,BLUR)
    else:
        #masterImg = cv2.imread('singleCar.png')
        #masterHist = RadAngleHist(masterImg, 0, 27, 29)
        masterImg, cen = genReferenceCar(WINSZ)
        print cen
        print masterImg
        masterHist = RadAngleHist(masterImg, 0, cen[0], cen[1],BLUR)
        print str(masterHist)

    #begin transform
    filename = args[0]
    basename = os.path.splitext(filename)[0]
    img = cv2.imread(filename)

    grayImg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(basename+"_gray.png", grayImg)
    pha, ori, tot, T = phasesym(grayImg, nscale=NSCALE, norient=NORIENT, minWaveLength=3, mult=MULT, sigmaOnf=SIGMAONF, k=K, polarity=0)
    pha = cv2.normalize(pha, pha, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    pha = np.uint8(pha)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K), pha)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_ori.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K), ori)
    np.savetxt('python_ori.txt',ori)

    pha = cv2.blur(pha, BLUR)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1]), pha)
    pha = cv2.cvtColor(pha, cv2.COLOR_GRAY2RGB)
    pha = cv2.cvtColor(pha, cv2.COLOR_RGB2LUV)


    (segmented_image, labels_image, number_regions) = pms.segment(pha, spatial_radius=SRAD, range_radius=RRAD, min_density=DEN)
    segmented_image=cv2.normalize(segmented_image, segmented_image, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    segmented_image = np.uint8(segmented_image)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_LUV2RGB)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN), segmented_image)


    #ori = np.uint8(ori)
    #(ori, labels_image, number_regions) = pms.segment(ori, spatial_radius=SRAD, range_radius=RRAD, min_density=DEN)
    #ori=cv2.normalize(ori, ori, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    #ori = np.uint8(ori)
    #cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_ori.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN), ori)


    thresh_img = cv2.adaptiveThreshold(segmented_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, 0)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_T.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN), thresh_img)


    #get centroids
    contours = getContours(thresh_img, AMIN, AMAX, WMIN, WMAX, HMIN, HMAX, ARATIO)
    centroids = getCentroids(contours)
    
    #cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_A_%d_%d_W_%d_%d_H_%d_%d_R_%.2f.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN, AMIN, AMAX, WMIN, WMAX, HMIN, HMAX, ARATIO), img)

    histograms = []


    dirName = 'windowTiles'
    if not (os.path.isdir(dirName)):
        #create subdirectory if it does not already exist
        os.mkdir(dirName)

    outputImg = img.copy()
    centroidsImg = img.copy()
    drawCentroids(centroidsImg,centroids)
    #drawCentroids(img,centroids)

    np.set_printoptions(formatter={'float': '{: 0.2f}'.format})
    print str(masterHist) + "\n"
    shapedCentroids = []
    for cen,cnt in zip(centroids,contours):
        print cen
        #win = getImageWindow(img, cen[0],cen[1],26,52)

        #if cen[0]==100 and cen[1] == 310:
        #    import pdb; pdb.set_trace()
        win = getImageWindow(img, cen[0],cen[1],WINSZ,WINSZ)   #correct
        filename = "win_%d_%d.jpg" % (cen[0], cen[1])
        cv2.imwrite(os.path.join(dirName, filename), win)
        #histogram = RadAngleHist(win, 90+ori[cen[0], cen[1]],cen[0],cen[1])
        histogram = RadAngleHist(win, 90+ori[cen[0], cen[1]],cen[0],cen[1],BLUR)

        #print ori[cen[0], cen[1]]
        print str(histogram) + "\n"

        if histogram.getHistSum() < 0.1:
            continue

        #if cen == (11,144):
        #    import pdb;pdb.set_trace()

        #print masterHist.compare(histogram)
        if masterHist.compare(histogram) < TOL:
            drawBox(outputImg, cnt)
            shapedCentroids.append(cen)

    dirName = ""
    cv2.imwrite(os.path.join(dirName, "Boxes_Centroids.jpg"), outputImg)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_A_%d_%d_W_%d_%d_H_%d_%d_R_%.2f_E_%d_%d_W_%d_T_%.2f.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN, AMIN, AMAX, WMIN, WMAX, HMIN, HMAX, ARATIO,EDGEMIN,EDGEMAX,WINSZ,TOL), outputImg) 
    cv2.imwrite(os.path.join(dirName, "Centroids.jpg"), centroidsImg) 
    
    dbResult = scanBlobs(shapedCentroids, img.copy(), EPS, MINSAMPLES)
    drawClusterColors(dbResult, img.copy(), shapedCentroids)
Exemple #6
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    desc = """%prog is a tool performs mean shift clustering and a phase symmetry transfromation\n
     on a given input image, in that order."""

    parser = optparse.OptionParser(description=desc, usage='Usage: ex) %prog imageFile.png')
    parser.add_option('--scale', help='specify number of phasesym scales', dest='NSCALE', default=4, type="int")
    parser.add_option('--ori', help='specify number of phasesym orientations', dest='NORIENT', default=6, type="int")
    parser.add_option('--mult', help='specify multiplier for phasesym', dest='MULT', default=3.0, type="float")
    parser.add_option('--sig', help='specify sigma on frequncy for phasesym', dest='SIGMAONF', default=0.55, type="float")
    parser.add_option('--k', help='specify k value for phasesym', dest='K', default=1, type="int")
    parser.add_option('--blur', help='specify blur width value N for NxN blur operation', dest='BLUR', default=3, type="int")
    parser.add_option('--srad', help='specify spatial radius for mean shift', dest='SRAD', default=5, type="int")
    parser.add_option('--rrad', help='specify radiometric radius for mean shift', dest='RRAD', default=6, type="int")
    parser.add_option('--den', help='specify pixel density value for mean shift', dest='DEN', default=10, type="int")
    parser.add_option('--amin', help='specify blob minimum area for boxing', dest='AMIN', default=5, type="int")
    parser.add_option('--amax', help='specify blob maximum area for boxing', dest='AMAX', default=400, type="int")
    parser.add_option('--wmin', help='specify box minimum width acceptance', dest='WMIN', default=3, type="int")
    parser.add_option('--wmax', help='specify box maximum width acceptance', dest='WMAX', default=35, type="int")
    parser.add_option('--hmin', help='specify box minimum height acceptance', dest='HMIN', default=2, type="int")
    parser.add_option('--hmax', help='specify box maximum height acceptance', dest='HMAX', default=55, type="int")
    parser.add_option('--arat', help='specify minimum box aspect ratio for acceptance', dest='ARATIO', default=0.25, type="float")
    parser.add_option('--edgeMin', help='specify minimum hysteresis value for edge detection', dest='EDGEMIN', default=100, type="int")
    parser.add_option('--edgeMax', help='specify maximum hysteresis value for edge detection', dest='EDGEMAX', default=200, type="int")
    parser.add_option('--eps', help='specify maximum epsilon value for DBSCAN clustering algorithm', dest='EPS', default=15, type="int")
    parser.add_option('--min_samples', help='specify the numer fo minimum samples that constitute a cluster during DBSCAN',
                      dest='MINSAMPLES', default=1, type="int")

    (opts, args) = parser.parse_args(argv)
    args = args[1:]

    #check to see if the user provided an output directory
    if len(args) == 0:
        print "\nNo input file provided"
        parser.print_help()
        sys.exit(-1)

    #check to see if the file system image is provided
    if len(args) > 1:
        print "\n invalid argument(s) %s provided" % (str(args))
        parser.print_help()
        sys.exit(-1)


    NSCALE = opts.NSCALE
    NORIENT = opts.NORIENT
    MULT = opts.MULT
    SIGMAONF = opts.SIGMAONF
    K = opts.K
    BLUR = (opts.BLUR, opts.BLUR)
    SRAD = opts.SRAD
    RRAD = opts.RRAD
    DEN = opts.DEN
    AMIN = opts.AMIN
    AMAX = opts.AMAX
    WMAX = opts.WMAX
    WMIN = opts.WMIN
    HMAX = opts.HMAX
    HMIN = opts.HMIN
    ARATIO = opts.ARATIO
    EDGEMIN = opts.EDGEMIN
    EDGEMAX = opts.EDGEMAX
    EPS = opts.EPS
    MINSAMPLES = opts.MINSAMPLES


    #begin transform
    filename = args[0]
    basename = os.path.splitext(filename)[0]
    img = cv2.imread(filename)

    grayImg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(basename+"_gray.png", grayImg)
    pha, ori, tot, T = phasesym(grayImg, nscale=NSCALE, norient=NORIENT, minWaveLength=3, mult=MULT, sigmaOnf=SIGMAONF, k=K, polarity=0)
    pha = cv2.normalize(pha, pha, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    pha = np.uint8(pha)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K), pha)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_ori.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K), ori)
    np.savetxt('python_ori.txt',ori)

    pha = cv2.blur(pha, BLUR)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1]), pha)
    pha = cv2.cvtColor(pha, cv2.COLOR_GRAY2RGB)
    pha = cv2.cvtColor(pha, cv2.COLOR_RGB2LUV)


    (segmented_image, labels_image, number_regions) = pms.segment(pha, spatial_radius=SRAD, range_radius=RRAD, min_density=DEN)
    segmented_image=cv2.normalize(segmented_image, segmented_image, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    segmented_image = np.uint8(segmented_image)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_LUV2RGB)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN), segmented_image)


    #ori = np.uint8(ori)
    #(ori, labels_image, number_regions) = pms.segment(ori, spatial_radius=SRAD, range_radius=RRAD, min_density=DEN)
    #ori=cv2.normalize(ori, ori, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    #ori = np.uint8(ori)
    #cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_ori.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN), ori)


    thresh_img = cv2.adaptiveThreshold(segmented_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, 0)
    cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_T.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN), thresh_img)


    #get centroids
    contours = getContours(thresh_img, AMIN, AMAX, WMIN, WMAX, HMIN, HMAX, ARATIO)
    centroids = getCentroids(contours)
    
    #cv2.imwrite(basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_A_%d_%d_W_%d_%d_H_%d_%d_R_%.2f.png" % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN, AMIN, AMAX, WMIN, WMAX, HMIN, HMAX, ARATIO), img)

    histograms = []

    f = open('masterHist.txt')
    h = np.loadtxt(f)
    masterImg = cv2.imread('singleCar.png')
    masterHist = RadAngleHist(masterImg, 180, 27, 29)

    dirName = 'windowTiles'
    if not (os.path.isdir(dirName)):
        #create subdirectory if it does not already exist
        os.mkdir(dirName)

    outputImg = img.copy()
    #ori = np.loadtxt('matlab_ori.txt',delimiter=',')

    shapedCentroids = []
    for cen,cnt in zip(centroids,contours):
        #print cen
        #win = getImageWindow(img, cen[0],cen[1],26,52)

        #import pdb; pdb.set_trace()
        win = getImageWindow(img, cen[0],cen[1],55,55)   #correct


        #win = getImageWindow(img, cen[0],cen[1],51,51)
        filename = "win_%d_%d.jpg" % (cen[0], cen[1])
        cv2.imwrite(os.path.join(dirName, filename), win)
        histogram = RadAngleHist(win, ori[cen[0], cen[1]],cen[0],cen[1])
        #print ori[cen[0], cen[1]]
        #print masterHist.compare(histogram)
#        if masterHist.compare(histogram, dist=60) == 0:
#            histograms.append(histogram)
        if masterHist.compare(histogram) < 0.07:
            drawBox(outputImg, cnt)
            shapedCentroids.append(cen)
            # boxedImg = img.copy()
            # drawBox(boxedImg,cnt)
            # filename = "win_box_%d_%d.jpg" % (cen[0], cen[1])
            # cv2.imwrite(os.path.join(dirName, filename), boxedImg)
    dirName = ""
    filename = "Boxes_+_Centroids.jpg"
    cv2.imwrite(os.path.join(dirName, filename), outputImg)

    dbResult = scanBlobs(shapedCentroids, img.copy(), EPS, MINSAMPLES)
    drawClusterColors(dbResult, img.copy(), shapedCentroids)
Exemple #7
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    desc = """%prog is a tool performs mean shift clustering and a phase symmetry transfromation\n
     on a given input image, in that order."""

    parser = optparse.OptionParser(description=desc,
                                   usage='Usage: ex) %prog imageFile.png')
    parser.add_option('--scale',
                      help='specify number of phasesym scales',
                      dest='NSCALE',
                      default=4,
                      type="int")
    parser.add_option('--ori',
                      help='specify number of phasesym orientations',
                      dest='NORIENT',
                      default=6,
                      type="int")
    parser.add_option('--mult',
                      help='specify multiplier for phasesym',
                      dest='MULT',
                      default=3.0,
                      type="float")
    parser.add_option('--sig',
                      help='specify sigma on frequncy for phasesym',
                      dest='SIGMAONF',
                      default=0.55,
                      type="float")
    parser.add_option('--k',
                      help='specify k value for phasesym',
                      dest='K',
                      default=1,
                      type="int")
    parser.add_option('--blur',
                      help='specify blur width value N for NxN blur operation',
                      dest='BLUR',
                      default=3,
                      type="int")
    parser.add_option('--srad',
                      help='specify spatial radius for mean shift',
                      dest='SRAD',
                      default=5,
                      type="int")
    parser.add_option('--rrad',
                      help='specify radiometric radius for mean shift',
                      dest='RRAD',
                      default=6,
                      type="int")
    parser.add_option('--den',
                      help='specify pixel density value for mean shift',
                      dest='DEN',
                      default=10,
                      type="int")
    parser.add_option('--amin',
                      help='specify blob minimum area for boxing',
                      dest='AMIN',
                      default=5,
                      type="int")
    parser.add_option('--amax',
                      help='specify blob maximum area for boxing',
                      dest='AMAX',
                      default=400,
                      type="int")
    parser.add_option('--wmin',
                      help='specify box minimum width acceptance',
                      dest='WMIN',
                      default=3,
                      type="int")
    parser.add_option('--wmax',
                      help='specify box maximum width acceptance',
                      dest='WMAX',
                      default=35,
                      type="int")
    parser.add_option('--hmin',
                      help='specify box minimum height acceptance',
                      dest='HMIN',
                      default=2,
                      type="int")
    parser.add_option('--hmax',
                      help='specify box maximum height acceptance',
                      dest='HMAX',
                      default=55,
                      type="int")
    parser.add_option('--arat',
                      help='specify minimum box aspect ratio for acceptance',
                      dest='ARATIO',
                      default=0.25,
                      type="float")

    (opts, args) = parser.parse_args(argv)
    args = args[1:]

    #check to see if the user provided an output directory
    if len(args) == 0:
        print "\nNo input file provided"
        parser.print_help()
        sys.exit(-1)

    #check to see if the file system image is provided
    if len(args) > 1:
        print "\n invalid argument(s) %s provided" % (str(args))
        parser.print_help()
        sys.exit(-1)

    NSCALE = opts.NSCALE
    NORIENT = opts.NORIENT
    MULT = opts.MULT
    SIGMAONF = opts.SIGMAONF
    K = opts.K
    BLUR = (opts.BLUR, opts.BLUR)
    SRAD = opts.SRAD
    RRAD = opts.RRAD
    DEN = opts.DEN
    AMIN = opts.AMIN
    AMAX = opts.AMAX
    WMAX = opts.WMAX
    WMIN = opts.WMIN
    HMAX = opts.HMAX
    HMIN = opts.HMIN
    ARATIO = opts.ARATIO

    THRESH = 0

    #begin transform
    filename = args[0]
    basename = os.path.splitext(filename)[0]
    img = cv2.imread(filename)
    #img = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)
    #(segmented_image, labels_image, number_regions) = pms.segment(img, spatial_radius=3, range_radius=11, min_density=10)
    #img = cv2.cvtColor(img, cv2.COLOR_LUV2RGB)
    #cv2.imwrite("MS_" + basename+".png", img)
    #img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    #pha, ori, tot, T = phasesym(img, nscale=5, norient=6, minWaveLength=3, mult=3.4, sigmaOnf = 0.45, k=29)

    #pha=cv2.normalize(pha,pha,0,255,cv2.NORM_MINMAX,cv2.CV_8UC1)
    #pha = np.uint8(pha)
    #cv2.imwrite("MSPS_Serial_" + basename + ".png", pha)
    #blobDetect(pha)

    #NSCALE = 4
    #NORIENT = 6
    #MULT = 3
    #SIGMAONF = 0.55
    #K = 1
    #BLUR = (3,3)
    #SRAD = 5
    #RRAD = 6
    #DEN = 10
    #THRESH = 127
    #AMIN = 5
    #AMAX = 400
    #WMAX = 35
    #WMIN = 3
    #HMAX = 55
    #HMIN = 2
    #ARATIO = 0.25

    grayImg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(basename + "_gray.png", grayImg)
    #pha, ori, tot, T = phasesym(grayImg, nscale=5, norient=6, minWaveLength=3, mult=3.4, sigmaOnf = 0.45, k=29)
    #pha, ori, tot, T = phasesym(grayImg, nscale=6, norient=6, minWaveLength=3, mult=3, sigmaOnf = 0.55, k=1, polarity=0)
    pha, ori, tot, T = phasesym(grayImg,
                                nscale=NSCALE,
                                norient=NORIENT,
                                minWaveLength=3,
                                mult=MULT,
                                sigmaOnf=SIGMAONF,
                                k=K,
                                polarity=0)
    pha = cv2.normalize(pha, pha, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
    pha = np.uint8(pha)
    cv2.imwrite(
        basename + "_PS" + "_%d_%d_%.2f_%.2f_%d.png" %
        (NSCALE, NORIENT, MULT, SIGMAONF, K), pha)

    pha = cv2.blur(pha, BLUR)
    cv2.imwrite(
        basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d.png" %
        (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1]), pha)
    pha = cv2.cvtColor(pha, cv2.COLOR_GRAY2RGB)
    pha = cv2.cvtColor(pha, cv2.COLOR_RGB2LUV)

    #(segmented_image, labels_image, number_regions) = pms.segment(pha, spatial_radius=3, range_radius=11, min_density=10)
    #(segmented_image, labels_image, number_regions) = pms.segment(pha, spatial_radius=15, range_radius=11, min_density=10)
    (segmented_image, labels_image,
     number_regions) = pms.segment(pha,
                                   spatial_radius=SRAD,
                                   range_radius=RRAD,
                                   min_density=DEN)
    segmented_image = cv2.normalize(segmented_image, segmented_image, 0, 255,
                                    cv2.NORM_MINMAX, cv2.CV_8UC1)
    segmented_image = np.uint8(segmented_image)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_LUV2RGB)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2GRAY)
    cv2.imwrite(
        basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d.png" %
        (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD,
         DEN), segmented_image)

    #ret,thresh_img = cv2.threshold(segmented_image,THRESH,255, cv2.THRESH_BINARY| cv2.THRESH_OTSU)
    thresh_img = cv2.adaptiveThreshold(segmented_image, 255,
                                       cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                       cv2.THRESH_BINARY, 7, 0)
    cv2.imwrite(
        basename + "_PS" + "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_T_%d.png" %
        (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD, DEN,
         THRESH), thresh_img)

    #find contours in black and white image
    contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_LIST,
                                           cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        area = cv2.contourArea(cnt)
        #if area >= 200 and area <= 2000:
        #if area >= 10 and area <= 1200:
        if area >= AMIN and area <= AMAX:
            x, y, w, h = cv2.boundingRect(cnt)
            #if w < WMAX and h < HMAX and w > 3 and h > 2:
            if w < WMAX and h < HMAX and w > WMIN and h > HMIN:
                aspectRatio = 0
                if w == min(w, h):
                    aspectRatio = float(w) / h
                else:
                    aspectRatio = float(h) / w

                if aspectRatio > ARATIO:
                    print "x=%d y=%d w=%d h=%d" % (x, y, w, h)
                    rect = cv2.minAreaRect(cnt)
                    box = cv2.cv.BoxPoints(rect)
                    box = np.int0(box)
                    cv2.drawContours(img, [box], 0, (255, 0, 0), 2)

    #cv2.imwrite('Boxes.jpg', img)
    cv2.imwrite(
        basename + "_PS" +
        "_%d_%d_%.2f_%.2f_%d_B_%d_%d_MS_%d_%d_%d_T_%d_A_%d_%d_W_%d_%d_H_%d_%d_R_%.2f.png"
        % (NSCALE, NORIENT, MULT, SIGMAONF, K, BLUR[0], BLUR[1], SRAD, RRAD,
           DEN, THRESH, AMIN, AMAX, WMIN, WMAX, HMIN, HMAX, ARATIO), img)