import glob import random import cv2,os,numpy import numpy as np # floorDir = './webfloor/wraper_floor_crop' # floorDir = './webfloor/wood_floor_resize640' floorDir = './webfloor/wraper_floor' carpetDir = './webfloor/carpets/carpets2_wraper' floor_list = [floor for floor in glob.glob(floorDir+'/*')] carpet_list = [carpet for carpet in glob.glob(carpetDir + '/*')] savePath = './webfloor/floor_carpet' # 根据已知两点坐标,求过这两点的直线解析方程: a*x+b*y+c = 0 (a >= 0) def getLinearEquation(p1x, p1y, p2x, p2y): sign = 1 a = p2y - p1y if a < 0: sign = -1 a = sign * a b = sign * (p1x - p2x) c = sign * (p1y * p2x - p1x * p2y) #return [a, b, c] #y=kx+bb k=-a/b bb= -c/b return k,bb j = 0 while True: seed1 = random.randint(0, len(floor_list)-1) seed2 = random.randint(0, len(carpet_list)-1) carpet = cv2.imread(carpet_list[seed2]) print(carpet_list[seed2]) carpet = cv2.resize(carpet,(420,360),cv2.INTER_NEAREST) carpet_copy = carpet.copy() #左边三角形的三个点坐标 p_left_up_x, p_left_up_y = 0, 0 p_left_down_x, p_left_down_y = 0, carpet.shape[0] pp_x, ppy = 50, 0 # point_list = [(int(p_left_up_x),int(p_left_up_y)),(int(p_left_down_x),int(p_left_down_y)),(pp_x,ppy)] # for point in point_list: # cv2.circle(floor, point, 3, (255, 0, 0), 3) # 两点的直线方程,斜边. 左边的三角形 k_l, b_l = getLinearEquation(p_left_down_x, p_left_down_y, pp_x, ppy) # 找出斜边上所有点的坐标,就可以找到没三角形内每一行遍历的起点和终点 # for y0 in range(ppy, p_left_down_y): # x0 = (y0 - b_l) / k_l # point = (int(x0), int(y0)) # x00.append(int(x0)) # points00.append(point) # cv2.circle(carpet_copy, point, 2, (255, 0, 0), 1)# 划线 #找出三角形内所有的点,并赋值为0 for column_l in range(p_left_up_y, p_left_down_y): for row_l in range(p_left_up_x, int((column_l - b_l) / k_l)): carpet_copy[column_l][row_l] = 255 #右边三角形的三个点坐标 p_right_x1,p_right_y1 = carpet.shape[1]-50,0 #三角形左边点的坐标 p_right_x2, p_right_y2 = carpet.shape[1],0 #三角形右边点的坐标 p_right_x3, p_right_y3 = carpet.shape[1],carpet.shape[0] # 右边的斜线,右边的三角形 k_r, b_r = getLinearEquation(p_right_x1, p_right_y1, p_right_x3, p_right_y3) for column_r in range(p_right_y2,p_right_y3): for row_r in range(int((column_r - b_r) / k_r),p_right_x2): carpet_copy[column_r][row_r] = 255 # kernel = np.ones(shape=[3, 3], dtype=np.uint8) # img_ = cv2.dilate(carpet, kernel) # img = cv2.medianBlur(img_, 3) floor = cv2.imread(floor_list[seed1]) floor_cp = floor.copy() # cv2.imshow('im0', floor_cp) # cv2.imshow('im2', carpet) floor[100:460,100:520] = carpet_copy # cv2.imshow('im2', floor) where0BG = numpy.where(floor == 255) floor[where0BG] = floor_cp[where0BG] # carpet tran cv2.imwrite('{}/0906floor_carpet_{}.jpg'.format(savePath,j),floor) j += 1 # cv2.imshow('im',floor) # cv2.waitKey(0)