Ejemplo n.º 1
0
def compare_pngs(actual, expected):
    """ Compare two images using RMS approach.
    
    Actual and expected are paths to PNG files.
    
    Returns True for mismatch, False otherwise.
    """

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)
    expectedImage = expectedImage[:, :, :3]
    actualImage = actualImage[:, :, :3]

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expectedImage = expectedImage.astype(np.int16)
    actualImage = actualImage.astype(np.int16)

    num_values = np.prod(expectedImage.shape)
    abs_diff_image = abs(expectedImage - actualImage)

    histogram = np.bincount(abs_diff_image.ravel(), minlength=256)

    sum_of_squares = np.sum(histogram * np.arange(len(histogram)) ** 2)
    rms = np.sqrt(float(sum_of_squares) / num_values)
    
    return rms
Ejemplo n.º 2
0
def encode(message):
    """Encode the message string into images based on PNGs in pool/, 
	and store the resulting images in encoded/.
	"""
    if len(message) == 0:
        return

    pool_list = ["pool/" + f for f in os.listdir("pool/") if '.png' in f]
    assert len(message) <= max_length()

    int_list = [ord(c) for c in message] + [0]

    for i in range(len(pool_list)):
        pool_im = mpng.read_png_int(pool_list[i])

        assert pool_im.shape[2] == 3  #check if RGB image

        # print(len(tiles(pool_im.shape[0], pool_im.shape[1])), (pool_im.shape[0]//2) * (pool_im.shape[1]//8))
        for tile in tiles(pool_im.shape[0], pool_im.shape[1]):
            encode_int(pool_im, tile, int_list.pop(0))

            if len(int_list) == 0:
                break

        png.from_array(pool_im,
                       mode='RGB').save("encoded/image_{0}.png".format(i))

        if len(int_list) <= 1:
            break

    return
Ejemplo n.º 3
0
 def __init__(self, img_path):
     self.img = png.read_png_int(img_path)
     self.traffic_light = []
     self.traffic_lights_3d_location = []
     self.EM = []
     self.corresponding_ind = []
     self.valid = []
Ejemplo n.º 4
0
def test_imread_png_uint16():
    from matplotlib import _png
    img = _png.read_png_int(os.path.join(os.path.dirname(__file__),
                                         'baseline_images/test_png/uint16.png'))

    assert (img.dtype == np.uint16)
    assert np.sum(img.flatten()) == 134184960
def test_imread_png_uint16():
    from matplotlib import _png
    with (Path(__file__).parent
          / 'baseline_images/test_png/uint16.png').open('rb') as file:
        img = _png.read_png_int(file)
    assert (img.dtype == np.uint16)
    assert np.sum(img.flatten()) == 134184960
Ejemplo n.º 6
0
def main():
    plt.ion()
    im2 = png.read_png_int('0_disc.png')
    im1 = png.read_png_int('0_macular.png')
    # im1 = matplotlib.image.imread('orange.jpg')
    # im1 = matplotlib.image.imread('0_disc.png')
    im1, im2 = preprocess_images(im1, im2)

    gp_1, gp_2 = [gaussian_pyramid(im) for im in [im1, im2]]
    lp_1, lp_2 = [laplacian_pyramid(gp) for gp in [gp_1, gp_2]]
    lp_join = laplacian_pyr_join(lp_1, lp_2)
    im_join = laplacian_collapse(lp_join)

    np.clip(im_join, 0, 255, out=im_join)
    im_join = np.uint8(im_join)
    plt.imsave('orapple.jpg', im_join)
    return 0
Ejemplo n.º 7
0
def max_length():
    """Returns max. length message that can be encoded based on
	PNGs in pool/ and SPACING.
	"""
    pool_list = ["pool/" + f for f in os.listdir("pool/") if '.png' in f]

    max_chars = 0
    for pool_im_name in pool_list:
        pool_im = mpng.read_png_int(pool_im_name)
        max_chars += len(tiles(pool_im.shape[0], pool_im.shape[1]))

    return max_chars
Ejemplo n.º 8
0
def do_spline(img1_path, img2_path):
    input_img1 = png.read_png_int(img1_path)
    input_img2 = png.read_png_int(img2_path)
    input_img1, input_img2 = preprocess_images(input_img1, input_img2)
    img_nonzero = [np.nonzero(input_img1), np.nonzero(input_img2)]
    img1_L = img_nonzero[0][1][0]
    img2_L = img_nonzero[1][1][0]
    L_min = np.argmax([img1_L, img2_L])
    if L_min == 0:
        img1 = input_img2
        img2 = input_img1
    else:
        img1 = input_img1
        img2 = input_img2

    gp_1, gp_2 = [gaussian_pyramid(im) for im in [img1, img2]]
    lp_1, lp_2 = [laplacian_pyramid(gp) for gp in [gp_1, gp_2]]
    lp_join = laplacian_pyr_join(lp_1, lp_2)
    im_join = laplacian_collapse(lp_join)

    np.clip(im_join, 0, 255, out=im_join)
    im_join = np.uint8(im_join)
    # plt.imsave('orapple.jpg', im_join)
    return im_join
Ejemplo n.º 9
0
def decode():
    """Decode message from PNGs in encoded/.
	Returns the decoded message string.
	"""
    number_encoded_images = len(
        [f for f in os.listdir("encoded/") if '.png' in f])
    decoded_ints = []

    for i in range(number_encoded_images):
        encoded_im = mpng.read_png_int("encoded/image_{0}.png".format(i))

        for tile in tiles(encoded_im.shape[0], encoded_im.shape[1]):
            decoded_int = decode_int(encoded_im, tile)
            if decoded_int == 0:
                return ''.join([chr(c) for c in decoded_ints])

            decoded_ints.append(decoded_int)

    return ''.join([chr(c) for c in decoded_ints])
Ejemplo n.º 10
0
                    y_cb_cr_img[i, j, 1] + ratio_as_al,
                    y_cb_cr_img[i, j, 2] + ratio_as_al
                ]

    # covert the YCbCr image to the BGR image
    return cv2.cvtColor(y_cb_cr_img, cv2.COLOR_YCR_CB2BGR)


def thresholding(img):
    return cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                 cv2.THRESH_BINARY, 11, 2)


if __name__ == "__main__":
    test_images = np.array([
        png.read_png_int('./data_road/training/image_2/um_000001.png'),
        png.read_png_int('./data_road/training/image_2/um_000002.png'),
        png.read_png_int('./data_road/training/image_2/um_000003.png'),
        png.read_png_int('./data_road/training/image_2/um_000004.png'),
        png.read_png_int('./data_road/training/image_2/um_000005.png'),
        png.read_png_int('./data_road/training/image_2/um_000006.png'),
    ])
    #show_images(test_images)
    white_yellow_images = list(map(select_white_yellow, test_images))
    #show_images(white_yellow_images)
    gray_images = list(map(convert_gray_scale, white_yellow_images))
    #show_images(gray_images)
    #gray_images = list(map(thresholding, gray_images))

    blurred_images = list(
        map(lambda image: apply_smoothing(image), gray_images))
