Exemplo n.º 1
0
def randomized_scale_Hexarray(Hexarray_s):
    Hexarray_s = test_image_batch(Hexarray_s)

    res_factor = Hexarray_s.shape[1:3]
    res_selector_range = (0.5, 2)

    res_selector = random.uniform(res_selector_range[0], res_selector_range[1])
    res = np.round(res_selector * res_factor)

    Hexarray_s_scaled = scale_Hexarray(Hexarray_s, res, method=0, fill_value=0)

    return Hexarray_s_scaled
Exemplo n.º 2
0
def randomized_rotate_Hexarray(Hexarray_s):
    Hexarray_s = test_image_batch(Hexarray_s)

    angle_factor = 60
    angle_selector_range = (1, 5)

    angle_selector = random.randint(angle_selector_range[0],
                                    angle_selector_range[1])
    angle = angle_selector * angle_factor

    Hexarray_s_rotated = rotate_Hexarray(Hexarray_s, angle)

    return Hexarray_s_rotated
Exemplo n.º 3
0
def translate_Hexarray(Hexarray_s,
                       translation=(1, 1),
                       axis=(1, 2),
                       fill_value=0,
                       cyclic_translation=False):
    Hexarray_s = test_image_batch(Hexarray_s)

    if any(current_translation for current_translation in translation):
        if not cyclic_translation:
            Hexarray_s_translated = np.full_like(Hexarray_s, fill_value)

            Hexarray_s_slice = min(axis) * [slice(None)]
            Hexarray_s_translated_slice = min(axis) * [slice(None)]

            for current_translation in translation:
                if not current_translation:
                    Hexarray_s_slice.append(slice(None))
                    Hexarray_s_translated_slice.append(slice(None))
                elif current_translation < 0:
                    Hexarray_s_slice.append(slice(-current_translation, None))
                    Hexarray_s_translated_slice.append(
                        slice(None, current_translation))
                else:
                    Hexarray_s_slice.append(slice(None, -current_translation))
                    Hexarray_s_translated_slice.append(
                        slice(current_translation, None))

            Hexarray_s_slice += (
                (Hexarray_s.ndim - 1) - max(axis)) * [slice(None)]
            Hexarray_s_translated_slice += (
                (Hexarray_s.ndim - 1) - max(axis)) * [slice(None)]

            Hexarray_s_slice = tuple(Hexarray_s_slice)
            Hexarray_s_translated_slice = tuple(Hexarray_s_translated_slice)

            Hexarray_s_translated[Hexarray_s_translated_slice] = Hexarray_s[
                Hexarray_s_slice]
        else:
            Hexarray_s_translated = np.roll(Hexarray_s,
                                            shift=translation,
                                            axis=axis)
    else:
        Hexarray_s_translated = Hexarray_s

    return Hexarray_s_translated
Exemplo n.º 4
0
def randomized_translate_Hexarray(Hexarray_s):
    Hexarray_s = test_image_batch(Hexarray_s)

    translation_factor = Hexarray_s.shape[1:3]
    translation_selector_range = (-0.5, 0.5)

    translation_selector = np.random.uniform(translation_selector_range[0],
                                             translation_selector_range[1],
                                             size=2)
    translation = np.round(translation_selector * translation_factor)

    Hexarray_s_translated = translate_Hexarray(Hexarray_s,
                                               translation,
                                               axis=(1, 2),
                                               fill_value=0,
                                               cyclic_translation=False)

    return Hexarray_s_translated
