Exemplo n.º 1
0
def square_patches(picture, label, lo=2, hi=15):
    pic = picture.copy()
    height = pic.shape[0]
    width = pic.shape[1]
    number_of_patches = random.randint(lo, hi + 1)

    p_json_list = []

    first_y = -1
    first_x = -1

    r = int(random.uniform(0, 1) * 255)
    g = int(random.uniform(0, 1) * 255)
    b = int(random.uniform(0, 1) * 255)

    for i in range(number_of_patches):
        size = random.randint(2, 5)
        red = check_val(r + random.randint(-30, 30))
        green = check_val(g + random.randint(-30, 30))
        blue = check_val(b + random.randint(-30, 30))
        color = [blue, green, red]
        if first_y < 0:
            first_y = random.randint(int(height * 0.2), int(height * 0.8))
            first_x = random.randint(int(width * 0.2), int(width * 0.8))

            last_y = first_y + size
            last_x = first_x + size

            pic[first_y:(last_y), first_x:(last_x)] = color

            p_shapes = labelMe_class.Shapes(
                label,
                [[first_x, first_y], [last_x, first_y], [last_x, last_y],
                 [first_x, last_y]], None, "polygon", {})
            p_json_list.append(p_shapes)

        else:
            y = first_y + random.randint(-int(height * 0.1), int(height * 0.1))
            x = first_x + random.randint(-int(width * 0.1), int(width * 0.1))

            last_y = y + size
            last_x = x + size

            pic[y:(last_y), x:(last_x)] = color

            p_shapes = labelMe_class.Shapes(
                label, [[x, y], [last_x, y], [last_x, last_y], [x, last_y]],
                None, "polygon", {})
            p_json_list.append(p_shapes)

    res = RetClass(pic, p_json_list)

    return res
Exemplo n.º 2
0
def add_triangles(im, label, lo = 1, hi = 3):
	h, w, _ = im.shape
# 	colors = np.array((
#                    (250,206,135),
#                    (153,255, 255),
#                    (255, 203, 76)),dtype = int) #maybe expand this list of colors

	output = im.copy()
	overlay = im.copy()

	x_0, y_0 = np.random.randint(w), np.random.randint(h)
	x_1, y_1 = np.random.randint(w), np.random.randint(h)
	x_2, y_2 = np.random.randint(w), np.random.randint(h)
	pts = np.array(((x_0, y_0), (x_1, y_1), (x_2, y_2)), dtype=int)

	b_int, g_int, r_int = get_random_color()
	cv2.fillConvexPoly(overlay, pts, color= tuple([b_int, g_int, r_int]) )

	p_json_list = []

	p_shapes = labelMe_class.Shapes(label,  np_array_to_list_int_points(pts), None, "polygon", {})
	p_json_list = [p_shapes]

	num_shapes = np.random.randint(lo, hi + 1)
	alpha = .95
	for i in range(num_shapes):
		x_1, y_1 = np.mean([x_1, x_0]) + np.random.randint(-60,60), np.mean([y_1,y_0])+ np.random.randint(-60,60)
		x_2, y_2 = np.mean([x_2, x_0]) + np.random.randint(-60,60), np.mean([y_2,y_0])+ np.random.randint(-60,60)

		x_1 = min(max(x_1, 0), w-1)
		x_2 = min(max(x_2, 0), w-1)
		y_1 = min(max(y_1, 0), h-1)
		y_1 = min(max(y_2, 0), h-1)

		pts = np.array(((x_0, y_0), (x_1, y_1), (x_2, y_2)), dtype=int)
		# if not is_random:
		# 	cv2.fillConvexPoly(overlay, pts, color= tuple([int(x) for x in colors[np.random.randint(3)]]) )

		b_int, g_int, r_int = get_random_color()
		cv2.fillConvexPoly(overlay, pts, color= tuple([b_int, g_int, r_int]) )

		p_shapes = labelMe_class.Shapes(label, np_array_to_list_int_points(pts), None, "polygon", {})
		p_json_list.append(p_shapes)

	cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)

	res = RetClass(output, p_json_list)
	return res
Exemplo n.º 3
0
def add_random_patches(im, label, lo = 3, hi = 20):
	color = npr.randint(0, 6)
	imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
	ret, thresh = cv2.threshold(imgray, 127, 255, 0)
	contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

	contours.sort(key = len)
	patch_number = np.random.randint(lo,hi+1)
	b_int, g_int, r_int = get_random_color()

	p_json_list = []

	for i in range(patch_number):
		if color == 0:
			cv2.drawContours(im, contours,len(contours) - 1 - i , (0,0,250), -1)
		elif color == 1:
			cv2.drawContours(im, contours,len(contours) - 1 - i , (0,250,0), -1)
		elif color == 2:
			cv2.drawContours(im, contours,len(contours) - 1 - i , (250,0,0), -1)
		else:
			cv2.drawContours(im, contours,len(contours) - 1 - i , (b_int,g_int,r_int), -1)

		contour = contours[len(contours) - 1 - i]

		p_shapes = labelMe_class.Shapes(label, contur_to_list_int_points(contour), None, "polygon", {})
		p_json_list.append(p_shapes)

	res = RetClass(im, p_json_list)
	return res
Exemplo n.º 4
0
def produce_stuttering(image, label):
    vdivisors = divisors(image.shape[0])
    hdivisors = divisors(image.shape[1])
    iterations = np.random.choice(np.arange(1, 5), p=[0.65, 0.2, 0.1, 0.05])

    res = TreeRet(image.copy(), [], [])

    for i in list(range(iterations)):
        sizex = random.choice(hdivisors)
        sizey = random.choice(vdivisors)
        res = stutter(res, label, sizex, sizey)

    r_shapes = labelMe_class.Shapes(
        label, [[0, 0], [image.shape[1] - 1, image.shape[0] - 1]], None,
        "rectangle", {})
    res.r_json.append(r_shapes.to_string_form())

    return res


# img=np.array(cv2.imread(in_name,1))
# vdivisors=divisors(img.shape[0])
# hdivisors=divisors(img.shape[1])
# iterations=np.random.choice(np.arange(1,5),p=[0.65,0.2,0.1,0.05])
# for i in list(range(iterations)):
# 	sizex=random.choice(hdivisors)
# 	sizey=random.choice(vdivisors)
# 	img=stutter(img,sizex,sizey)
# cv2.imwrite(out_name,img)
Exemplo n.º 5
0
def add_vertical_pattern(img, label):
	color = np.random.randint(0,256,size = 3)
	(height, width, channel) = img.shape
	pattern_dist = int(width * 0.01)
	pattern_length = int(height * 0.04)
	segment_length = pattern_length // 8

	row_count = 0