Ejemplo n.º 11
0
import time

data = []

for compression in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):
    for filter in (-1, 'NONE', 'SUB', 'UP', 'AVG', 'PAETH'):
        if not isinstance(filter, int):
            filter_no = getattr(_png, 'PNG_FILTER_' + filter)
        else:
            filter = 'XXX'
            filter_no = -1

        times = 0
        sizes = 0
        for i in range(16):
            filename = 'test{0:04d}.png'.format(i)
            image = _png.read_png_int(filename)

            t = time.time()
            buffer = _png.write_png(image,
                                    None,
                                    compression=compression,
                                    filter=filter_no)
            times += (time.time() - t)
            sizes += len(buffer)

        print(compression, filter, times, sizes)
        data.append((compression, filter, times, sizes))

print(data)
Ejemplo n.º 12
0
        dtype=np.uint8,
    )
    # Loop over all lines and draw them on the blank image.
    for line in lines:
        for x1, y1, x2, y2 in line:
            cv2.line(line_img, (x1, y1), (x2, y2), color, thickness)
    # Merge the image with the lines onto the original.
    img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)
    # Return the modified image.
    return img


if __name__ == "__main__":

    image = png.read_png_int(
        '/Users/jeong-yeonji/mobNavigation/kitti/data_road/training/image_2/um_000012.png'
    )
    height, width = image.shape[:2]
    plt.figure()
    plt.imshow(image)

    region_of_interest_vertices = [
        [0, height],
        [width / 3, height / 2],
        [width * (2 / 3), height / 2],
        [width, height],
    ]
    #
    # cropped_image = region_of_interest(
    #     image,
    #     [np.array(region_of_interest_vertices, np.int32)],
Ejemplo n.º 13
0
def compare_images(expected, actual, tol, in_decorator=False):
    '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a unitless float).  This is used to
               determine the 'fuzziness' to use when comparing images.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

    verify(actual)

    # Convert the image to png
    extension = expected.split('.')[-1]
    if extension != 'png':
        actual = convert(actual)
        expected = convert(expected)

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)

    actualImage, expectedImage = crop_to_same(actual, actualImage, expected,
                                              expectedImage)

    # normalize the images
    expectedImage = image_util.autocontrast(expectedImage, 2)
    actualImage = image_util.autocontrast(actualImage, 2)

    # compare the resulting image histogram functions
    rms = 0
    bins = np.arange(257)
    for i in xrange(0, 3):
        h1p = expectedImage[:, :, i]
        h2p = actualImage[:, :, i]

        h1h = np.histogram(h1p, bins=bins)[0]
        h2h = np.histogram(h2p, bins=bins)[0]

        rms += np.sum(np.power((h1h - h2h), 2))
    rms = np.sqrt(rms / (256 * 3))

    diff_image = os.path.join(os.path.dirname(actual),
                              'failed-diff-' + os.path.basename(actual))

    if ((rms / 10000.0) <= tol):
        if os.path.exists(diff_image):
            os.unlink(diff_image)
        return None

    save_diff_image(expected, actual, diff_image)

    if in_decorator:
        results = dict(
            rms=rms,
            expected=str(expected),
            actual=str(actual),
            diff=str(diff_image),
        )
        return results
    else:
        # old-style call from mplTest directory
        msg = "  Error: Image files did not match.\n"       \
              "  RMS Value: " + str( rms / 10000.0 ) + "\n" \
              "  Expected:\n    " + str( expected ) + "\n"  \
              "  Actual:\n    " + str( actual ) + "\n"      \
              "  Difference:\n    " + str( diff_image ) + "\n"      \
              "  Tolerance: " + str( tol ) + "\n"
        return msg
Ejemplo n.º 14
0
def compare_images( expected, actual, tol, in_decorator=False ):
   '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a unitless float).  This is used to
               determine the 'fuzziness' to use when comparing images.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

   verify(actual)

   # Convert the image to png
   extension = expected.split('.')[-1]
   if extension != 'png':
      actual = convert(actual, False)
      expected = convert(expected, True)

   # open the image files and remove the alpha channel (if it exists)
   expectedImage = _png.read_png_int( expected )
   actualImage = _png.read_png_int( actual )

   actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)

   # compare the resulting image histogram functions
   expected_version = version.LooseVersion("1.6")
   found_version = version.LooseVersion(np.__version__)

   rms = calculate_rms(expectedImage, actualImage)

   diff_image = make_test_filename(actual, 'failed-diff')

   if ( (rms / 10000.0) <= tol ):
      if os.path.exists(diff_image):
         os.unlink(diff_image)
      return None

   # For Agg-rendered images, we can retry by ignoring pixels with
   # differences of only 1
   if extension == 'png':
       # Remove differences of only 1
       diffImage = np.abs(np.asarray(actualImage, dtype=np.int) -
                          np.asarray(expectedImage, dtype=np.int))
       actualImage = np.where(diffImage <= 1, expectedImage, actualImage)

       rms = calculate_rms(expectedImage, actualImage)

       if ( (rms / 10000.0) <= tol ):
           if os.path.exists(diff_image):
               os.unlink(diff_image)
           return None

   save_diff_image( expected, actual, diff_image )

   if in_decorator:
      results = dict(
         rms = rms,
         expected = str(expected),
         actual = str(actual),
         diff = str(diff_image),
         )
      return results
   else:
      # old-style call from mplTest directory
      msg = "  Error: Image files did not match.\n"       \
            "  RMS Value: " + str( rms / 10000.0 ) + "\n" \
            "  Expected:\n    " + str( expected ) + "\n"  \
            "  Actual:\n    " + str( actual ) + "\n"      \
            "  Difference:\n    " + str( diff_image ) + "\n"      \
            "  Tolerance: " + str( tol ) + "\n"
      return msg
Ejemplo n.º 15
0
def compare_images( expected, actual, tol, in_decorator=False ):
   '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a color value difference, where 255 is the
               maximal difference).  The test fails if the average pixel
               difference is greater than this value.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

   verify(actual)

   # Convert the image to png
   extension = expected.split('.')[-1]

   if not os.path.exists(expected):
       raise IOError('Baseline image %r does not exist.' % expected)

   if extension != 'png':
      actual = convert(actual, False)
      expected = convert(expected, True)

   # open the image files and remove the alpha channel (if it exists)
   expectedImage = _png.read_png_int( expected )
   actualImage = _png.read_png_int( actual )
   expectedImage = expectedImage[:, :, :3]
   actualImage = actualImage[:, :, :3]

   actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)

   # convert to signed integers, so that the images can be subtracted without
   # overflow
   expectedImage = expectedImage.astype(np.int16)
   actualImage = actualImage.astype(np.int16)

   rms = calculate_rms(expectedImage, actualImage)

   diff_image = make_test_filename(actual, 'failed-diff')

   if rms <= tol:
      if os.path.exists(diff_image):
         os.unlink(diff_image)
      return None

   save_diff_image( expected, actual, diff_image )

   if in_decorator:
      results = dict(
         rms = rms,
         expected = str(expected),
         actual = str(actual),
         diff = str(diff_image),
         )
      return results
   else:
      # old-style call from mplTest directory
      msg = "  Error: Image files did not match.\n"       \
            "  RMS Value: " + str( rms ) + "\n" \
            "  Expected:\n    " + str( expected ) + "\n"  \
            "  Actual:\n    " + str( actual ) + "\n"      \
            "  Difference:\n    " + str( diff_image ) + "\n"      \
            "  Tolerance: " + str( tol ) + "\n"
      return msg