Exemplo n.º 5
0
def Hexsamp_h2h(h1_s, h2_s, method):
	h1_s_is_tensor = tf.is_tensor(h1_s)
	h2_s_is_tensor = tf.is_tensor(h2_s)

	if h1_s_is_tensor or h2_s_is_tensor:
		if not h1_s_is_tensor:
			h1_s = tf.convert_to_tensor(h1_s, dtype=tf.float32)

		if not h2_s_is_tensor:
			h2_s = tf.convert_to_tensor(h2_s, dtype=tf.float32)

		s12_is_tensor = True
	else:
		h1_s = test_image_batch(h1_s)
		h2_s = test_image_batch(h2_s)

		s12_is_tensor = False


	h1_s_shape = h1_s.shape
	h2_s_shape = h2_s.shape

	hexarrays   = h1_s_shape[0]
	h1_s_height = h1_s_shape[1]
	h2_s_height = h2_s_shape[1]
	h1_s_width  = h1_s_shape[2]
	h2_s_width  = h2_s_shape[2]
	depth       = h1_s_shape[3]


	h1_s_rad_o  = 1.0
	h1_s_rad_i  = (math.sqrt(3) / 2) * h1_s_rad_o
	h1_s_dia_o  = 2 * h1_s_rad_o
	h1_s_dia_i  = 2 * h1_s_rad_i
	h1_s_dist_w = h1_s_dia_i
	h1_s_dist_h = 1.5 * h1_s_rad_o

	if h1_s_height > 1:
		h1_s_width_hex  = h1_s_width * h1_s_dia_i + h1_s_rad_i
		h1_s_height_hex = h1_s_dia_o + (h1_s_height - 1) * h1_s_dist_h
	else:
		h1_s_width_hex  = h1_s_width * h1_s_dia_i
		h1_s_height_hex = h1_s_dia_o


	if h2_s_height > 1:
		h2_s_rad_i_w = ((h1_s_width_hex - h1_s_dia_i) / (h2_s_width - 0.5)) / 2
		h2_s_rad_i_h = (((h1_s_height_hex - h1_s_rad_o) / (h2_s_height - 0.5)) / 2) / (math.sqrt(3) / 2)
	else:
		h2_s_rad_i_w = (h1_s_width_hex / h2_s_width) / 2
		h2_s_rad_i_h = ((h1_s_height_hex - h1_s_rad_o) / 2) / (math.sqrt(3) / 2)

	h2_s_rad_i = max(h2_s_rad_i_w, h2_s_rad_i_h)

	h2_s_rad_o  = h2_s_rad_i / (math.sqrt(3) / 2)
	h2_s_dia_o  = 2 * h2_s_rad_o
	h2_s_dia_i  = 2 * h2_s_rad_i
	h2_s_dist_w = h2_s_dia_i
	h2_s_dist_h = 1.5 * h2_s_rad_o

	if h2_s_height > 1:
		h2_s_width_hex  = h2_s_width * h2_s_dia_i + h2_s_rad_i
		h2_s_height_hex = h2_s_dia_o + (h2_s_height - 1) * h2_s_dist_h
	else:
		h2_s_width_hex  = h2_s_width * h2_s_dia_i
		h2_s_height_hex = h2_s_dia_o


	if s12_is_tensor:
		h2_s_list   = h2_s_height * [h2_s_width * [None]]
		h2_s_list_h = []




	r  = max(h1_s_dia_i, h2_s_dia_i)
	ri = math.ceil(r)
	wb = ((h1_s_width_hex  - h1_s_dia_i) - (h2_s_width_hex  - h2_s_dia_i)) / 2
	hb = ((h1_s_height_hex - h1_s_dia_o) - (h2_s_height_hex - h2_s_dia_o)) / 2

	for h in range(h2_s_height):
		ht   = hb + h * h2_s_dist_h
		hth  = ht / h1_s_dist_h
		hthi = round(hth)

		for w in range(h2_s_width):
			wt   = wb + w * h2_s_dia_i if not (h    % 2) else wb + h2_s_rad_i + w * h2_s_dia_i
			wth  = wt / h1_s_dia_i     if not (hthi % 2) else -h1_s_rad_i + wt / h1_s_dia_i
			wthi = round(wth)

			if not s12_is_tensor:
				o = np.zeros(shape = (hexarrays, depth))
			else:
				o = tf.zeros(shape = (tf.shape(h2_s)[0], depth))

			on = 0.0


			for y in range(hthi - ri, hthi + ri + 1):
				hh = y * h1_s_dist_h

				for x in range(wthi - ri, wthi + ri + 1):
					wh = x * h1_s_dia_i if not (y % 2) else h1_s_rad_i + x * h1_s_dia_i
					rx = abs(wt - wh)
					ry = abs(ht - hh)

					if x >= 0 and x < h1_s_width and y >= 0 and y < h1_s_height and rx < r and ry < r:
						k = Hexsamp_kernel(rx / r, ry / r, method)

						o  += k * h1_s[:, y, x, :]
						on += k

			if on:
				o /= on


			if not s12_is_tensor:
				h2_s[:, h, w, :] = np.round(np.minimum(o, 255))
			else:
				h2_s_list[h][w] = o


		if s12_is_tensor:
			h2_s_list_h.append(
				tf.stack(
					values = h2_s_list[h],
					axis   = 1,
					name   = 'Hexsamp_h2h_h2_s_list_h_stack'))

	if s12_is_tensor:
		h2_s = tf.stack(
			values = h2_s_list_h,
			axis   = 1,
			name   = 'Hexsamp_h2h_h2_s_stack')


	return h2_s