# 	start_row_index = 0
	horizontal_shift = random.randint(0, pattern_dist)

	p_json_list = []


	for x in range(horizontal_shift, width, pattern_dist):
		if row_count % 4 == 0:
			vertical_shift = random.randint(0, pattern_length)

		if np.random.uniform() < 0.75:
			row_count += 1
			continue

		min_x_c = int(x)
		max_x_c = int(x+1)

		min_y_c = 0
		max_y_c = 0

		for y in range(0, height, pattern_length):
			if np.random.uniform() < 0.4:
				continue
			y1 = (vertical_shift + y) % height
			y2 = (vertical_shift + y + segment_length) % height
			y3 = (vertical_shift + y + 2 * segment_length)% height
			y3_step = (vertical_shift + y + 4 * segment_length)% height
			y4 = (vertical_shift + y + 5 * segment_length)% height
			y5 = (vertical_shift + y + 6 * segment_length)% height

			img[y1, x, :] = color
			img[y2, x, :] = color

			max_y_3_f = 0
			if y3_step != 0:
				for y_3_f in range(y3, height, y3_step):
					img[y_3_f, x] = color
					max_y_3_f = int(max(max_y_3_f, y_3_f))
			img[y4, x, :] = color
			img[y5, x, :] = color

			max_y_c = int(max(max_y_c, y1,y2,y3,y4,y5, max_y_3_f))

		row_count += 1

		p_shapes = labelMe_class.Shapes(label, [[min_x_c, min_y_c], [min_x_c, max_y_c],[max_x_c, max_y_c], [max_x_c, min_y_c]], None, "polygon", {})
		p_json_list.append(p_shapes)


	res = RetClass(img, p_json_list)
	return res
Exemplo n.º 6
0
def dotted_lines_radial(picture, label, lo=30, hi=60):
    pic = picture.copy()
    height = pic.shape[0]
    width = pic.shape[1]
    # angle = random.randint(15,345)
    number_of_lines = random.randint(lo, hi)

    x = random.randint(int(0.2 * width), int(0.8 * width))
    y = random.randint(int(0.2 * height), int(0.8 * height))

    r = np.random.randint(0, 256)
    g = np.random.randint(0, 256)
    b = np.random.randint(0, 256)

    angle_step = np.floor(360 / number_of_lines)
    initial_angle = random.randint(-10, 10)

    p_json_list = []

    end_px, end_py = x, y

    for i in np.arange(number_of_lines):

        theta = initial_angle + angle_step * i + random.randint(-5, 5)
        radian = theta / 180 * np.pi
        if np.cos(radian) >= 0:
            hstep = random.choice([4, 5, 3, 6])
        else:
            hstep = random.choice([4, 5, 3, 6]) * -1
        vstep = hstep * np.tan(radian)
        for j in np.arange(random.randint(20, 50)):
            px = int(x + j * hstep)
            py = int(y + j * vstep)
            if px >= 0 and px <= width - 1 and py >= 0 and py <= height - 1:
                u = random.uniform(0, 1)
                if u > 0.9:
                    nx = max(px - 1, 0)
                    pic[py, nx] = [r, g, b]

                if u < 0.1:
                    ny = max(py - 1, 0)
                    pic[ny, px] = [r, g, b]

                pic[py, px] = [r, g, b]
                end_px, end_py = px, py

            else:
                break

        p_shapes = labelMe_class.Shapes(
            label,
            [[x, y], [max(x - 1, 0), max(y - 1, 0)],
             [max(end_px - 1, 0), max(end_py - 1, 0)], [end_px, end_py]], None,
            "polygon", {})
        p_json_list.append(p_shapes)

    res = RetClass(pic, p_json_list)
    return res
Exemplo n.º 7
0
def create_discoloration(image, label):
	img = image.copy()
	threshold = npr.randint(100, 140)
	new_intesity = npr.randint(200, 256)

	min_x = img.shape[1]
	min_y = img.shape[0]

	max_x = 0
	max_y = 0

	color = npr.randint(0, 6)
	if color == 0:
		for y in range(0, img.shape[0]):
			for x in range(0, img.shape[1]):
				if img[y,x][2] > threshold:
					img[y,x] = (0, 0, new_intesity)

					min_x = min(min_x, x)
					max_x = max(max_x, x)

					min_y = min(min_y, y)
					max_y = max(max_y, y)

	elif color == 1:
		for y in range(0, img.shape[0]):
			for x in range(0, img.shape[1]):
				if img[y,x][1] > threshold:
					img[y,x] = (0, new_intesity, 0)

					min_x = min(min_x, x)
					max_x = max(max_x, x)

					min_y = min(min_y, y)
					max_y = max(max_y, y)
	elif color == 2:
		for y in range(0, img.shape[0]):
			for x in range(0, img.shape[1]):
				if img[y,x][0] > threshold:
					img[y,x] = (new_intesity, 0, 0)

					min_x = min(min_x, x)
					max_x = max(max_x, x)

					min_y = min(min_y, y)
					max_y = max(max_y, y)
	else:
		b_int, g_int, r_int = random.randint(new_intesity, 256, size = 3)
		for y in range(0, img.shape[0]):
			for x in range(0, img.shape[1]):
				if img[y,x][0] > threshold:
					img[y,x] = (b_int, g_int, r_int)

	shapes = labelMe_class.Shapes(label, [[min_x, min_y],[max_x, min_y],[max_x, max_y],[min_x, max_y]], None, "polygon", {})
	res = RetClass(img, [shapes])
	return res
Exemplo n.º 8
0
def dotted_lines(picture, label, lo=15, hi=35):
    pic = picture.copy()
    height = pic.shape[0]
    width = pic.shape[1]
    angle = random.randint(15, 345)
    number_of_lines = random.randint(lo, hi + 1)

    ox = random.randint(int(0.2 * width), int(0.8 * width))
    oy = random.randint(int(0.2 * height), int(0.8 * height))

    r = random.randint(0, 256)
    g = random.randint(0, 256)
    b = random.randint(0, 256)

    p_json_list = []

    for i in np.arange(number_of_lines):
        x = ox + random.randint(-int(0.2 * width), int(0.2 * width))
        y = oy + random.randint(-int(0.2 * height), int(0.2 * height))

        theta = angle + random.randint(-20, 20)
        tangent = np.tan(theta / 180 * np.pi)
        hstep = random.choice([-4, 4, -5, 5, -3, 3, -6, 6])
        vstep = hstep * tangent

        end_px, end_py = x, y

        for j in np.arange(random.randint(20, 50)):
            px = int(x + j * hstep)
            py = int(y + j * vstep)

            if px >= 0 and px <= width - 1 and py >= 0 and py <= height - 1:
                u = random.uniform(0, 1)
                if u > 0.9:
                    nx = max(px - 1, 0)
                    pic[py, nx] = [r, g, b]

                if u < 0.1:
                    ny = max(py - 1, 0)
                    pic[ny, px] = [r, g, b]

                pic[py, px] = [r, g, b]
                end_px, end_py = px, py

            else:
                break

        p_shapes = labelMe_class.Shapes(
            label,
            [[x, y], [max(x - 1, 0), max(y - 1, 0)],
             [max(end_px - 1, 0), max(end_py - 1, 0)], [end_px, end_py]], None,
            "polygon", {})
        p_json_list.append(p_shapes)

    res = RetClass(pic, p_json_list)
    return res