Ejemplo n.º 16
0
    # project_clip = VideoFileClip("project_video.mp4")
    #
    # # Importing calibration images
    # cal_filenames = glob.glob('camera_cal/*.jpg')
    # cal_images = np.array([np.array(plt.imread(img)) for img in cal_filenames])
    #
    # # Importing test images
    # test_filenames = glob.glob('test_images/*.jpg')
    # test_images = np.array([np.array(plt.imread(img)) for img in test_filenames])
    #
    # # Chessboard edges
    # nx = 9
    # ny = 6

    test_images = np.array([
        png.read_png_int('./../../kitti/data_road/training/image_2/um_000000.png'),
        png.read_png_int('./../../kitti/data_road/training/image_2/um_000001.png'),
        png.read_png_int('./../../kitti/data_road/training/image_2/um_000002.png'),
    ])
    img = test_images[0]
    img_y, img_x = (img.shape[0], img.shape[1])
    offset = 50

    # Lane masking and coordinates for perspective transform
    # source = np.float32([  # MASK
    #     [img_y - offset, offset],  # bottom left
    #     [img_y - offset, img_x - offset],  # bottom right
    #     [offset, offset],  # top left
    #     [offset, img_x - offset]])  # top right

    source = np.float32([
Ejemplo n.º 17
0
    assert len(
        sys.argv) == 3, "Usage : python execute.py <input_dir> <output_dir>"

    input_dir = sys.argv[1]
    output_dir = sys.argv[2]

    input_dir = os.path.join(os.path.join(input_dir, "training"), "image_2")
    filenames = os.listdir(input_dir)

    um_images = [filename for filename in filenames if filename[:3] == "um_"]
    um_images.sort()

    os.makedirs(output_dir, exist_ok=True)

    for filename in um_images:
        png_image = png.read_png_int(os.path.join(input_dir, filename))

        white_yellow_image = select_white_yellow(png_image)
        gray_image = convert_gray_scale(white_yellow_image)
        gray_image = thresholding(gray_image)
        blurred_image = apply_smoothing(gray_image)
        edge_image = detect_edges(blurred_image)
        roi_image = select_region(edge_image)
        lines = hough_lines(roi_image)
        lane_line = detect_lanes(roi_image, lines)

        o_blurred_image = apply_smoothing(png_image)
        o_edge_image = detect_edges(o_blurred_image)
        o_roi_image = select_region(o_edge_image)
        o_lines = hough_lines(o_roi_image)
        o_lane_line = detect_lanes(o_roi_image, o_lines)
Ejemplo n.º 18
0
def mura_preprocess(train_path):
    train_path = "MURA-v1.1/"
    csv_train_filess = os.path.join("dataloader", train_path,
                                    "train_labeled_studies.csv")
    csv_valid_filess = os.path.join("dataloader", train_path,
                                    "valid_labeled_studies.csv")

    train_df = pd.read_csv(csv_train_filess,
                           names=['img', 'label'],
                           header=None)
    valid_df = pd.read_csv(csv_valid_filess,
                           names=['img', 'label'],
                           header=None)

    train_img_paths = train_df.img.values.tolist()
    valid_img_paths = valid_df.img.values.tolist()
    train_labels_patient = train_df.label.values.tolist()
    valid_labels_patient = valid_df.label.values.tolist()
    train_data_list = []
    train_labels = []
    valid_data_list = []
    valid_labels = []

    for i in range(len(train_img_paths) / 100):
        patient_dir = os.path.join("dataloader", train_img_paths[i])
        msg = "\r Loading: %s (%d/%d)    " % (patient_dir, i + 1,
                                              len(train_img_paths))
        sys.stdout.write(msg)
        sys.stdout.flush()
        for f in glob.glob(patient_dir + "*"):
            train_data_patient = []
            train_img = png.read_png_int(f)
            if train_img.shape != (512, 512):
                if len(train_img.shape) > 2:
                    train_img = train_img[:, :, 1]
                l, w = train_img.shape
                train_img = np.pad(train_img,
                                   [((512 - l) / 2, 512 / 2 - l / 2),
                                    ((512 - w) / 2, 512 / 2 - w / 2)],
                                   'constant',
                                   constant_values=0)
            train_img = imresize(train_img, (256, 256))
            # you can replace 256 with other number but any number greater then 256 will exceed the memory limit of 12GB
            train_img = np.stack((train_img, ) * 3, -1)
            train_data_patient.append(train_img)
        train_data_list.extend(train_data_patient)
        for _ in range(len(train_data_patient)):
            lst = [0, 0]
            lst[train_labels_patient[i]] = 1
            train_labels.append(lst)
    train_data = np.asarray(train_data_list)

    for i in range(len(valid_img_paths) / 10):
        patient_dir = os.path.join("dataloader", valid_img_paths[i])
        msg = "\r Loading: %s (%d/%d)     " % (patient_dir, i + 1,
                                               len(valid_img_paths))
        sys.stdout.write(msg)
        sys.stdout.flush()
        for f in glob.glob(patient_dir + "*"):
            valid_data_patient = []
            valid_img = png.read_png_int(f)
            if train_img.shape != (512, 512):
                if len(valid_img.shape) > 2:
                    valid_img = valid_img[:, :, 1]
                l, w = valid_img.shape
                valid_img = np.pad(valid_img,
                                   [((512 - l) / 2, 512 / 2 - l / 2),
                                    ((512 - w) / 2, 512 / 2 - w / 2)],
                                   'constant',
                                   constant_values=0)
            valid_img = imresize(valid_img, (256, 256))
            valid_img = np.stack((valid_img, ) * 3, -1)
            valid_data_patient.append(valid_img)
        valid_data_list.extend(valid_data_patient)
        for _ in range(len(valid_data_patient)):
            lst = [0, 0]
            lst[valid_labels_patient[i]] = 1
            valid_labels.append(lst)
    valid_data = np.asarray(valid_data_list)

    return np.array(train_data), np.array(train_labels), np.array(
        valid_data), np.array(valid_labels)
Ejemplo n.º 19
0
def compare_images(expected, actual, tol, in_decorator=False):
    '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a unitless float).  This is used to
               determine the 'fuzziness' to use when comparing images.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

    verify(actual)

    # Convert the image to png
    extension = expected.split('.')[-1]
    if extension != 'png':
        actual = convert(actual, False)
        expected = convert(expected, True)

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)

    actualImage, expectedImage = crop_to_same(actual, actualImage, expected,
                                              expectedImage)

    # compare the resulting image histogram functions
    expected_version = version.LooseVersion("1.6")
    found_version = version.LooseVersion(np.__version__)

    rms = calculate_rms(expectedImage, actualImage)

    diff_image = make_test_filename(actual, 'failed-diff')

    if ((rms / 10000.0) <= tol):
        if os.path.exists(diff_image):
            os.unlink(diff_image)
        return None

    # For Agg-rendered images, we can retry by ignoring pixels with
    # differences of only 1
    if extension == 'png':
        # Remove differences of only 1
        diffImage = np.abs(
            np.asarray(actualImage, dtype=np.int) -
            np.asarray(expectedImage, dtype=np.int))
        actualImage = np.where(diffImage <= 1, expectedImage, actualImage)

        rms = calculate_rms(expectedImage, actualImage)

        if ((rms / 10000.0) <= tol):
            if os.path.exists(diff_image):
                os.unlink(diff_image)
            return None

    save_diff_image(expected, actual, diff_image)

    if in_decorator:
        results = dict(
            rms=rms,
            expected=str(expected),
            actual=str(actual),
            diff=str(diff_image),
        )
        return results
    else:
        # old-style call from mplTest directory
        msg = "  Error: Image files did not match.\n"       \
              "  RMS Value: " + str( rms / 10000.0 ) + "\n" \
              "  Expected:\n    " + str( expected ) + "\n"  \
              "  Actual:\n    " + str( actual ) + "\n"      \
              "  Difference:\n    " + str( diff_image ) + "\n"      \
              "  Tolerance: " + str( tol ) + "\n"
        return msg
Ejemplo n.º 20
0
def time_imread_png_uint16():
    img = _png.read_png_int(
        os.path.join(os.path.dirname(__file__), 'data/uint16.png'))
Ejemplo n.º 21
0
import glob
import matplotlib.pyplot as plt
import matplotlib._png as png
import h5py
import numpy as np
shuffle_data = True  # shuffle the addresses before saving
hdf5_path = '/home/mak/Desktop/Stroboscopy/dataset1.hdf5'  # address to where you want to save the hdf5 file
train_path = '/home/mak/Desktop/Stroboscopy/AllImages/*.png'
# read addresses and labels from the 'train' folder
addrs = glob.glob(train_path)
# to shuffle data

train_addrs = addrs
hdf5_file = h5py.File(hdf5_path, mode='w')
train_shape = (len(train_addrs), 576, 720, 3)
hdf5_file.create_dataset("train_img", train_shape, np.uint8)

for i in range(len(train_addrs)):
    # print how many images are saved every 1000 images
    if i % 100 == 0 and i > 1:
        print 'Train data: {}/{}'.format(i, len(train_addrs))
    # read an image and resize to (224, 224)
    # cv2 load images as BGR, convert it to RGB
    addr = train_addrs[i]
    img = png.read_png_int(addr)
    print img.dtype
    # add any image pre-processing here
    # if the data order is Theano, axis orders should change
    hdf5_file["train_img"][i, :, :, :] = img
#
hdf5_file.close()
Ejemplo n.º 22
0
import matplotlib.pyplot as plt
import matplotlib._png as png
import numpy as np

path = "/home/mak/Desktop/Stroboscopy/AllImages/3.png"
img = png.read_png_int(path)
print img.dtype
print img[0:10, 0:10, 0]
plt.imshow(img)
plt.show()
Ejemplo n.º 23
0
def compare_images( expected, actual, tol, in_decorator=False ):
   '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a color value difference, where 255 is the
               maximal difference).  The test fails if the average pixel
               difference is greater than this value.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

   verify(actual)

   # Convert the image to png
   extension = expected.split('.')[-1]

   if not os.path.exists(expected):
       raise IOError('Baseline image %r does not exist.' % expected)

   if extension != 'png':
      actual = convert(actual, False)
      expected = convert(expected, True)

   # open the image files and remove the alpha channel (if it exists)
   expectedImage = _png.read_png_int( expected )
   actualImage = _png.read_png_int( actual )
   expectedImage = expectedImage[:, :, :3]
   actualImage = actualImage[:, :, :3]

   actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)

   # convert to signed integers, so that the images can be subtracted without
   # overflow
   expectedImage = expectedImage.astype(np.int16)
   actualImage = actualImage.astype(np.int16)

   # compare the resulting image histogram functions
   expected_version = version.LooseVersion("1.6")
   found_version = version.LooseVersion(np.__version__)

   # On Numpy 1.6, we can use bincount with minlength, which is much faster than
   # using histogram
   if found_version >= expected_version:
      rms = 0

      for i in xrange(0, 3):
         h1p = expectedImage[:,:,i]
         h2p = actualImage[:,:,i]

         h1h = np.bincount(h1p.ravel(), minlength=256)
         h2h = np.bincount(h2p.ravel(), minlength=256)

         rms += np.sum(np.power((h1h-h2h), 2))
   else:
      rms = 0
      ns = np.arange(257)

      for i in xrange(0, 3):
         h1p = expectedImage[:,:,i]
         h2p = actualImage[:,:,i]

         h1h = np.histogram(h1p, bins=ns)[0]
         h2h = np.histogram(h2p, bins=ns)[0]

         rms += np.sum(np.power((h1h-h2h), 2))

   rms = calculate_rms(expectedImage, actualImage)

   diff_image = make_test_filename(actual, 'failed-diff')

   if rms <= tol:
      if os.path.exists(diff_image):
         os.unlink(diff_image)
      return None

   save_diff_image( expected, actual, diff_image )

   if in_decorator:
      results = dict(
         rms = rms,
         expected = str(expected),
         actual = str(actual),
         diff = str(diff_image),
         )
      return results
   else:
      # old-style call from mplTest directory
      msg = "  Error: Image files did not match.\n"       \
            "  RMS Value: " + str( rms ) + "\n" \
            "  Expected:\n    " + str( expected ) + "\n"  \
            "  Actual:\n    " + str( actual ) + "\n"      \
            "  Difference:\n    " + str( diff_image ) + "\n"      \
            "  Tolerance: " + str( tol ) + "\n"
      return msg
