def imghTest(img_path): image = None image_path = img_path # load default image if image is left blank if not img_path: dbg.dprintln( INFO, "NO IMAGE PATH SPECIFIED, LOADING DEFAULT IMAGE: " + cfg.DEFAULT_IMAGE_PATH) image_path = cfg.DEFAULT_IMAGE_PATH dbg.dprintln(VERBOSE, "imghTest() image_path = " + image_path, 1) # test case 1: check if file exists (THIS CHECKS FROM OUR CURRENT PATH) ret_val = imgh.imageExists(image_path) dbg.dprintln(INFO, "imghTest() image_exists() = " + rv.getRetString(ret_val), 1) # test case 2: convert image to grayscale if ret_val == rv.SUCCESS: [ret_val, image] = imgh.readImageGrayscale(image_path) elif ret_val == rv.ERROR_NO_FILE: dbg.dprintln(INFO, "NO FILE AT SPECIFIED PATH") elif ret_val == rv.ERROR_NO_IMAGE: dbg.dprintln(INFO, "NO IMAGE AT SPECIFIED PATH") return [ret_val, image]
def imghTest(img_path): image = None dbg.dprintln(VERBOSE, "imghTest() img_path = " + img_path, 1) # test case 1: check if file exists (THIS CHECKS FROM OUR CURRENT PATH) ret_val = imgh.imageExists(img_path) dbg.dprintln(INFO, "imghTest() image_exists() = " + rv.getRetString(ret_val), 1) # test case 2: convert image to grayscale if ret_val == rv.SUCCESS: [ret_val, image] = imgh.readImageGrayscale(img_path) return [ret_val, image]
def globalThresholding(image): new_image = image.copy() threshold = find_optimal_threshold(compute_histogram(new_image)) dbg.dprintln(VERBOSE, "Threshold is: " + str(threshold), 2) for row in range(new_image.shape[0]): for col in range(new_image.shape[1]): if new_image[row, col] >= threshold: new_image[row, col] = 255 else: new_image[row, col] = 0 return [new_image, threshold]
def compute_histogram(image): hist = [0] * 256 for row in range(image.shape[0]): for col in range(image.shape[1]): pixel_val = image[row, col] if pixel_val >= 0 and pixel_val < 256: hist[pixel_val] = hist[pixel_val] + 1 else: dbg.dprintln( CRITICAL, "Found erroneous pixel value: " + str(pixel_val) + " at " + str(row) + ", " + str(col), 2) return hist
def main(): dbg.enable() # SET DEBUG LEVEL dbg.setLevel(ALL) dbg.dprintln(INFO, "test.py main() started") # # start test functions # # image handler testing # ./image or image all works [ret_val, image] = imghTest("./image/_raw/oscar_wilde_1.jpg") if ret_val == rv.SUCCESS: # TESTING GRAYSCALE DISCRETATION AND BIMODAL IMAGES # discrete grayscale image imgh.writeImage(imgh.BW, "test_image_discretegrayscale.jpg", image) # inverted test invert_image = imgh.invert(image) imgh.writeImage(imgh.BW, "test_image_inverted_discretegrayscale.jpg", invert_image) # user defined threshold image + threshold value [user_image, user_threshold] = imgh.imageToBimodal(imgh.USER_THRESH, image, 220) imgh.writeImage(imgh.BIN, "test_image_bimodal_user.jpg", user_image) # global defined threshold image + global threshold value [global_image, global_threshold] = imgh.imageToBimodal(imgh.GLOBAL_THRESH, image) imgh.writeImage(imgh.BIN, "test_image_bimodal_global.jpg", global_image) # TEST MORPHOLOGIES # erosion ret_image = mo.erode(user_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_erode.jpg", ret_image) ret_image = mo.dilate(user_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_dilation.jpg", ret_image) ret_image = mo.open(user_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_open.jpg", ret_image) ret_image = mo.close(user_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_close.jpg", ret_image) # # end test functions # dbg.dprintln(INFO, "test.py main() ended")
def readImageGrayscale(abs_image_path, binarize=False): if abs_image_path is not None: # success if imageExists(abs_image_path) == retval.SUCCESS: image = cv2.imread(abs_image_path, cv2.IMREAD_GRAYSCALE) return [retval.SUCCESS, image] # bad file path else: dbg.dprintln( CRITICAL, "readImageGrayscale() invalid image path, no file found") return [retval.ERROR_NO_FILE, None] # bad input argument abs_image_path else: dbg.dprintln(CRITICAL, "readImageGrayscale() no image path defined") return [retval.ERROR_NO_IMAGE, None]
def find_optimal_threshold(hist): # init counters max_lo = 0 max_hi = 0 lo_cnt = 0 hi_cnt = 0 # init threshold threshold = len(hist) // 2 # find the avg prob val before and after threshold for x in range(len(hist)): if x < threshold: max_lo = max_lo + x * hist[x] lo_cnt = lo_cnt + hist[x] else: max_hi = max_hi + x * hist[x] hi_cnt = hi_cnt + hist[x] # calc threshold value max_lo = max_lo // lo_cnt max_hi = max_hi // hi_cnt threshold = (max_lo + max_hi) // 2 dbg.dprintln( VERBOSE, "Threshold value is: " + str(threshold) + " with maximas: " + str(max_lo) + ", " + str(max_hi), 2) return threshold
def writeImage(type, filename, image): if (".jpg" in str(filename)): if type == BW: dbg.dprintln(INFO, "writeImage() Writing BW file " + str(filename), 1) path = "image/bw/" + str(filename) cv2.imwrite(path, image) elif type == BIN: dbg.dprintln(INFO, "writeImage() Writing BINARY file " + str(filename), 1) path = "image/binary/" + str(filename) cv2.imwrite(path, image) else: dbg.dprintln( INFO, "writeImage() Type is not BW or BINARY, not writing image " + str(filename), 1) else: dbg.dprintln( INFO, "writeImage() .JPG extension not found in filename, not writing image " + str(filename), 1)
def Function(): dbg.dprintln(CRITICAL, "Text from a function")
from debug import debug as dbg # These values can be used as integers, they wont change in value # if you specify an integer HIGHER than 3 then everything will show # CRITICAL = 0 # INFO = 2 # VERBOSE = 3 # ALL = 4 CRITICAL = dbg.CRITICAL INFO = dbg.INFO VERBOSE = dbg.VERBOSE ALL = dbg.ALL # # Printing with control over output # dbg.dprintln(INFO, "INFO is too high, this wont print") dbg.dprintln(VERBOSE, "Verbose is too high, this wont print") dbg.dprintln(ALL, "ALL is too high this wont print") # # Printing without a newline # has optional 3rd argument for tabs # dbg.dprintHeader(CRITICAL) dbg.dprint(CRITICAL, "Text without") dbg.dprint(CRITICAL, " a newline") dbg.dprint(CRITICAL, "and one tab ", 1) dbg.dprint(CRITICAL, "in the middle\n") #
def imageToBimodal(type, image, threshold=127): dbg.dprintln(INFO, "imageToBimodal(): threshold specified " + str(threshold)) # invalid threshold value, return if threshold < 0 or threshold > 255: dbg.dprintln(CRITICAL, "Invalid threshold value: " + str(threshold), 1) # check for type and return the threshold elif type == NO_THRESH: dbg.dprintln(INFO, "No threshold, returning raw image untouched", 1) # user specified bimodal threshold elif type == USER_THRESH: dbg.dprintln(INFO, "User threshold specified " + str(threshold), 1) image = userThresholding(image, threshold) # global threshold elif type == GLOBAL_THRESH: dbg.dprintln( INFO, "Global threshold, automatically finding optimal threshold based on histogram" ) [image, threshold] = globalThresholding(image) # not yet implemented else: dbg.dprintln(CRITICAL, "Thresholding mode has no valid implementation") return [image, threshold]
def main(): dbg.enable() # SET DEBUG LEVEL dbg.setLevel(ALL) dbg.dprintln(INFO, "test.py main() started") # # start test functions # # image handler testing # ./image or image all works # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", type=str, help="path to input image") ap.add_argument( "-c", "--conversion", type=aph.conversion2int, default=cfg.CONVERSION_ARG_BW, help="intermediate image conversion: 'bw' or 'bin'; defaults to 'bw'") ap.add_argument( "-t", "--threshold", type=int, help="define an integer threshold for bimodal image conversion: (0-255)" ) ap.add_argument( "-m", "--morph", type=aph.morph2int, help= "define any number of morphological operations separated by commas: 'erode', 'dilate', 'open' or 'close'" ) ap.add_argument("--invert", type=aph.str2bool, default=False, help="invert image?:'t' or 'f'; defaults to False") args = vars(ap.parse_args()) # load the input image [ret_val, image] = imghTest(args["image"]) if ret_val == rv.SUCCESS: # TESTING GRAYSCALE DISCRETATION AND BIMODAL IMAGES intermediate_image = None if args["conversion"] == cfg.CONVERSION_ARG_BW: # discrete grayscale image imgh.writeImage(imgh.BW, "test_image_discretegrayscale.jpg", image) # inverted test if args["invert"]: intermediate_image = imgh.invert(image) imgh.writeImage(imgh.BW, "test_image_inverted_discretegrayscale.jpg", intermediate_image) elif args["conversion"] == cfg.CONVERSION_ARG_BIN: if args["threshold"]: # user defined threshold image + threshold value [intermediate_image, user_threshold] = imgh.imageToBimodal(imgh.USER_THRESH, image, args["threshold"]) imgh.writeImage(imgh.BIN, "test_image_bimodal_user.jpg", intermediate_image) else: # global defined threshold image + global threshold value [intermediate_image, global_threshold ] = imgh.imageToBimodal(imgh.GLOBAL_THRESH, image) imgh.writeImage(imgh.BIN, "test_image_bimodal_global.jpg", intermediate_image) # TEST MORPHOLOGIES # erosion if not args["morph"]: dbg.dprintln( INFO, "No morphological operation defined; operation will not be applied to image" ) else: for morph_arg in args["morph"]: if morph_arg == cfg.MORPH_ARG_ERODE: ret_image = mo.erode(intermediate_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_erode.jpg", ret_image) if morph_arg == cfg.MORPH_ARG_DILATE: ret_image = mo.dilate(intermediate_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_dilation.jpg", ret_image) if morph_arg == cfg.MORPH_ARG_OPEN: ret_image = mo.open(intermediate_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_open.jpg", ret_image) if morph_arg == cfg.MORPH_ARG_CLOSE: ret_image = mo.close(intermediate_image, 1) imgh.writeImage(imgh.BIN, "test_image_bimodal_user_close.jpg", ret_image) # TEST # # end test functions # dbg.dprintln(INFO, "test.py main() ended")