Exemplo n.º 9
0
def overlap(img, label, lo=5, hi=10):

    h, w, _ = img.shape
    pic = np.copy(img)

    x0 = random.randint(0, int(w / 6))
    y0 = random.randint(0, int(h / 6))
    x1 = random.randint(int(3 * w / 6), w)
    y1 = random.randint(int(3 * h / 6), h)

    copy = np.copy(img[y0:y1, x0:x1, :])

    count_overlap = random.randint(lo, hi + 1)

    orientation_overlap_x = random.randint(-1, 2)
    orientation_overlap_y = random.randint(-1, 2)

    p_json_list = []

    while (orientation_overlap_x == 0 and orientation_overlap_y == 0):
        orientation_overlap_x = random.randint(-1, 2)
        orientation_overlap_y = random.randint(-1, 2)

    size_x = x1 - x0
    size_y = y1 - y0

    contours = [[[x0, y0], [x1, y0], [x1, y1], [x0, y1]]]

    for i in range(count_overlap):
        offset_x = random.randint(20, 20 + 1)
        offset_y = random.randint(20, 20 + 1)

        x_o_0 = min(max(x0 + offset_x * (i + 1) * orientation_overlap_x, 0),
                    w - 1)
        y_o_0 = min(max(y0 + offset_y * (i + 1) * orientation_overlap_y, 0),
                    h - 1)

        size_o_x = min(max(x_o_0 + size_x, 0), w - 1) - x_o_0
        size_o_y = min(max(y_o_0 + size_y, 0), h - 1) - y_o_0

        pic[y_o_0:y_o_0 + size_o_y,
            x_o_0:x_o_0 + size_o_x, :] = copy[:size_o_y, :size_o_x, :]

        contours.append([[x_o_0, y_o_0], [x_o_0 + size_o_x, y_o_0],
                         [x_o_0 + size_o_x, y_o_0 + size_o_y],
                         [x_o_0, y_o_0 + size_o_y]])

    change_overlap_contours(contours)

    for contour in contours:
        p_shapes = labelMe_class.Shapes(label, contour, None, "polygon", {})
        p_json_list.append(p_shapes)

    res = RetClass(pic, p_json_list)
    return res
Exemplo n.º 10
0
def create_discoloration_new(image, label):
    img = image.copy()
    threshold = random.randint(100, 140)
    new_intesity = random.randint(200, 256)

    imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, im_gray_mask = cv2.threshold(imgray, 255, 255, 0)

    color = random.randint(0, 6)
    if color == 0:
        for y in range(0, img.shape[0]):
            for x in range(0, img.shape[1]):
                if img[y, x][2] > threshold:
                    img[y, x] = (0, 0, new_intesity)
                    im_gray_mask[y, x] = 255

    elif color == 1:
        for y in range(0, img.shape[0]):
            for x in range(0, img.shape[1]):
                if img[y, x][1] > threshold:
                    img[y, x] = (0, new_intesity, 0)
                    im_gray_mask[y, x] = 255

    elif color == 2:
        for y in range(0, img.shape[0]):
            for x in range(0, img.shape[1]):
                if img[y, x][0] > threshold:
                    img[y, x] = (new_intesity, 0, 0)
                    im_gray_mask[y, x] = 255

    else:
        b_int, g_int, r_int = random.randint(200, 256, size=3)

        for y in range(0, img.shape[0]):
            for x in range(0, img.shape[1]):
                if img[y, x][0] > threshold:
                    img[y, x] = (b_int, g_int, r_int)
                    im_gray_mask[y, x] = 255

    contours, hierarchy = cv2.findContours(im_gray_mask, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)

    p_list = []

    for contour in contours:
        p_list.append(
            labelMe_class.Shapes(label, contur_to_list_int_points(contour),
                                 None, "polygon", {}))

    res = RetClass(img, p_list)
    return res
Exemplo n.º 11
0
def create_radiation(img, label):
	height, width, channel = img.shape

	mask_triangles, vertex_triangles, common_vertex = get_triangles(width, height)
	warp_img = radiation(img, vertex_triangles, common_vertex)
	warp_mask = radiation(mask_triangles, vertex_triangles, common_vertex)
	mask_contours, rect_contours = find_contours(warp_mask)

	f_json_list = []

	for rct in rect_contours:
		f_shapes = labelMe_class.Shapes(label, rct, None, "rectangle", {})
		f_json_list.append(f_shapes.to_string_form())

	res = TreeRet(warp_img, f_json_list, None)
	return res
Exemplo n.º 12
0
def mosaics (img, label, lo = 2, hi = 15):
	pic = np.copy(img)

	p_json_list = []

	pts = get_random_contoure(img)

	min_x, min_y, w, h = cv2.boundingRect(pts)
	mos = mosaics_texture_img(w, h, 25, 25, 5, 5, 0.50)

	pic = contuer_filler(pic, pts, mos)

	p_shapes = labelMe_class.Shapes(label, np_array_to_list_int_points(pts), None, "polygon", {})
	p_json_list.append(p_shapes)

	res = RetClass(pic, p_json_list)
	return res
Exemplo n.º 13
0
def color_cast(picture, label, allow_intersections, lo=64, hi=127):
    pic = picture.copy()
    h, w, _ = picture.shape

    chanal = random.randint(0, 7)
    # может 128 ?
    rand_minus_color_value, rand_minus_color_value2 = random.randint(lo,
                                                                     hi + 1,
                                                                     size=2)

    #2/7 портит 1 канал, 1/7 портит 2
    if chanal == 0 or chanal == 1:
        for y in range(0, h):
            for x in range(0, w):
                pic[y, x][0] = check_val(pic[y, x][0] - rand_minus_color_value)

    elif chanal == 2 or chanal == 3:
        for y in range(0, h):
            for x in range(0, w):
                pic[y, x][1] = check_val(pic[y, x][1] - rand_minus_color_value)

    elif chanal == 4 or chanal == 5:
        for y in range(0, h):
            for x in range(0, w):
                pic[y, x][2] = check_val(pic[y, x][2] - rand_minus_color_value)

    else:
        list_chanal = [0, 1, 2]
        chanal1 = random.choice(list_chanal)
        list_chanal.remove(chanal1)
        chanal2 = random.choice(list_chanal)

        for y in range(0, h):
            for x in range(0, w):
                pic[y, x][chanal1] = check_val(pic[y, x][chanal1] -
                                               rand_minus_color_value)
                pic[y, x][chanal2] = check_val(pic[y, x][chanal2] -
                                               rand_minus_color_value2)

    p_shapes = labelMe_class.Shapes(
        label, [[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]], None,
        "polygon", {})

    res = RetClass(pic, [p_shapes])
    return res
Exemplo n.º 14
0
def triangulation(img, label):
	h,w,_ = img.shape
	grid_length = int(np.random.uniform(1.0 / 40, 1.0 / 25) * w)
	half_grid = grid_length // 2

	triangles = []

	for i in range(0,h,grid_length):
		for j in range(0,w,grid_length):
			pt1, pt2 = np.array([i,j]), np.array([i,min(j+ grid_length, w-1)])
			pt3 = np.array([min(i+half_grid, h-1),min(j+half_grid, w-1)])
			pt4, pt5 = np.array([min(i+grid_length,  h-1),j]), np.array([min(i+grid_length, h-1),min(j+grid_length, w-1)])


			pt1 = pt1[[1,0]]
			pt2 = pt2[[1,0]]
			pt3 = pt3[[1,0]]
			pt4 = pt4[[1,0]]
			pt5 = pt5[[1,0]]

			triangles.append(np.array([pt1,pt2,pt3]))
			triangles.append(np.array([pt1,pt4,pt3]))
			triangles.append(np.array([pt5,pt2,pt3]))
			triangles.append(np.array([pt5,pt4,pt3]))

	p_json_list = []

	for t in triangles:
		mid_pt = ((t[0] + t[1] + t[2])/3).astype(int)

		mid_pt = mid_pt[[1,0]]

		color = img[mid_pt[0], mid_pt[1],:]*0.85 + 0.05 * img[t[0,1], t[0,0], :] + 0.05 * img[t[1,1], t[1,0], :] + 0.05 * img[t[2,1], t[2,0], :]
		color = np.uint8(color)
		c = tuple(map(int, color))

		p = cv2.drawContours(img, [t], -1, c, -1)

		p_shapes = labelMe_class.Shapes(label, np_array_to_list_int_points(t), None, "polygon", {})
		p_json_list.append(p_shapes)



	res = RetClass(p, p_json_list)
	return res