Ejemplo n.º 24
0
def compare_images(expected, actual, tol, in_decorator=False):
    '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a color value difference, where 255 is the
               maximal difference).  The test fails if the average pixel
               difference is greater than this value.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

    verify(actual)

    # Convert the image to png
    extension = expected.split('.')[-1]
    if extension != 'png':
        actual = convert(actual, False)
        expected = convert(expected, True)

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)
    expectedImage = expectedImage[:, :, :3]
    actualImage = actualImage[:, :, :3]

    actualImage, expectedImage = crop_to_same(actual, actualImage, expected,
                                              expectedImage)

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expectedImage = expectedImage.astype(np.int16)
    actualImage = actualImage.astype(np.int16)

    rms = calculate_rms(expectedImage, actualImage)

    diff_image = make_test_filename(actual, 'failed-diff')

    if rms <= tol:
        if os.path.exists(diff_image):
            os.unlink(diff_image)
        return None

    save_diff_image(expected, actual, diff_image)

    if in_decorator:
        results = dict(
            rms=rms,
            expected=str(expected),
            actual=str(actual),
            diff=str(diff_image),
        )
        return results
    else:
        # old-style call from mplTest directory
        msg = "  Error: Image files did not match.\n"       \
              "  RMS Value: " + str( rms ) + "\n" \
              "  Expected:\n    " + str( expected ) + "\n"  \
              "  Actual:\n    " + str( actual ) + "\n"      \
              "  Difference:\n    " + str( diff_image ) + "\n"      \
              "  Tolerance: " + str( tol ) + "\n"
        return msg
