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
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
Beispiel #3
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
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
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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
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
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
Beispiel #10
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
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
Beispiel #12
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
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
Beispiel #14
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
Beispiel #15
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
Beispiel #16
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
Beispiel #17
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