Exemplo n.º 1
0
def main():
    # 1. Parse Command Line arguments
    main_args = cer.parse_main()
    imagename = main_args.full_img_path
    svmfile = main_args.svm_file
    loglevel = main_args.log_level

    # 2. Init log file
    helps.init_log_file("cervical", "Cervical Imaging Classification",
                        loglevel)

    # 3. Read input image (in color)
    image = cer.read_image(imagename)
    if image is None:
        cer.error("Unable to read the provided input image: %r\n" % imagename)
        cer.info(fail)
        return

    # 4. Determine x-axis parameter (critical pixel density)
    criticalvalues = cer.parse_critical("abnormal.txt")
    if criticalvalues == {}:
        cer.info(fail)
        return

    x = cer.critical_pixel_density(image, criticalvalues)
    if x is None:
        cer.info(fail)
        return
    cer.info("Calculated critical pixel density: %.5f" % x)

    # 5. Determine y-axis parameter (blue mean)
    channels = cer.extract_RGB(image)
    if channels == {}:
        cer.info(fail)
        return
    bluestats = cer.channel_stats(channels["blue"])
    if bluestats == {}:
        cer.info(fail)
        return
    y = bluestats["mean"]
    cer.info("Calculated blue channel mean: %.2f" % y)

    # 6. Package x and y into one variable
    unknown = [x, y]

    # 7. Use both calculated parameters to classify image from svmfile data
    svmdata = jl.load(svmfile)
    classification = svmdata.predict(unknown)[0]

    # 8. Print to terminal & log file
    cer.print_diagnosis(imagename, classification, True)

    # DONE
    cer.info(success)
def main():
    # 1. Parse CLA for image name and init logfile
    main_args = cer.parse_critical_CLA()
    imgname = main_args.img_name

    helps.init_log_file(abnormal, "Abnormal cervix images", "INFO")
    info(imgname)

    # 2. Read in image
    rgbimage = cer.read_image(imgname)
    gsimage = cer.read_image(imgname, False)
    if rgbimage is None or gsimage is None:
        info("EXIT_FAILURE")
        return

    # 3. Blackout glare (i.e. white portions of image)
    rgbimage = cer.blackout_glare(rgbimage)

    # 4. Extract RGB + grayscale
    channels = cer.extract_RGB(rgbimage)
    channels["gray"] = cer.create_grayscale_channel(gsimage)

    # 5. Run stats on each color component
    redstats = cer.channel_stats(channels["red"])
    info("RED")
    helps.print_channel_stats(redstats, True)
    greenstats = cer.channel_stats(channels["green"])
    info("GREEN")
    helps.print_channel_stats(greenstats, True)
    bluestats = cer.channel_stats(channels["blue"])
    info("BLUE")
    helps.print_channel_stats(bluestats, True)
    graystats = cer.channel_stats(channels["gray"])
    info("GRAY")
    helps.print_channel_stats(graystats, True)

    # 6. Write the critical boundaries for RGB to output file
    with open(abnormal+".txt", 'w') as f:
        f.write("red ")
        f.write("%d %d\n" % (redstats["median"], cer.COLORMAX-1))
        f.write("green ")
        f.write("%d %d\n" % (greenstats["median"], cer.COLORMAX-1))
        f.write("blue ")
        f.write("%d %d\n" % (bluestats["firstQrt"], bluestats["median"]))