Ejemplo n.º 25
0
def compare_images(expected, actual, tol, in_decorator=False):
    '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a color value difference, where 255 is the
               maximal difference).  The test fails if the average pixel
               difference is greater than this value.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

    verify(actual)

    # Convert the image to png
    extension = expected.split('.')[-1]

    if not os.path.exists(expected):
        raise IOError('Baseline image %r does not exist.' % expected)

    if extension != 'png':
        actual = convert(actual, False)
        expected = convert(expected, True)

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)
    expectedImage = expectedImage[:, :, :3]
    actualImage = actualImage[:, :, :3]

    actualImage, expectedImage = crop_to_same(actual, actualImage, expected,
                                              expectedImage)

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expectedImage = expectedImage.astype(np.int16)
    actualImage = actualImage.astype(np.int16)

    # compare the resulting image histogram functions
    expected_version = version.LooseVersion("1.6")
    found_version = version.LooseVersion(np.__version__)

    # On Numpy 1.6, we can use bincount with minlength, which is much faster than
    # using histogram
    if found_version >= expected_version:
        rms = 0

        for i in xrange(0, 3):
            h1p = expectedImage[:, :, i]
            h2p = actualImage[:, :, i]

            h1h = np.bincount(h1p.ravel(), minlength=256)
            h2h = np.bincount(h2p.ravel(), minlength=256)

            rms += np.sum(np.power((h1h - h2h), 2))
    else:
        rms = 0
        ns = np.arange(257)

        for i in xrange(0, 3):
            h1p = expectedImage[:, :, i]
            h2p = actualImage[:, :, i]

            h1h = np.histogram(h1p, bins=ns)[0]
            h2h = np.histogram(h2p, bins=ns)[0]

            rms += np.sum(np.power((h1h - h2h), 2))

    rms = calculate_rms(expectedImage, actualImage)

    diff_image = make_test_filename(actual, 'failed-diff')

    if rms <= tol:
        if os.path.exists(diff_image):
            os.unlink(diff_image)
        return None

    save_diff_image(expected, actual, diff_image)

    if in_decorator:
        results = dict(
            rms=rms,
            expected=str(expected),
            actual=str(actual),
            diff=str(diff_image),
        )
        return results
    else:
        # old-style call from mplTest directory
        msg = "  Error: Image files did not match.\n"       \
              "  RMS Value: " + str( rms ) + "\n" \
              "  Expected:\n    " + str( expected ) + "\n"  \
              "  Actual:\n    " + str( actual ) + "\n"      \
              "  Difference:\n    " + str( diff_image ) + "\n"      \
              "  Tolerance: " + str( tol ) + "\n"
        return msg