Exemplo n.º 6
0
def rotate_Hexarray(Hexarray_s, angle=60):
    Hexarray_s = test_image_batch(Hexarray_s)

    Hexarray_h = Hexarray_s.shape[1]
    Hexarray_w = Hexarray_s.shape[2]
    Hexarray_center = (int(Hexarray_h / 2), int(Hexarray_w / 2))
    center_is_even_row = 1 - Hexarray_center[0] % 2

    Hexarray_s_rotated = np.zeros_like(Hexarray_s)

    shift_factor = 1  # Hexarrays start with an even row

    for h in range(Hexarray_h):
        for w in range(Hexarray_w):
            if h == Hexarray_center[0] and w == Hexarray_center[1]:
                Hexarray_s_rotated[:, h, w, :] = Hexarray_s[:, h, w, :]
                continue

            coordinate = (Hexarray_center[0] - h, w - Hexarray_center[1])

            if center_is_even_row:
                if not h % 2:
                    coordinate_hex = (coordinate[0] * 1.5,
                                      coordinate[1] * math.sqrt(3))
                else:
                    coordinate_hex = (coordinate[0] * 1.5,
                                      (coordinate[1] + shift_factor * 0.5) *
                                      math.sqrt(3))
            else:
                if not h % 2:
                    coordinate_hex = (coordinate[0] * 1.5,
                                      (coordinate[1] - shift_factor * 0.5) *
                                      math.sqrt(3))
                else:
                    coordinate_hex = (coordinate[0] * 1.5,
                                      coordinate[1] * math.sqrt(3))

            coordinate_hex_rotated = rotate_Hexint(coordinate_hex, angle)

            coordinate_rotated_row = round_half_up(coordinate_hex_rotated[0] /
                                                   1.5)

            if center_is_even_row:
                if not coordinate_rotated_row % 2:
                    coordinate_rotated = (coordinate_rotated_row,
                                          coordinate_hex_rotated[1] /
                                          math.sqrt(3))
                else:
                    coordinate_rotated = (
                        coordinate_rotated_row,
                        coordinate_hex_rotated[1] / math.sqrt(3) -
                        shift_factor * 0.5)
            else:
                if not coordinate_rotated_row % 2:
                    coordinate_rotated = (coordinate_rotated_row,
                                          coordinate_hex_rotated[1] /
                                          math.sqrt(3))
                else:
                    coordinate_rotated = (
                        coordinate_rotated_row,
                        coordinate_hex_rotated[1] / math.sqrt(3) +
                        shift_factor * 0.5)

            coordinate_rotated = (coordinate_rotated[0],
                                  round_half_up(coordinate_rotated[1]))

            hwr = (Hexarray_h - 1 -
                   (coordinate_rotated[0] + Hexarray_center[0]),
                   coordinate_rotated[1] + Hexarray_center[1])

            if 0 <= hwr[0] < Hexarray_h and 0 <= hwr[1] < Hexarray_w:
                Hexarray_s_rotated[:, hwr[0], hwr[1], :] = Hexarray_s[:, h,
                                                                      w, :]

    return Hexarray_s_rotated