def svm_param1():

    # Parse CLA & init log file always comes first!
    main_args = cer.parse_SVM_CLA()
    dirname = main_args.directory
    critfile = main_args.crit_file

    helps.init_log_file(training, "Moss", "INFO")

    # 1. Initialize empty lists for both dysplasia and healthy images
    # These lists will contain counts for each image of how many critical
    # pixels each contains
    allDysplasia = []
    allHealthy = []

    # 2. Parse critical values file
    critVals = cer.parse_critical(critfile)

    # 3. Cylce through all dysplasia images in TrainingData directory
    dys_n = 0
    while True:

        imgname = helps.create_image_name(dysplasia, dys_n)
        filename = dirname + imgname

        # 3a. Read in image
        rgbimage = cer.read_image(filename)
        if rgbimage is None:
            break

        # 3b. Count how many pixels are in critical range
        density = cer.critical_pixel_density(rgbimage, critVals)

        # 3c. Log count for image and append list
        allDysplasia.append(density)
        info(filename)
        info("Critical p: %.5f\n" % density)

        # 3d. Extract RGB channels
        channels = cer.extract_RGB(rgbimage)

        # 3e. Calculate stats on each channel
        redstats = cer.channel_stats(channels["red"])
        greenstats = cer.channel_stats(channels["green"])
        bluestats = cer.channel_stats(channels["blue"])

        # 3f. Print stats
        helps.print_channel_stats(redstats, True)
        helps.print_channel_stats(greenstats, True)
        helps.print_channel_stats(bluestats, True)

        dys_n += 1

    # 4. Cycle through all healthy images in TrainingData and repeat 2a-g
    hea_n = 0
    while True:

        imgname = helps.create_image_name(healthy, hea_n)
        filename = dirname + imgname

        # 4a. Read in image
        rgbimage = cer.read_image(filename)
        if rgbimage is None:
            break

        # 4b. Count how many pixels are in critical range
        density = cer.critical_pixel_density(rgbimage, critVals)

        # 4c. Log count for image and append list
        allHealthy.append(density)
        info(filename)
        info("Critical p: %.5f\n" % density)

        # 4d. Extract RGB channels
        channels = cer.extract_RGB(rgbimage)

        # 4e. Calculate stats on each channel
        redstats = cer.channel_stats(channels["red"])
        greenstats = cer.channel_stats(channels["green"])
        bluestats = cer.channel_stats(channels["blue"])

        # 4f. Print stats
        helps.print_channel_stats(redstats, True)
        helps.print_channel_stats(greenstats, True)
        helps.print_channel_stats(bluestats, True)

        hea_n += 1

        # 5. Save parameter
        with open('svm_param1.txt', 'w') as f:
            json.dump({"dysplasia": allDysplasia, "healthy": allHealthy}, f)

    info("EXIT_SUCCESS")
Exemplo n.º 4
0
def main():
    # 1. Parse Command Line arguments
    main_args = us.parse_main()
    json_file = main_args.json_filename
    rf_file = main_args.rf_filename
    log_level = main_args.log_level
    display = main_args.display
    save = main_args.save

    # Start Logging
    helps.init_log_file("b_mode_us", "BME590 Assignment 05", log_level)

    # 2. Read JSON metadata
    params = us.read_jsonfile(json_file)

    c = params['c']
    Fs = params['fs']
    axial_samples = params['axial_samples']
    beam_spacing = params['beam_spacing']
    n_beams = params['num_beams']
    log.info(
        "C = %d m/s\nFs = %d Hz\nAxial samples=%d\nBeam spacing = %.8f m\
             \nNumber of beams = %d", c, Fs, axial_samples, beam_spacing,
        n_beams)

    # 3. Initialize 2-D Matrix
    image_matrix = us.init_matrix(axial_samples, n_beams)

    # 4. Read in a Single Beam of RF data while matrix not full
    byte_n = 0  # read-in byte counter
    for line, _ in enumerate(range(n_beams)):

        rf_beam, byte_n = us.read_rf(rf_file, axial_samples, byte_n)

        # Error handling
        if byte_n == -1:
            return

        # 5. Rectify Beam
        rf_beam = us.rectify(rf_beam)

        # 6. Create Envelope of Beam
        rf_beam = us.find_envelope(rf_beam)

        # 7. Logarithmic Compression
        rf_beam = us.log_comp(rf_beam)

        # 8. Place Beam in 2-D Matrix
        image_matrix[line] = array(rf_beam)

    # 9. Output
    # A. Reshape Matrix
    bmode_data = us.reshape_matrix(image_matrix)

    # B. Axes Calculations
    x_axis, x_len = us.calc_lat_position(beam_spacing, n_beams)
    log.info("X Length = %.5f m", x_len)
    y_axis, y_len = us.calc_axial_position(c, Fs, axial_samples)
    log.info("Y Length = %.5f m", y_len)

    # C. Plot/Save/Display Image
    fig = us.plot_bmode(x_axis, y_axis, bmode_data)
    us.save_bmode(fig, save)
    us.display_bmode(fig, display)

    log.info("EXIT SUCCESS\n")