Ejemplo n.º 26
0
def mura_preprocess(train_path):
    train_path = "MURA-v1.1/"
    csv_train_filess = os.path.join("dataloader", train_path,
                                    "train_labeled_studies.csv")
    csv_valid_filess = os.path.join("dataloader", train_path,
                                    "valid_labeled_studies.csv")

    train_df = pd.read_csv(csv_train_filess,
                           names=['img', 'label'],
                           header=None)
    valid_df = pd.read_csv(csv_valid_filess,
                           names=['img', 'label'],
                           header=None)

    # train_data_list=[]
    # train_labels=[]
    # for i in range(len(train_img_paths)):
    #     patient_dir=os.path.join("dataloader", train_img_paths[i])
    #     print("Loading: %s (%d/%d)"%(patient_dir, i, len(train_img_paths)))
    #     for f in glob.glob(patient_dir + "*"):
    #         train_data_patient=[]
    #         train_img=png.read_png_int(f).tolist()
    #         train_data_patient.append(train_img)
    #     train_data_list.extend(train_data_patient)
    #     for _ in range(len(train_data_patient)):
    #         train_labels.append(train_labels_patient[i])

    train_img_paths = train_df.img.values.tolist()
    valid_img_paths = valid_df.img.values.tolist()
    train_labels_patient = train_df.label.values.tolist()
    valid_labels_patient = valid_df.label.values.tolist()
    train_data_list = []
    train_labels = []
    valid_data_list = []
    valid_labels = []

    for i in range(len(valid_img_paths) // 3):
        patient_dir = os.path.join("dataloader", train_img_paths[i])
        print("Loading: %s (%d/%d)" % (patient_dir, i, len(train_img_paths)))
        for f in glob.glob(patient_dir + "*"):
            valid_data_patient = []
            valid_img = png.read_png_int(f)
            valid_img = imresize(valid_img, (64, 64))
            valid_img = np.stack((valid_img, ) * 3, -1)
            valid_data_patient.append(valid_img)
        train_data_list.extend(valid_data_patient)
        for _ in range(len(valid_data_patient)):
            lst = [0, 0]
            lst[train_labels_patient[i]] = 1
            train_labels.append(lst)
    train_data = np.asarray(train_data_list)

    for i in range(len(valid_img_paths) // 3):
        patient_dir = os.path.join("dataloader", valid_img_paths[i])
        print("Loading: %s (%d/%d)" % (patient_dir, i, len(valid_img_paths)))
        for f in glob.glob(patient_dir + "*"):
            valid_data_patient = []
            valid_img = png.read_png_int(f)
            valid_img = imresize(valid_img, (64, 64))
            valid_img = np.stack((valid_img, ) * 3, -1)
            valid_data_patient.append(valid_img)
        valid_data_list.extend(valid_data_patient)
        for _ in range(len(valid_data_patient)):
            lst = [0, 0]
            lst[valid_labels_patient[i]] = 1
            valid_labels.append(lst)
    valid_data = np.asarray(valid_data_list)

    # return train_data, train_labels, valid_data, valid_labels

    return np.array(train_data), np.array(train_labels), np.array(
        valid_data), np.array(valid_labels)
Ejemplo n.º 27
0
 def new_test_func(*args, **kwargs):
     if inspect.ismethod(original):
         result = original.__func__(*args, **kwargs)
     else:
         result = original(*args, **kwargs)
     for baseline_image, test_image, baseline_rcparams, test_rcparams in self._get_baseline_result_pairs(
             test_suite_name, filename, self.extensions):
         # save image
         fig = plt.gcf()
         if fig is not None:
             if self.fig_size is not None:
                 fig.set_size_inches(self.fig_size)
                 fig.set_tight_layout(True)
             fig.savefig(test_image, **self.savefig_kwargs)
             # save rcParams
             with open(test_rcparams, "w") as rcfile:
                 from pprint import pprint
                 rc = matplotlib.rcParams.copy()
                 rc.pop("datapath")  # hide datapath
                 pprint(rc, rcfile)
             import pytest
             if self.is_compare_image and os.path.exists(
                     baseline_image):
                 msg = compare_images(baseline_image,
                                      test_image,
                                      tol=self.tolerance)
                 if msg is not None:
                     msg += "\n"
                     msg += self.compare_rcParam(
                         baseline_rcparams, test_rcparams)
                     # print image in base64
                     # print("====================")
                     # print("Expected Image:")
                     # self._print_image_base64(baseline_image)
                     # print("Actual Image:")
                     # self._print_image_base64(test_image)
                     # print("====================")
                     self.print_image_testing_note(file=sys.stderr)
                     if self.on_compare_fail is not None:
                         self.on_compare_fail()
                     if self.on_fail is not None:
                         self.on_fail()
                     pytest.fail(msg, pytrace=False)
                 else:
                     # clearup the image as they are the same with the baseline
                     os.remove(test_image)
                     os.remove(test_rcparams)
                     if not os.listdir(os.path.dirname(test_image)):
                         os.rmdir(os.path.dirname(test_image))
             else:
                 # checking if the created image is empty
                 verify(test_image)
                 actual_image = _png.read_png_int(test_image)
                 actual_image = actual_image[:, :, :
                                             3]  # remove the alpha channel (if exists)
                 import numpy as np
                 if np.any(actual_image):
                     self.print_image_testing_note(file=sys.stderr)
                     if self.is_compare_image:
                         pytest.skip(
                             "Image file not found for comparison test "
                             "(This is expected for new tests.)\nGenerated Image: "
                             "\n\t{test}".format(test=test_image))
                     else:
                         self._logger.info(
                             "\nGenerated Image: {test}".format(
                                 test=test_image))
                 else:
                     # empty image created
                     if self.on_empty_image is not None:
                         self.on_empty_image()
                     if self.on_fail is not None:
                         self.on_fail()
                     pytest.fail(
                         "Image file not found for comparison test "
                         "(This is expected for new tests.),"
                         " but the new image created is empty.")
     return result
Ejemplo n.º 28
0
def compare_images(expected, actual, tol, in_decorator=False):
    """
    Compare two "image" files checking differences within a tolerance.

    The two given filenames may point to files which are convertible to
    PNG via the `.converter` dictionary. The underlying RMS is calculated
    with the `.calculate_rms` function.

    Parameters
    ----------
    expected : str
        The filename of the expected image.
    actual : str
        The filename of the actual image.
    tol : float
        The tolerance (a color value difference, where 255 is the
        maximal difference).  The test fails if the average pixel
        difference is greater than this value.
    in_decorator : bool
        Determines the output format. If called from image_comparison
        decorator, this should be True. (default=False)

    Returns
    -------
    comparison_result : None or dict or str
        Return *None* if the images are equal within the given tolerance.

        If the images differ, the return value depends on  *in_decorator*.
        If *in_decorator* is true, a dict with the following entries is
        returned:

        - *rms*: The RMS of the image difference.
        - *expected*: The filename of the expected image.
        - *actual*: The filename of the actual image.
        - *diff_image*: The filename of the difference image.
        - *tol*: The comparison tolerance.

        Otherwise, a human-readable multi-line string representation of this
        information is returned.

    Examples
    --------
    ::

        img1 = "./baseline/plot.png"
        img2 = "./output/plot.png"
        compare_images(img1, img2, 0.001)

    """
    from matplotlib import _png

    if not os.path.exists(actual):
        raise Exception("Output image %s does not exist." % actual)

    if os.stat(actual).st_size == 0:
        raise Exception("Output image file %s is empty." % actual)

    # Convert the image to png
    extension = expected.split('.')[-1]

    if not os.path.exists(expected):
        raise IOError('Baseline image %r does not exist.' % expected)

    if extension != 'png':
        actual = convert(actual, False)
        expected = convert(expected, True)

    # open the image files and remove the alpha channel (if it exists)
    expected_image = _png.read_png_int(expected)
    actual_image = _png.read_png_int(actual)
    expected_image = expected_image[:, :, :3]
    actual_image = actual_image[:, :, :3]

    actual_image, expected_image = crop_to_same(
        actual, actual_image, expected, expected_image)

    diff_image = make_test_filename(actual, 'failed-diff')

    if tol <= 0:
        if np.array_equal(expected_image, actual_image):
            return None

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expected_image = expected_image.astype(np.int16)
    actual_image = actual_image.astype(np.int16)

    rms = calculate_rms(expected_image, actual_image)

    if rms <= tol:
        return None

    save_diff_image(expected, actual, diff_image)

    results = dict(rms=rms, expected=str(expected),
                   actual=str(actual), diff=str(diff_image), tol=tol)

    if not in_decorator:
        # Then the results should be a string suitable for stdout.
        template = ['Error: Image files did not match.',
                    'RMS Value: {rms}',
                    'Expected:  \n    {expected}',
                    'Actual:    \n    {actual}',
                    'Difference:\n    {diff}',
                    'Tolerance: \n    {tol}', ]
        results = '\n  '.join([line.format(**results) for line in template])
    return results
Ejemplo n.º 29
0
def compare_images(expected, actual, tol, in_decorator=False):
    """
    Compare two "image" files checking differences within a tolerance.

    The two given filenames may point to files which are convertible to
    PNG via the `.converter` dictionary. The underlying RMS is calculated
    with the `.calculate_rms` function.

    Parameters
    ----------
    expected : str
        The filename of the expected image.
    actual : str
        The filename of the actual image.
    tol : float
        The tolerance (a color value difference, where 255 is the
        maximal difference).  The test fails if the average pixel
        difference is greater than this value.
    in_decorator : bool
        Determines the output format. If called from image_comparison
        decorator, this should be True. (default=False)

    Returns
    -------
    comparison_result : None or dict or str
        Return *None* if the images are equal within the given tolerance.

        If the images differ, the return value depends on  *in_decorator*.
        If *in_decorator* is true, a dict with the following entries is
        returned:

        - *rms*: The RMS of the image difference.
        - *expected*: The filename of the expected image.
        - *actual*: The filename of the actual image.
        - *diff_image*: The filename of the difference image.
        - *tol*: The comparison tolerance.

        Otherwise, a human-readable multi-line string representation of this
        information is returned.

    Examples
    --------
    ::

        img1 = "./baseline/plot.png"
        img2 = "./output/plot.png"
        compare_images(img1, img2, 0.001)

    """
    from matplotlib import _png

    actual = os.fspath(actual)
    if not os.path.exists(actual):
        raise Exception("Output image %s does not exist." % actual)
    if os.stat(actual).st_size == 0:
        raise Exception("Output image file %s is empty." % actual)

    # Convert the image to png
    expected = os.fspath(expected)
    if not os.path.exists(expected):
        raise IOError('Baseline image %r does not exist.' % expected)
    extension = expected.split('.')[-1]
    if extension != 'png':
        actual = convert(actual, cache=False)
        expected = convert(expected, cache=True)

    # open the image files and remove the alpha channel (if it exists)
    with open(expected, "rb") as expected_file:
        expected_image = _png.read_png_int(expected_file)[:, :, :3]
    with open(actual, "rb") as actual_file:
        actual_image = _png.read_png_int(actual_file)[:, :, :3]

    actual_image, expected_image = crop_to_same(
        actual, actual_image, expected, expected_image)

    diff_image = make_test_filename(actual, 'failed-diff')

    if tol <= 0:
        if np.array_equal(expected_image, actual_image):
            return None

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expected_image = expected_image.astype(np.int16)
    actual_image = actual_image.astype(np.int16)

    rms = calculate_rms(expected_image, actual_image)

    if rms <= tol:
        return None

    save_diff_image(expected, actual, diff_image)

    results = dict(rms=rms, expected=str(expected),
                   actual=str(actual), diff=str(diff_image), tol=tol)

    if not in_decorator:
        # Then the results should be a string suitable for stdout.
        template = ['Error: Image files did not match.',
                    'RMS Value: {rms}',
                    'Expected:  \n    {expected}',
                    'Actual:    \n    {actual}',
                    'Difference:\n    {diff}',
                    'Tolerance: \n    {tol}', ]
        results = '\n  '.join([line.format(**results) for line in template])
    return results
Ejemplo n.º 30
0
def compare_images( expected, actual, tol, in_decorator=False ):
   '''Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

   # img1 = "./baseline/plot.png"
   # img2 = "./output/plot.png"
   #
   # compare_images( img1, img2, 0.001 ):

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       The tolerance (a unitless float).  This is used to
               determine the 'fuzziness' to use when comparing images.
   - in_decorator If called from image_comparison decorator, this should be
               True. (default=False)
   '''

   verify(actual)

   # Convert the image to png
   extension = expected.split('.')[-1]
   if extension != 'png':
      actual = convert(actual, False)
      expected = convert(expected, True)

   # open the image files and remove the alpha channel (if it exists)
   expectedImage = _png.read_png_int( expected )
   actualImage = _png.read_png_int( actual )

   actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)

   # compare the resulting image histogram functions
   expected_version = version.LooseVersion("1.6")
   found_version = version.LooseVersion(np.__version__)

   # On Numpy 1.6, we can use bincount with minlength, which is much faster than
   # using histogram
   if found_version >= expected_version:
      rms = 0

      for i in xrange(0, 3):
         h1p = expectedImage[:,:,i]
         h2p = actualImage[:,:,i]

         h1h = np.bincount(h1p.ravel(), minlength=256)
         h2h = np.bincount(h2p.ravel(), minlength=256)

         rms += np.sum(np.power((h1h-h2h), 2))
   else:
      rms = 0
      bins = np.arange(257)

      for i in xrange(0, 3):
         h1p = expectedImage[:,:,i]
         h2p = actualImage[:,:,i]

         h1h = np.histogram(h1p, bins=bins)[0]
         h2h = np.histogram(h2p, bins=bins)[0]

         rms += np.sum(np.power((h1h-h2h), 2))

   rms = np.sqrt(rms / (256 * 3))

   diff_image = make_test_filename(actual, 'failed-diff')

   if ( (rms / 10000.0) <= tol ):
      if os.path.exists(diff_image):
         os.unlink(diff_image)
      return None

   save_diff_image( expected, actual, diff_image )

   if in_decorator:
      results = dict(
         rms = rms,
         expected = str(expected),
         actual = str(actual),
         diff = str(diff_image),
         )
      return results
   else:
      # old-style call from mplTest directory
      msg = "  Error: Image files did not match.\n"       \
            "  RMS Value: " + str( rms / 10000.0 ) + "\n" \
            "  Expected:\n    " + str( expected ) + "\n"  \
            "  Actual:\n    " + str( actual ) + "\n"      \
            "  Difference:\n    " + str( diff_image ) + "\n"      \
            "  Tolerance: " + str( tol ) + "\n"
      return msg
Ejemplo n.º 31
0
def compare_images(expected, actual, tol, in_decorator=False):
    """
    Compare two "image" files checking differences within a tolerance.

    The two given filenames may point to files which are convertible to
    PNG via the `.converter` dictionary. The underlying RMS is calculated
    with the `.calculate_rms` function.

    Parameters
    ----------
    expected : str
        The filename of the expected image.
    actual :str
        The filename of the actual image.
    tol : float
        The tolerance (a color value difference, where 255 is the
        maximal difference).  The test fails if the average pixel
        difference is greater than this value.
    in_decorator : bool
        If called from image_comparison decorator, this should be
        True. (default=False)

    Example
    -------
    img1 = "./baseline/plot.png"
    img2 = "./output/plot.png"
    compare_images( img1, img2, 0.001 ):

    """
    if not os.path.exists(actual):
        msg = "Output image %s does not exist." % actual
        raise Exception(msg)

    if os.stat(actual).st_size == 0:
        msg = "Output image file %s is empty." % actual
        raise Exception(msg)

    verify(actual)

    # Convert the image to png
    extension = expected.split('.')[-1]

    if not os.path.exists(expected):
        raise IOError('Baseline image %r does not exist.' % expected)

    if extension != 'png':
        actual = convert(actual, False)
        expected = convert(expected, True)

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)
    expectedImage = expectedImage[:, :, :3]
    actualImage = actualImage[:, :, :3]

    actualImage, expectedImage = crop_to_same(
        actual, actualImage, expected, expectedImage)

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expectedImage = expectedImage.astype(np.int16)
    actualImage = actualImage.astype(np.int16)

    rms = calculate_rms(expectedImage, actualImage)

    diff_image = make_test_filename(actual, 'failed-diff')

    if rms <= tol:
        if os.path.exists(diff_image):
            os.unlink(diff_image)
        return None

    save_diff_image(expected, actual, diff_image)

    results = dict(rms=rms, expected=str(expected),
                   actual=str(actual), diff=str(diff_image))

    if not in_decorator:
        # Then the results should be a string suitable for stdout.
        template = ['Error: Image files did not match.',
                    'RMS Value: {rms}',
                    'Expected:  \n    {expected}',
                    'Actual:    \n    {actual}',
                    'Difference:\n    {diff}',
                    'Tolerance: \n    {tol}', ]
        results = '\n  '.join([line.format(**results) for line in template])
    return results