Exemplo n.º 15
0
def poligon_to_region_json(p_json, img_shape):
    min_x = img_shape[1] - 1
    min_y = img_shape[0] - 1
    max_x = 0
    max_y = 0

    for el_p_json in p_json:
        for (x, y) in el_p_json.points:
            min_x = min(min_x, x)
            min_y = min(min_y, y)
            max_x = max(max_x, x)
            max_y = max(max_y, y)

    r_shapes = labelMe_class.Shapes(el_p_json.label,
                                    [[min_x, min_y], [max_x, max_y]], None,
                                    "rectangle", {})

    return [r_shapes.to_string_form()]
Exemplo n.º 16
0
def poligon_to_full_json(p_json, img_shape):
    string_f_json = []
    for el_p_json in p_json:
        min_x = img_shape[1] - 1
        min_y = img_shape[0] - 1
        max_x = 0
        max_y = 0

        for (x, y) in el_p_json.points:
            min_x = min(min_x, x)
            min_y = min(min_y, y)
            max_x = max(max_x, x)
            max_y = max(max_y, y)

        f_shapes = labelMe_class.Shapes(el_p_json.label,
                                        [[min_x, min_y], [max_x, max_y]], None,
                                        "rectangle", {})
        string_f_json.append(f_shapes.to_string_form())

    return string_f_json
Exemplo n.º 17
0
def create_desktop_glitch_two(img, label):
    height, width, channel = img.shape
    sub_height = np.random.randint(int(height / 40), int(height / 30))
    sub_width = sub_height

    f_json_list = []

    count = 0
    for i in range(0, height, sub_height):
        generate_pattern = False
        for j in range(0, width, sub_width * 4):
            if count % 4 == 0:
                if np.random.uniform() < 0.05:
                    generate_pattern = True
                else:
                    generate_pattern = False

            if generate_pattern:
                img[i:i + sub_height,
                    j:j + sub_width, :] = form_combined_pattern(
                        img[i:i + sub_height, j:j + sub_width, :])
                if np.random.uniform() < 0.1:
                    generate_pattern = False

                f_shapes = labelMe_class.Shapes(
                    label, [[j, i], [j + sub_width, i + sub_height]], None,
                    "rectangle", {})
                f_json_list.append(f_shapes.to_string_form())
            count += 1

    #r_shapes = labelMe_class.Shapes(label, [[y0, x0], [y1, x1]], None, "rectangle", {})
    #r_json_list = [r_shapes.to_string_form()]

    #r_shapes2 = labelMe_class.Shapes("2", [[sub_orig_y, sub_orig_x], [sub_orig_y + subwidth, sub_orig_x + subheight]], None, "rectangle", {})
    #r_json_list.append(r_shapes2.to_string_form())
    #res = TreeRet(orig_img, f_json_list, r_json_list)

    res = TreeRet(img, f_json_list, None)
    return res
Exemplo n.º 18
0
def create_desktop_glitch_one(orig_img, label):
    height, width, _ = orig_img.shape
    x0 = npr.randint(0, int(height / 6))
    y0 = npr.randint(0, int(width / 6))
    x1 = npr.randint(int(5 * height / 6), height)
    y1 = npr.randint(int(5 * width / 6), width)

    copy = np.copy(orig_img[x0:x1, y0:y1, :])

    square_width = np.random.randint(50, 80)
    img = np.empty_like(copy)
    height, width, channel = img.shape
    img = add_glitch(img, square_width)

    subwidth = int(width / 3)
    subheight = int(height / 3)

    sub_orig_x = np.random.randint(int(subheight / 2), int(subheight * 3 / 2))
    sub_orig_y = np.random.randint(0, subwidth)

    added_patch = add_glitch(
        img[sub_orig_x:sub_orig_x + subheight,
            sub_orig_y:sub_orig_y + subwidth, :], square_width)
    img[sub_orig_x:sub_orig_x + subheight,
        sub_orig_y:sub_orig_y + subwidth, :] = np.clip(added_patch, 0, 255)
    img = merge(copy, img, square_width)

    orig_img[x0:x1, y0:y1, :] = img

    r_shapes = labelMe_class.Shapes(label, [[y0, x0], [y1, x1]], None,
                                    "rectangle", {})
    r_json_list = [r_shapes.to_string_form()]

    #res = TreeRet(orig_img, f_json_list, r_json_list)

    res = TreeRet(orig_img, None, r_json_list)
    return res
Exemplo n.º 19
0
def stutter(res, label, sizex, sizey):

    vstripes = int(res.img.shape[1] / sizex)
    hstripes = int(res.img.shape[0] / sizey)

    for k in list(range(0, res.img.shape[1], 2 * sizex)):
        for i in list(range(sizex)):
            for j in list(range(res.img.shape[0])):
                if (i + k + sizex < res.img.shape[1]):
                    swap(res.img, [j, i + k], [j, sizex + i + k])

        first_y = 0
        first_x_1 = k
        first_x_2 = sizex + k

        is_stop_1_2 = False
        if (first_x_1 + sizex < res.img.shape[1]):
            last_x_1 = first_x_1 + sizex
        else:
            last_x_1 = res.img.shape[1] - 1
            is_stop_2 = True

        if (first_x_2 + sizex < res.img.shape[1]):
            last_x_2 = first_x_2 + sizex
        else:
            last_x_2 = res.img.shape[1] - 1

        last_y = res.img.shape[0] - 1

        f_shapes_1 = labelMe_class.Shapes(
            label, [[first_x_1, first_y], [last_x_1, last_y]], None,
            "rectangle", {})
        res.f_json.append(f_shapes_1.to_string_form())

        if not is_stop_1_2:
            f_shapes_2 = labelMe_class.Shapes(
                label, [[first_x_2, first_y], [last_x_2, last_y]], None,
                "rectangle", {})
            res.f_json.append(f_shapes_2.to_string_form())

    for k in list(range(0, res.img.shape[0], 2 * sizey)):
        for i in list(range(sizey)):
            for j in list(range(res.img.shape[1])):
                if (i + k + sizey < res.img.shape[0]):
                    swap(res.img, [i + k, j], [i + k + sizey, j])

        first_y_1 = k
        first_y_2 = k + sizey
        first_x = 0

        is_stop_2_2 = False

        if (first_y_1 + sizey < res.img.shape[0]):
            last_y_1 = first_y_1 + sizey
        else:
            last_y_1 = res.img.shape[0] - 1
            is_stop_2 = True

        if (first_y_2 + sizey < res.img.shape[0]):
            last_y_2 = first_y_2 + sizey
        else:
            last_y_2 = res.img.shape[0] - 1

        last_x = res.img.shape[1] - 1

        f_shapes_1 = labelMe_class.Shapes(
            label, [[first_x, first_y_1], [last_x, last_y_1]], None,
            "rectangle", {})
        res.f_json.append(f_shapes_1.to_string_form())

        if not is_stop_2_2:
            f_shapes_2 = labelMe_class.Shapes(
                label, [[first_x, first_y_2], [last_x, last_y_2]], None,
                "rectangle", {})
            res.f_json.append(f_shapes_2.to_string_form())

    return res