def svm_param2():

    # parse CLA & init log file always comes first!
    main_args = cer.parse_SVM_CLA()
    dirname = main_args.directory

    helps.init_log_file(training, "Moss", "INFO")

    # 1. Initialize empty lists for both dysplasia and healthy images
    # These lists will have elements that are dictionaries to dictionaries to
    # dictionaries (this maps an image number to four channels (RGB and
    # grayscale), which map to a statistical value)
    allDysplasia = []
    allHealthy = []

    # 2. Cylce through all dysplasia images in TrainingData directory
    dys_n = 0
    while True:

        imgname = helps.create_image_name(dysplasia, dys_n)
        filename = dirname + imgname
        # 2a. extract RGB + grayscale
        rgbimage = cer.read_image(filename)
        gsimage = cer.read_image(filename, False)
        if rgbimage is None or gsimage is None:
            break

        info(filename)
        channels = cer.extract_RGB(rgbimage)
        channels["gray"] = cer.create_grayscale_channel(gsimage)

        # 2b. Blue channel analysis
        blue_channels = cer.remove_glare(channels["blue"], 240)
        bluestats = cer.channel_stats(blue_channels)
        info("BLUE")
        helps.print_channel_stats(bluestats, True)

        # 2c. Green channel analysis
        greenstats = cer.channel_stats(channels["green"])
        info("GREEN")
        helps.print_channel_stats(greenstats, True)

        # 2d. Red channel analysis
        redstats = cer.channel_stats(channels["red"])
        info("RED")
        helps.print_channel_stats(redstats, True)

        # 2e. Grayscale channel analysis
        graystats = cer.channel_stats(channels["gray"])
        info("GRAYSCALE")
        helps.print_channel_stats(graystats, True)

        # 2f. Create dictionary of dictionary of dictionary
        allstats = {
            "red": redstats,
            "green": greenstats,
            "blue": bluestats,
            "gray": graystats
        }

        imagestats = {dys_n: allstats}

        # 2g. Update/append list for dysplasia images
        allDysplasia.append(imagestats)
        dys_n += 1

    # 3. Cycle through all healthy images in TrainingData and repeat 2a-g
    hea_n = 0
    while True:

        imgname = helps.create_image_name(healthy, hea_n)
        filename = dirname + imgname
        # 3a. extract RGB + grayscale
        rgbimage = cer.read_image(filename)
        gsimage = cer.read_image(filename, False)
        if rgbimage is None or gsimage is None:
            break

        info(filename)
        channels = cer.extract_RGB(rgbimage)
        channels["gray"] = cer.create_grayscale_channel(gsimage)

        # 3b. Blue channel analysis
        blue_channels = cer.remove_glare(channels["blue"], 240)
        bluestats = cer.channel_stats(blue_channels)
        info("BLUE")
        helps.print_channel_stats(bluestats, True)

        # 3c. Green channel analysis
        greenstats = cer.channel_stats(channels["green"])
        info("GREEN")
        helps.print_channel_stats(greenstats, True)

        # 3d. Red channel analysis
        redstats = cer.channel_stats(channels["red"])
        info("RED")
        helps.print_channel_stats(redstats, True)

        # 3e. Grayscale channel analysis
        graystats = cer.channel_stats(channels["gray"])
        info("GRAYSCALE")
        helps.print_channel_stats(graystats, True)

        # 3f. Create dictionary of dictionary of dictionary
        allstats = {
            "red": redstats,
            "green": greenstats,
            "blue": bluestats,
            "gray": graystats
        }

        imagestats = {hea_n: allstats}

        # 3g. Update/append list for healthy images
        allHealthy.append(imagestats)
        hea_n += 1

        # 4. Save parameter
        with open('svm_param2.txt', 'w') as f:
            json.dump({"dysplasia": allDysplasia, "healthy": allHealthy}, f)

    info("EXIT_SUCCESS")