Ejemplo n.º 32
0
def mura_preprocess(train_path, study_type):
    #train_path = "MURA-v1.1/"
    csv_train_filess = os.path.join(train_path, "MURA-v1.1/",
                                    "train_{}.csv".format(study_type))
    csv_valid_filess = os.path.join(train_path, "MURA-v1.1/",
                                    "valid_{}.csv".format(study_type))

    train_df = pd.read_csv(csv_train_filess,
                           names=['img', 'count', 'label'],
                           header=None)
    valid_df = pd.read_csv(csv_valid_filess,
                           names=['img', 'count', 'label'],
                           header=None)

    train_img_paths = train_df.img.values.tolist()
    valid_img_paths = valid_df.img.values.tolist()
    train_labels_patient = train_df.label.values.tolist()
    valid_labels_patient = valid_df.label.values.tolist()
    train_data_list = []
    train_labels = []
    valid_data_list = []
    valid_labels = []

    for i in range(len(train_img_paths)):
        patient_dir = os.path.join(train_path, train_img_paths[i])
        msg = "\r Loading: %s (%d/%d)    " % (patient_dir, i + 1,
                                              len(train_img_paths))
        sys.stdout.write(msg)
        sys.stdout.flush()
        train_data_patient = []
        for f in glob.glob(patient_dir + "*"):
            train_img = png.read_png_int(f)
            if len(train_img.shape) == 2:
                train_img = np.stack((train_img, ) * 3, -1)
            train_img = normalize(np.array(train_img))
            # you can replace 256 with other number but any number greater then 256 will exceed the memory limit of 12GB
            train_data_patient.append(train_img)
        train_data_list.extend(train_data_patient)
        for _ in range(len(train_data_patient)):
            lst = [0, 0]
            lst[train_labels_patient[i]] = 1
            #lst = train_labels_patient[i]
            train_labels.append(lst)
    train_data = np.asarray(train_data_list)

    for i in range(len(valid_img_paths)):
        patient_dir = os.path.join(train_path, valid_img_paths[i])
        msg = "\r Loading: %s (%d/%d)     " % (patient_dir, i + 1,
                                               len(valid_img_paths))
        sys.stdout.write(msg)
        sys.stdout.flush()
        valid_data_patient = []
        for f in glob.glob(patient_dir + "*"):
            valid_img = png.read_png_int(f)
            if len(valid_img.shape) == 2:
                valid_img = np.stack((valid_img, ) * 3, -1)
            valid_img = normalize(np.array(valid_img))
            valid_data_patient.append(valid_img)
        valid_data_list.extend(valid_data_patient)
        for _ in range(len(valid_data_patient)):
            lst = [0, 0]
            lst[valid_labels_patient[i]] = 1
            valid_labels.append(lst)
    valid_data = np.asarray(valid_data_list)

    np.save('MURA_{}_train.npy'.format(study_type), np.array(train_data))
    print("【Saving data to MURA_{}_train.npy】".format(study_type))
    np.save('MURA_{}_valid.npy'.format(study_type), np.array(valid_data))
    print("【Saving data to MURA_{}_valid.npy】".format(study_type))
    np.save('MURA_{}_train_lab.npy'.format(study_type), np.array(train_labels))
    np.save('MURA_{}_valid_lab.npy'.format(study_type), np.array(valid_labels))
