def test_read_image(): """ Tests functionality of read_image from cervical.py """ # Create and save an image that is just four pixels as such: # [RED] [YELLOW] # [GREEN] [BLUE] test_image = ones((2, 2, 3), uint8) test_image[0][0] = helps.colorDict["red"] test_image[0][1] = helps.colorDict["yellow"] test_image[1][0] = helps.colorDict["green"] test_image[1][1] = helps.colorDict["blue"] cv2.imwrite("test.png", test_image) # Read color image and verify values clone_image = cer.read_image("./test.png") assert_equal(clone_image, test_image) # Read grayscale image gray_image = cer.read_image("./test.png", False) assert_raises(AssertionError, assert_equal, gray_image, test_image) assert_raises(AssertionError, assert_equal, gray_image, clone_image) for i in range(2): for j in range(2): try: len(gray_image[i][j]) assert False except TypeError: assert gray_image[i][j] >= 0 assert gray_image[i][j] < 256
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 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 test_critical_pixel_density(): """ Tests functionality of critical_pixel_density from cervical.py """ testCritVals = {"red": (240, 255), # these values are looking for yellow "green": (240, 255), "blue": (0, 25)} # Test case 1 - 1/4 pixels match img1 = cer.read_image("./test.png") output1 = cer.critical_pixel_density(img1, testCritVals) assert output1 == 0.25 # Test case 2 - 0/4 pixels match img2 = ones((2, 2, 3), uint8) img2[0][0] = helps.colorDict["blue"] img2[0][1] = helps.colorDict["blue"] img2[1][0] = helps.colorDict["blue"] img2[1][1] = helps.colorDict["blue"] output2 = cer.critical_pixel_density(img2, testCritVals) assert output2 == 0
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")
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")