Exemplo n.º 20
0
def parallel_lines(picture, label, lo=60, hi=100):
    pic = picture.copy()
    height = pic.shape[0]
    width = pic.shape[1]
    number_of_lines = np.random.randint(lo, hi + 1)
    theta = np.random.randint(10, 35)
    angle = np.tan(theta / 180 * np.pi)
    u = np.random.uniform(0, 1)
    sign = random.choice([1, -1])

    p_json_list = []

    while number_of_lines > 0:
        x1 = random.randint(int(0.3 * width), int(0.6 * width))
        y1 = random.randint(int(0.2 * height), int(0.8 * height))

        if u < 0.5:
            x2 = 0
            y2 = int(y1 + sign * int(x1 * angle))
            if y2 >= height or y2 < 0:
                continue
        else:
            x2 = width - 1
            y2 = int(y1 + sign * int((width - x1 - 1) * angle))
            if y2 >= height or y2 < 0:
                continue

        lineThickness = random.randint(1, 3)
        colors = pic[y1, x1].astype(float)
        cv2.line(pic, (x1, y1), (x2, y2), colors, lineThickness)

        if x1 > x2:
            tx = x1
            x1 = x2
            x2 = tx

            ty = y1
            y1 = y2
            y2 = ty

        point_l_x = max(x1 - lineThickness // 2, 0)
        point_r_x = min(x2 + lineThickness // 2, width - 1)

        point_l_1_y = max(y1 - lineThickness // 2, 0)
        point_l_2_y = min(y1 + lineThickness // 2, height - 1)

        point_r_1_y = max(y2 - lineThickness // 2, 0)
        point_r_2_y = min(y2 + lineThickness // 2, height - 1)

        point_l_1 = (point_l_x, point_l_1_y)
        point_l_2 = (point_l_x, point_l_2_y)
        point_r_1 = (point_r_x, point_r_1_y)
        point_r_2 = (point_r_x, point_r_2_y)

        p_shapes = labelMe_class.Shapes(
            label, [point_l_1, point_l_2, point_r_2, point_r_1], None,
            "polygon", {})

        p_json_list.append(p_shapes)

        number_of_lines -= 1

    res = RetClass(pic, p_json_list)

    return res
Exemplo n.º 21
0
def blurring(img, label):
	blur = cv2.bilateralFilter(img, 40, 100, 100)

	shapes = labelMe_class.Shapes(label, [[0, 0],[0, blur.shape[0]-1],[blur.shape[1]-1, blur.shape[0]-1],[blur.shape[1]-1, 0]], None, "polygon", {})
	res = RetClass(blur, [shapes])
	return res
Exemplo n.º 22
0
def add_shaders(im, label, lo = 1, hi = 3):
	angles = np.array([0,90,180,270])

	h,w,_ = im.shape
	output = im.copy()
	overlay1 = im.copy()
	overlay2 = im.copy()

	#####big shaders in forms of n-gons
	num_shapes = np.random.randint(lo,hi+1)

	f_json_list = []
	max_x = 0
	max_y = 0
	min_x = w
	min_y = h

	for i in range(num_shapes):
		x_0, y_0 = np.random.randint(w), np.random.randint(h)
		x_1, y_1 = np.random.randint(-300,w+300), np.random.randint(-300,h+300)
		x_2, y_2 = np.random.randint(-300,w+300), np.random.randint(-300,h+300)

		pts = np.array(((x_0, y_0), (x_1, y_1), (x_2, y_2)), dtype=int)

		temp_max_x = max(x_0,x_1,x_2)
		temp_max_y = max(y_0,y_1,y_2)
		temp_min_x = min(x_0,x_1,x_2)
		temp_min_y = min(y_0,y_1,y_2)

		extra_n = np.random.randint(4)

		for i in range(extra_n): #extra number of points to make an n_gon
			temp_ran_x = np.random.randint(-300,h+300)
			temp_ran_y = np.random.randint(-300,w+300)
			pts = np.append(pts, [[temp_ran_x, temp_ran_y]], axis = 0)


		contur = contntur_limitation(pts, 0,0, w-1,h-1)

		(t_x1, t_y1) = contur[0]
		t_x1 = int(math.ceil(t_x1))
		t_y1 = int(math.ceil(t_y1))
		temp_min_x = t_x1
		temp_min_y = t_y1

		temp_max_x = t_x1
		temp_max_y = t_y1

		for (t_x,t_y) in contur:
			t_x = int(math.ceil(t_x))
			t_y = int(math.ceil(t_y))

			temp_min_x = min(t_x,temp_min_x)
			temp_min_y = min(t_y,temp_min_y)

			temp_max_x = max(t_x,temp_max_x)
			temp_max_y = max(t_y,temp_max_y)

		f_shapes = labelMe_class.Shapes(label, [[temp_min_x, temp_min_y], [temp_max_x, temp_max_y]], None, "rectangle", {})
		f_json_list.append(f_shapes.to_string_form())

		min_x = min(min_x, temp_min_x)
		max_x = max(max_x, temp_max_x)

		min_y = min(min_y, temp_min_y)
		max_y = max(max_y, temp_max_y)

		alpha = 1

		colors = np.empty([2, 3])
		start_x = min(max(0, x_0), h-1)
		start_y = min(max(0, y_0), w-1)

		colors[0, :] = im[start_x, start_y,:] + npr.randint(-30, 30, size = [3])
		mid_x = (x_1+x_2)//2
		mid_y = (y_1+y_2)//2

		mid_x = min(max(0, mid_x), h-1)
		mid_y = min(max(0, mid_y), w-1)

		colors[1, :] = im[mid_x,mid_y,:] + npr.randint(-30, 30, size = [3])

		colors = np.clip(colors, a_min = 0, a_max = 255)


		#f_shapes = labelMe_class.Shapes("2", [[start_x, start_y], [mid_x, mid_y]], None, "rectangle", {})
		#f_json_list.append(f_shapes.to_string_form())

		# colors[0,:] = npr.randint(0, 256, size = 3)
		# colors[1,:] = colors[0,:] + npr.randint(0, 100, size = 3)
		# colors[1,:] = np.clip(colors[1,:], 0, 255)


		cv2.fillConvexPoly(overlay1, pts, color= tuple([int(x) for x in colors[0]]) )
		cv2.fillConvexPoly(overlay2, pts, color= tuple([int(x) for x in colors[1]]) )


	############
	a1, a2 = random.choice(angles), random.choice(angles)

	t_img = gradient(output, color_blend(im, overlay1, overlay2, a1), a2)

	r_shapes = labelMe_class.Shapes(label, [[min_x, min_y], [max_x, max_y]], None, "rectangle", {})
	r_json_list = [r_shapes.to_string_form()]

	res = TreeRet(t_img, f_json_list, r_json_list)
	return res
Exemplo n.º 23
0
def line_pixelation(img, label, label2="2"):

    f_json_list = []
    max_x = 0
    max_y = 0
    min_x = img.shape[1]
    min_y = img.shape[0]

    max_x_p = 0
    max_y_p = 0
    min_x_p = img.shape[1]
    min_y_p = img.shape[0]

    while (True):
        vertical = 0
        horizontal = 1

        # PARAMETERS
        skipstripe = random.randrange(0, 2)
        orientation = random.randrange(0, 2)
        brightness = random.randrange(0, 2)
        monobias = random.randrange(0, 3)
        biasval = random.randrange(0, 11)
        glow = random.randrange(0, 8)

        image = np.copy(img)
        height = max(abs(int(np.random.normal(int(image.shape[0] / 200), 2))),
                     1)
        width = max(abs(int(np.random.normal(height, 2))), 1)
        if (orientation == vertical):
            if (width < image.shape[1]):
                indent = random.randrange(0, image.shape[1] - width)
            else:
                print("Error: 'width' is too large for input dimensions. ")
                continue
        if (orientation == horizontal):
            if (height < image.shape[0]):
                indent = random.randrange(0, image.shape[0] - height)
            else:
                print("Error: 'height' is too large for input dimensions.")
                continue
        stripes = random.randrange(
            1, max(1 + abs(int(np.random.normal(20, 20))), 2))
        ss = np.ones(stripes)
        if (skipstripe == 1):
            ss = [1]
            for i in list(range(stripes - 2)):
                ss.append(random.randrange(0, 2))
            ss.append(1)
        if (monobias == 1):
            monocolor = [0, 0, 0]
        if (monobias == 2):
            monocolor = [255, 255, 255]

        if (orientation == vertical):
            for n in list(range(stripes)):
                if (ss[n] == 1):

                    temp_min_x = img.shape[1] - 1
                    temp_min_y = img.shape[0] - 1
                    temp_max_x = 0
                    temp_max_y = 0

                    temp_min_x_p = img.shape[1] - 1
                    temp_min_y_p = img.shape[0] - 1
                    temp_max_x_p = 0
                    temp_max_y_p = 0

                    for i in list(range(0, image.shape[0], height)):

                        color = np.array([
                            random.randrange(0, 256),
                            random.randrange(0, 256),
                            random.randrange(0, 256)
                        ])
                        mono = 0
                        if (monobias > 0):
                            mono = random.randrange(1, 11)
                        if (glow == 6 and n == 0
                                and random.randrange(1, 10) < random.randrange(
                                    1, 3)):
                            radius = random.randrange(5, 4 + 4 * height)
                            y = i * height + int(height / 2)
                            if (y - radius < image.shape[0] - 10):
                                image = vlglow(image, [y, indent], color,
                                               radius)

                                temp_min_x_p = min(max(indent - radius, 0),
                                                   img.shape[1] - 1)
                                temp_min_y_p = min(max(y - radius, 0),
                                                   img.shape[0] - 1)

                                temp_max_x_p = min(indent, img.shape[1] - 1)
                                temp_max_y_p = min(y + radius,
                                                   img.shape[0] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        if (glow == 7 and n == (len(ss) - 1)
                                and random.randrange(1, 10) < random.randrange(
                                    1, 4)):
                            radius = random.randrange(5, 70)
                            y = i * height + int(height / 2)
                            if (y - radius < image.shape[0] - 10):
                                image = vrglow(image, [y, indent + n * width],
                                               color, radius)

                                temp_min_x_p = min(max(indent + n * width, 0),
                                                   img.shape[1] - 1)
                                temp_min_y_p = min(max(y - radius, 0),
                                                   img.shape[0] - 1)

                                temp_max_x_p = min(
                                    indent + n * width + 1 + radius,
                                    img.shape[1] - 1)
                                temp_max_y_p = min(y + radius,
                                                   img.shape[0] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        for j in list(range(height)):
                            for k in list(range(width)):
                                localcolor = np.array(color)
                                if (((i + j) < image.shape[0]) and
                                    (indent + k + n * width < image.shape[1])):
                                    if (brightness == 1 and mono <= biasval):
                                        seed = int(np.random.normal(0, 10))
                                        localcolor[0] = max(
                                            min(color[0] + seed, 255), 0)
                                        localcolor[1] = max(
                                            min(color[1] + seed, 255), 0)
                                        localcolor[2] = max(
                                            min(color[2] + seed, 255), 0)
                                    elif (mono > biasval):
                                        localcolor = monocolor
                                    image[i + j, indent +
                                          (k + n * width)] = localcolor

                                    temp_min_x = min(indent + (k + n * width),
                                                     temp_min_x)
                                    temp_min_y = min(i + j, temp_min_y)

                                    temp_max_x = min(
                                        max(indent + (k + n * width) + 1,
                                            temp_max_x), img.shape[1] - 1)
                                    temp_max_y = min(
                                        max(i + j, temp_max_y) + 1,
                                        img.shape[0] - 1)

                    if (temp_min_x != 0 or temp_min_y != 0
                            or temp_max_x != img.shape[1] - 1
                            or temp_max_y != img.shape[0] - 1):

                        f_shapes = labelMe_class.Shapes(
                            label, [[temp_min_x, temp_min_y],
                                    [temp_max_x, temp_max_y]], None,
                            "rectangle", {})
                        f_json_list.append(f_shapes.to_string_form())

                        min_x = min(min_x, temp_min_x)
                        max_x = max(max_x, temp_max_x)

                        min_y = min(min_y, temp_min_y)
                        max_y = max(max_y, temp_max_y)

        if (orientation == horizontal):
            for n in list(range(stripes)):
                if (ss[n] == 1):
                    temp_min_x = img.shape[1] - 1
                    temp_min_y = img.shape[0] - 1
                    temp_max_x = 0
                    temp_max_y = 0

                    temp_min_x_p = img.shape[1] - 1
                    temp_min_y_p = img.shape[0] - 1
                    temp_max_x_p = 0
                    temp_max_y_p = 0

                    for i in list(range(0, image.shape[1], width)):
                        color = np.array([
                            random.randrange(0, 256),
                            random.randrange(0, 256),
                            random.randrange(0, 256)
                        ])
                        mono = 0
                        if (monobias > 0):
                            mono = random.randrange(1, 11)
                        if (glow == 6 and n == 0
                                and random.randrange(1, 10) < random.randrange(
                                    1, 3)):
                            x = i * width + int(width / 2)
                            radius = random.randrange(5, 4 + 4 * width)
                            if (x - radius < img.shape[1] - 10):
                                image = htglow(image, [indent, x], color,
                                               radius)

                                temp_min_y_p = min(max(indent - radius, 0),
                                                   img.shape[0] - 1)
                                temp_min_x_p = min(max(x - radius, 0),
                                                   img.shape[1] - 1)

                                temp_max_y_p = min(indent, img.shape[0] - 1)
                                temp_max_x_p = min(x + radius,
                                                   img.shape[1] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        if (glow == 7 and n == (len(ss) - 1)
                                and random.randrange(1, 10) < random.randrange(
                                    1, 4)):
                            radius = random.randrange(5, 70)
                            x = i * width + int(width / 2)
                            if (x - radius < img.shape[1] - 10):
                                image = hbglow(image, [indent + height * n, x],
                                               color, radius)

                                temp_min_y_p = min(max(indent + height * n, 0),
                                                   img.shape[0] - 1)
                                temp_min_x_p = min(max(x - radius, 0),
                                                   img.shape[1] - 1)

                                temp_max_y_p = min(
                                    indent + height * n + radius,
                                    img.shape[0] - 1)
                                temp_max_x_p = min(x + radius,
                                                   img.shape[1] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        for j in list(range(width)):
                            for k in list(range(height)):
                                localcolor = np.array(color)
                                if ((
                                    (k + n * height + indent) < image.shape[0])
                                        and (i + j < image.shape[1])):
                                    if (brightness == 1 and mono <= biasval):
                                        seed = int(np.random.normal(0, 10))
                                        localcolor[0] = max(
                                            min(color[0] + seed, 255), 0)
                                        localcolor[1] = max(
                                            min(color[1] + seed, 255), 0)
                                        localcolor[2] = max(
                                            min(color[2] + seed, 255), 0)
                                    elif (mono > biasval):
                                        localcolor = monocolor
                                    image[indent + k + (n * height),
                                          i + j] = localcolor

                                    temp_min_y = min(indent + k + (n * height),
                                                     temp_min_y)
                                    temp_min_x = min(i + j, temp_min_x)

                                    temp_max_y = min(
                                        max(indent + k + (n * height) + 1,
                                            temp_max_y), img.shape[0] - 1)
                                    temp_max_x = min(max(i + j, temp_max_x),
                                                     img.shape[1] - 1)

                    if (temp_min_x != 0 or temp_min_y != 0
                            or temp_max_x != img.shape[1] - 1
                            or temp_max_y != img.shape[0] - 1):

                        f_shapes = labelMe_class.Shapes(
                            label, [[temp_min_x, temp_min_y],
                                    [temp_max_x, temp_max_y]], None,
                            "rectangle", {})
                        f_json_list.append(f_shapes.to_string_form())

                        min_x = min(min_x, temp_min_x)
                        max_x = max(max_x, temp_max_x)

                        min_y = min(min_y, temp_min_y)
                        max_y = max(max_y, temp_max_y)

        if (not np.array_equal(img, image)):
            r_shapes = labelMe_class.Shapes(label,
                                            [[min_x, min_y], [max_x, max_y]],
                                            None, "rectangle", {})
            r_json_list = [r_shapes.to_string_form()]
            if (max_x_p != 0 or max_y_p != 0 or min_x_p != img.shape[1] - 1
                    or min_y_p != img.shape[0] - 1):
                r_shapes = labelMe_class.Shapes(
                    label2, [[min_x_p, min_y_p], [max_x_p, max_y_p]], None,
                    "rectangle", {})
                r_json_list.append(r_shapes.to_string_form())
            res = TreeRet(image, f_json_list, r_json_list)
            return res
Exemplo n.º 24
0
def white_square(picture,
                 label,
                 allow_intersections,
                 fill_percentage,
                 lo=2,
                 hi=15):
    height = picture.shape[0]
    width = picture.shape[1]
    number_of_patches = random.randint(lo, hi + 1)

    overlay = picture.copy()
    pic = picture.copy()
    p_json_list = []

    list_coordinate_rectangle = []
    count_fail = 0

    for i in range(number_of_patches):

        (red, green, blue) = random.randint(240, 256,
                                            size=3)  #верхняя граница не входит
        red = int(red)
        green = int(green)
        blue = int(blue)

        color = [blue, green, red]

        is_intersection = True

        count = 0
        while (is_intersection):
            forfeit = min((600 / 10000 * count), 300)
            forfeit2 = (30 / 10000 * count)

            (first_x, first_y) = random.random(2)
            (size_x, size_y) = random.random(2)

            size_x = to_int(size_x * (300 - forfeit) + 80 - forfeit2)
            size_y = to_int(size_y * (300 - forfeit) + 80 - forfeit2)

            first_y = to_int(
                first_y * (height * 0.7) +
                height * 0.1)  #(int(height* 0.1), int(height*0.8))
            first_x = to_int(first_x * (width * 0.7) + width * 0.1)

            last_y = min(first_y + size_y, height - 1)
            last_x = min(first_x + size_x, width - 1)

            #на случай, если фигура не вписывается в картинку
            size_x = last_x - first_x
            size_y = last_y - first_y

            if (size_x > 2 * size_y or size_y > 2 * size_x):
                #print ("cont")
                continue

            count += 1

            if allow_intersections:
                break
            if (count < 10000):
                is_intersection = intersection_check(list_coordinate_rectangle,
                                                     [first_x, first_y],
                                                     [last_x, last_y])
            else:
                is_intersection = False
                count_fail += 1

        list_coordinate_rectangle.append([[first_x, first_y], [last_x,
                                                               last_y]])

        orientation = random.randint(0, 2)

        if orientation == 0:
            x_top = random.randint(first_x, to_int(last_x - 0.5 * size_x))
            y_left = random.randint(first_y, to_int(last_y - 0.5 * size_y))
            x_down = min(
                last_x + first_x - x_top + random.randint(
                    -to_int(size_x * 0.1) - 1, to_int(size_x * 0.1)), last_x)
            y_right = min(
                last_y + first_y - y_left + random.randint(
                    -to_int(size_y * 0.1) - 1, to_int(size_y * 0.1)), last_y)
        else:
            x_top = random.randint(to_int(first_x + 0.5 * size_x), last_x)
            y_left = random.randint(to_int(first_y + 0.5 * size_y), last_y)
            x_down = max(
                last_x + first_x - x_top +
                random.randint(-to_int(size_x * 0.1),
                               to_int(size_x * 0.1) + 1), first_x)
            y_right = max(
                last_y + first_y - y_left +
                random.randint(-to_int(size_y * 0.1),
                               to_int(size_y * 0.1) + 1), first_y)

        pts = np.array(((x_top, last_y), (last_x, y_right), (x_down, first_y),
                        (first_x, y_left)),
                       dtype=int)
        cv2.fillConvexPoly(overlay, pts, color)

        ##############################################################################
        intensity_blur_in_contoure(overlay, pts, -5, 5)

        add_spots(overlay, pts, fill_percentage)

        p_shapes = labelMe_class.Shapes(label,
                                        np_array_to_list_int_points(pts), None,
                                        "polygon", {})
        p_json_list.append(p_shapes)

    if not count_fail == 0:
        print(count_fail)

    alpha = 1
    cv2.addWeighted(overlay, alpha, pic, 1 - alpha, 0, pic)

    res = RetClass(pic, p_json_list)
    return res
Exemplo n.º 25
0
def add_shapes(im, label, lo = 2, hi = 5):
	img = im.copy()
	h, w, _ = img.shape

	# Find the darkest region of the image
	grid = (-1,-1)
	mean_shade = np.mean(img)

	x_step, y_step = int(w/6), int(h/4)
	for y in range(0, h, y_step):
		for x in range(0, w, x_step):
			new_shade = np.mean(img[y:y+y_step, x:x+x_step])
			if  new_shade <= mean_shade:
				mean_shade = new_shade
				grid = (x,y)

	f_json_list = []
	max_x = 0
	max_y = 0
	min_x = w
	min_y = h

	# Add shapes
	minLoc = (np.random.randint(grid[0], min(grid[0]+x_step, w)), np.random.randint(grid[1], min(grid[1]+x_step, h)))
	#minLoc = (np.random.randint(0.1 * w, 0.9 * w), np.random.randint(0.1 * h, 0.9 * h))
	num_shapes = np.random.randint(lo,hi+1)

	for i in range(num_shapes):
		stretch = np.random.randint(40, 100)
		diff1, diff2 = np.random.randint(-5,5), np.random.randint(-5,5)
		x1 = minLoc[0] + diff1 * stretch
		y1 = minLoc[1] + diff2 * stretch
		x2 = x1 + np.random.randint(1,12)/5 * diff1 * stretch
		y2 = y1 + np.random.randint(1,12)/5 * diff2 * stretch

		pts = np.array((minLoc, (x1, y1), (x2, y2)), dtype=int)
		contur = contntur_limitation([minLoc, [x1,y1], [x2,y2]]	, 0,0, w-1,h-1)

		(t_x1, t_y1) = contur[0]
		t_x1 = int(math.ceil(t_x1))
		t_y1 = int(math.ceil(t_y1))
		temp_min_x = t_x1
		temp_min_y = t_y1

		temp_max_x = t_x1
		temp_max_y = t_y1

		for (t_x,t_y) in contur:
			t_x = int(math.ceil(t_x))
			t_y = int(math.ceil(t_y))

			temp_min_x = min(t_x,temp_min_x)
			temp_min_y = min(t_y,temp_min_y)

			temp_max_x = max(t_x,temp_max_x)
			temp_max_y = max(t_y,temp_max_y)

		f_shapes = labelMe_class.Shapes(label, [[temp_min_x, temp_min_y], [temp_max_x, temp_max_y]], None, "rectangle", {})
		f_json_list.append(f_shapes.to_string_form())

		min_x = min(min_x, temp_min_x)
		max_x = max(max_x, temp_max_x)

		min_y = min(min_y, temp_min_y)
		max_y = max(max_y, temp_max_y)

		c1, c2, c3 = np.random.randint(0,50),np.random.randint(0,50),np.random.randint(0,50)
		cv2.fillConvexPoly(img, pts, color= (c1,c2,c3))

	r_shapes = labelMe_class.Shapes(label, [[min_x, min_y], [max_x, max_y]], None, "rectangle", {})
	r_json_list = [r_shapes.to_string_form()]

	res = TreeRet(img, f_json_list, r_json_list)
	return res
Exemplo n.º 26
0
def black_tree(picture, label, allow_intersections, lo=2, hi=15):
    height = picture.shape[0]
    width = picture.shape[1]

    number_of_patches = random.randint(lo, hi + 1)

    overlay = picture.copy()
    pic = picture.copy()
    p_json_list = []

    list_coordinate_rectangle = []

    count_fail = 0
    for i in range(number_of_patches):

        (red, green, blue) = random.randint(0, 21,
                                            size=3)  #верхняя граница не входит
        red = int(red)
        green = int(green)
        blue = int(blue)
        color = [blue, green, red]

        is_intersection = True

        count = 0
        while (is_intersection):
            forfeit = min((300 / 10000 * count), 100)
            forfeit2 = (20 / 10000 * count)

            (first_y, first_x) = random.random(2)
            (size_x, size_y) = random.random(2)

            size_y = to_int(size_y * (150 - forfeit) + 40 - forfeit2)
            size_x = to_int(size_x * (100 - forfeit) + 40 - forfeit2)

            first_y = to_int(
                first_y * (height * 0.7) +
                height * 0.1)  #(int(height* 0.1), int(height*0.8))
            first_x = to_int(first_x * (width * 0.7) + width * 0.1)

            last_y = min(first_y + size_y, height - 1)
            last_x = min(first_x + size_x, width - 1)

            #на случай, если фигура не вписывается в картинку
            size_y = last_y - first_y
            size_x = last_x - first_x

            if size_x > size_y or size_y < 20 or size_x < 20:
                continue

            count += 1

            if allow_intersections:
                break
            if (count < 10000):
                is_intersection = intersection_check(list_coordinate_rectangle,
                                                     [first_x, first_y],
                                                     [last_x, last_y])
            else:
                is_intersection = False
                count_fail += 1

        list_coordinate_rectangle.append([[first_x, first_y], [last_x,
                                                               last_y]])

        contur = create_tree([first_x, first_y], [last_x, last_y])

        cv2.fillConvexPoly(overlay, contur, color)

        p_shapes = labelMe_class.Shapes(label,
                                        np_array_to_list_int_points(contur),
                                        None, "polygon", {})

        p_json_list.append(p_shapes)

    if not count_fail == 0:
        print(count_fail)
    alpha = 1
    cv2.addWeighted(overlay, alpha, pic, 1 - alpha, 0, pic)

    res = RetClass(pic, p_json_list)
    return res
Exemplo n.º 27
0
def add_random_patches_mods(img, label, lo=3, hi=10):
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    im = img.copy()

    ret, thresh = cv2.threshold(imgray, 127, 255, 0)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)

    contours.sort(key=len)
    patch_number = np.random.randint(lo, hi + 1)
    print(patch_number)

    p_json_list = []

    offset = 1

    for i in range(patch_number):
        color = random.randint(0, 7)
        intens_color = np.random.randint(128, 256)
        if color == 0:
            cv2.drawContours(img, contours,
                             len(contours) - 1 - i - offset,
                             (0, 0, intens_color), -1)
        elif color == 1:
            cv2.drawContours(img, contours,
                             len(contours) - 1 - i - offset,
                             (0, intens_color, 0), -1)
        elif color == 2:
            cv2.drawContours(img, contours,
                             len(contours) - 1 - i - offset,
                             (intens_color, 0, 0), -1)
        elif color == 3:

            ret, im_gray_mask = cv2.threshold(imgray, 255, 255, 0)

            # 	cv2.imshow ("img",im_gray_mask)
            # 	cv2.waitKey()

            cv2.drawContours(im_gray_mask, contours,
                             len(contours) - 1 - i - offset, 255, -1)

            count_pixel = 0

            sum_r_color = 0
            sum_g_color = 0
            sum_b_color = 0

            h, w, _ = img.shape
            for y in range(0, h):
                for x in range(0, w):
                    if im_gray_mask[y, x] == 255:
                        count_pixel += 1
                        (b, g, r) = im[y, x]
                        sum_b_color += b
                        sum_g_color += g
                        sum_r_color += r

            if count_pixel != 0:
                sum_b_color /= count_pixel
                sum_g_color /= count_pixel
                sum_r_color /= count_pixel
            else:
                print("Div zero")
            cv2.drawContours(img, contours,
                             len(contours) - 1 - i - offset,
                             (sum_b_color, sum_g_color, sum_r_color), -1)

        else:
            b_int, g_int, r_int = get_random_color()
            cv2.drawContours(img, contours,
                             len(contours) - 1 - i - offset,
                             (b_int, g_int, r_int), -1)

        contour = contours[len(contours) - 1 - i - offset]

        p_shapes = labelMe_class.Shapes(label,
                                        contur_to_list_int_points(contour),
                                        None, "polygon", {})
        p_json_list.append(p_shapes)

    res = RetClass(img, p_json_list)
    return res