Ejemplo n.º 33
0
def compare_images(expected, actual, tol, in_decorator=False):
    """
    Compare two "image" files checking differences within a tolerance.

    The two given filenames may point to files which are convertible to
    PNG via the `.converter` dictionary. The underlying RMS is calculated
    with the `.calculate_rms` function.

    Parameters
    ----------
    expected : str
        The filename of the expected image.
    actual :str
        The filename of the actual image.
    tol : float
        The tolerance (a color value difference, where 255 is the
        maximal difference).  The test fails if the average pixel
        difference is greater than this value.
    in_decorator : bool
        If called from image_comparison decorator, this should be
        True. (default=False)

    Examples
    --------
    img1 = "./baseline/plot.png"
    img2 = "./output/plot.png"
    compare_images( img1, img2, 0.001 ):

    """
    if not os.path.exists(actual):
        raise Exception("Output image %s does not exist." % actual)

    if os.stat(actual).st_size == 0:
        raise Exception("Output image file %s is empty." % actual)

    # Convert the image to png
    extension = expected.split('.')[-1]

    if not os.path.exists(expected):
        raise IOError('Baseline image %r does not exist.' % expected)

    if extension != 'png':
        actual = convert(actual, False)
        expected = convert(expected, True)

    # open the image files and remove the alpha channel (if it exists)
    expectedImage = _png.read_png_int(expected)
    actualImage = _png.read_png_int(actual)
    expectedImage = expectedImage[:, :, :3]
    actualImage = actualImage[:, :, :3]

    actualImage, expectedImage = crop_to_same(
        actual, actualImage, expected, expectedImage)

    diff_image = make_test_filename(actual, 'failed-diff')

    if tol <= 0.0:
        if np.array_equal(expectedImage, actualImage):
            return None

    # convert to signed integers, so that the images can be subtracted without
    # overflow
    expectedImage = expectedImage.astype(np.int16)
    actualImage = actualImage.astype(np.int16)

    rms = calculate_rms(expectedImage, actualImage)

    if rms <= tol:
        return None

    save_diff_image(expected, actual, diff_image)

    results = dict(rms=rms, expected=str(expected),
                   actual=str(actual), diff=str(diff_image), tol=tol)

    if not in_decorator:
        # Then the results should be a string suitable for stdout.
        template = ['Error: Image files did not match.',
                    'RMS Value: {rms}',
                    'Expected:  \n    {expected}',
                    'Actual:    \n    {actual}',
                    'Difference:\n    {diff}',
                    'Tolerance: \n    {tol}', ]
        results = '\n  '.join([line.format(**results) for line in template])
    return results
Ejemplo n.º 34
0
        ),
        dtype=np.uint8,
    )
    # Loop over all lines and draw them on the blank image.
    for line in lines:
        for x1, y1, x2, y2 in line:
            cv2.line(line_img, (x1, y1), (x2, y2), color, thickness)
    # Merge the image with the lines onto the original.
    img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)
    # Return the modified image.
    return img


if __name__=="__main__":

    image = png.read_png_int('/Users/junho/PycharmProjects/road_detection/data_road/training/image_2/um_000000.png')
    height, width = image.shape[:2]
    plt.figure()
    plt.imshow(image)

    region_of_interest_vertices = [
        [0, height],
        [width / 3, height/2],
        [width*(2/3), height/2],
        [width, height],
    ]
    #
    # cropped_image = region_of_interest(
    #     image,
    #     [np.array(region_of_interest_vertices, np.int32)],
    # )