Exemplo n.º 7
0
def scale_Hexarray(Hexarray_s, res=(64, 64), method=0, fill_value=0):
    Hexarray_s = test_image_batch(Hexarray_s)

    Hexarray_s_transformed_shape = (Hexarray_s.shape[0], ) + res + (
        Hexarray_s.shape[3], )
    Hexarray_s_transformed = np.empty_like(Hexarray_s,
                                           shape=Hexarray_s_transformed_shape)

    Hexarray_s_transformed = Hexsamp_h2h(h1_s=Hexarray_s,
                                         h2_s=Hexarray_s_transformed,
                                         method=method)

    print(
        f'Hexarray_s_transformed=\n{np.reshape(Hexarray_s_transformed[0], newshape=res)}'
    )

    if fill_value is not None:
        Hexarray_s_scaled = np.full_like(Hexarray_s, fill_value)
        Hexarray_s_scaled_shape = Hexarray_s_scaled.shape

        Hexarray_s_scaled_h = Hexarray_s_scaled_shape[1]
        Hexarray_s_scaled_w = Hexarray_s_scaled_shape[2]
        Hexarray_s_scaled_center = (int(Hexarray_s_scaled_h / 2),
                                    int(Hexarray_s_scaled_w / 2))

        Hexarray_s_transformed_h = Hexarray_s_transformed_shape[1]
        Hexarray_s_transformed_w = Hexarray_s_transformed_shape[2]
        Hexarray_s_transformed_center = (int(Hexarray_s_transformed_h / 2),
                                         int(Hexarray_s_transformed_w / 2))

        Hexarray_s_scaled_slice_h_start = max(
            0, Hexarray_s_scaled_center[0] - int(Hexarray_s_transformed_h / 2))
        Hexarray_s_scaled_slice_w_start = max(
            0, Hexarray_s_scaled_center[1] - int(Hexarray_s_transformed_w / 2))
        Hexarray_s_scaled_slice_h_stop = min(
            Hexarray_s_scaled_slice_h_start + Hexarray_s_transformed_shape[1],
            Hexarray_s_scaled_h)
        Hexarray_s_scaled_slice_w_stop = min(
            Hexarray_s_scaled_slice_w_start + Hexarray_s_transformed_shape[2],
            Hexarray_s_scaled_w)
        Hexarray_s_scaled_slice_h = slice(Hexarray_s_scaled_slice_h_start,
                                          Hexarray_s_scaled_slice_h_stop)
        Hexarray_s_scaled_slice_w = slice(Hexarray_s_scaled_slice_w_start,
                                          Hexarray_s_scaled_slice_w_stop)
        Hexarray_s_scaled_slice = (slice(None), Hexarray_s_scaled_slice_h,
                                   Hexarray_s_scaled_slice_w, slice(None))

        Hexarray_s_transformed_slice_h_start = max(
            0, Hexarray_s_transformed_center[0] - int(Hexarray_s_scaled_h / 2))
        Hexarray_s_transformed_slice_w_start = max(
            0, Hexarray_s_transformed_center[1] - int(Hexarray_s_scaled_w / 2))
        Hexarray_s_transformed_slice_h_stop = min(
            Hexarray_s_transformed_slice_h_start + Hexarray_s_scaled_shape[1],
            Hexarray_s_transformed_h)
        Hexarray_s_transformed_slice_w_stop = min(
            Hexarray_s_transformed_slice_w_start + Hexarray_s_scaled_shape[2],
            Hexarray_s_transformed_w)
        Hexarray_s_transformed_slice_h = slice(
            Hexarray_s_transformed_slice_h_start,
            Hexarray_s_transformed_slice_h_stop)
        Hexarray_s_transformed_slice_w = slice(
            Hexarray_s_transformed_slice_w_start,
            Hexarray_s_transformed_slice_w_stop)
        Hexarray_s_transformed_slice = (slice(None),
                                        Hexarray_s_transformed_slice_h,
                                        Hexarray_s_transformed_slice_w,
                                        slice(None))

        Hexarray_s_scaled[Hexarray_s_scaled_slice] = Hexarray_s_transformed[
            Hexarray_s_transformed_slice]
    else:
        Hexarray_s_scaled = Hexarray_s_transformed

    return Hexarray_s_scaled