Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the final RGB result #5

Open
EKGD opened this issue Feb 6, 2023 · 2 comments
Open

Get the final RGB result #5

EKGD opened this issue Feb 6, 2023 · 2 comments

Comments

@EKGD
Copy link

EKGD commented Feb 6, 2023

I love your works, But is there any way that i can get the final RGB result with out wait huge amount of time for the process ?.
Thank you for your work

@TZYSJTU
Copy link
Owner

TZYSJTU commented Feb 6, 2023 via email

@EKGD
Copy link
Author

EKGD commented Feb 7, 2023

thanks for your rely.
i modified the code and remove all the cv2.waitkey() and cv2.witre() process in the loop. however it still cost lot of time ( more than 12 to render my image). This is my code. please help.
`def process(input_path,output_path,step):
global max
global trueStep
file_name = os.path.basename(input_path)
file_name = file_name.split('.')[0]
output_path_file = output_path+"/"+file_name
if not os.path.exists(output_path_file):
os.makedirs(output_path_file)
os.makedirs(output_path_file+"/mask")
# os.makedirs(output_path+"/process")
if not os.path.exists(output_path+"/process"):
#os.makedirs(output_path+"/mask")
os.makedirs(output_path+"/process")
####### ETF #######
time_start=time.time()
ETF_filter = ETF(input_path=input_path, output_path=output_path_file+'/mask',
dir_num=direction, kernel_radius=kernel_radius, iter_time=iter_time, background_dir=background_dir)
ETF_filter.forward()
print('ETF done')

input_img = cv2.imread(input_path, cv2.IMREAD_GRAYSCALE)
(h0,w0) = input_img.shape
cv2.imwrite(output_path_file + "/input_gray.jpg", input_img)
# if h0>w0:
#     input_img = cv2.resize(input_img,(int(256*w0/h0),256))
# else:
#     input_img = cv2.resize(input_img,(256,int(256*h0/w0)))    
# (h0,w0) = input_img.shape

if transTone == True:
    input_img = transferTone(input_img)

now_ = np.uint8(np.ones((h0,w0)))*255
if draw_new==True:
    time_start=time.time()
    stroke_sequence=[]
    stroke_temp={'angle':None, 'grayscale':None, 'row':None, 'begin':None, 'end':None}
    for dirs in range(direction):
        angle = -90+dirs*180/direction
        print('angle:', angle)
        stroke_temp['angle'] = angle
        img,_ = rotate(input_img, -angle)

        ############ Adjust Histogram ############
        if CLAHE==True:
            img = HistogramEqualization(img)
        # cv2.imshow('HistogramEqualization', res)
        # cv2.waitKey(0)
        # cv2.imwrite(output_path + "/HistogramEqualization.png", res)
        print('HistogramEqualization done')

        ########### gredient #######
        img_pad = cv2.copyMakeBorder(img, 2*period, 2*period, 2*period, 2*period, cv2.BORDER_REPLICATE)
        img_normal = cv2.normalize(img_pad.astype("float32"), None, 0.0, 1.0, cv2.NORM_MINMAX)

        x_der = cv2.Sobel(img_normal, cv2.CV_32FC1, 1, 0, ksize=5) 
        y_der = cv2.Sobel(img_normal, cv2.CV_32FC1, 0, 1, ksize=5) 

        x_der = torch.from_numpy(x_der) + 1e-12
        y_der = torch.from_numpy(y_der) + 1e-12

        gradient_magnitude = torch.sqrt(x_der**2.0 + y_der**2.0)
        gradient_norm = gradient_magnitude/gradient_magnitude.max()

        ############   Quantization   ############ 
        ldr = LDR(img, n)
        # cv2.imshow('Quantization', ldr)
        # cv2.waitKey(0)
        cv2.imwrite(output_path_file + "/Quantization.png", ldr)

        # LDR_single(ldr,n,output_path) # debug
        ############     Cumulate     ############
        LDR_single_add(ldr,n,output_path_file)
        print('Quantization done')
        

        # get tone
        (h,w) = ldr.shape
        canvas = Gassian((h+4*period,w+4*period), mean=250, var = 3)


        for j in range(n):
            # print('tone:',j)
            # distribution = ChooseDistribution(period=period,Grayscale=j*256/n)
            stroke_temp['grayscale'] = j*256/n
            mask = cv2.imread(output_path_file + '/mask/mask{}.png'.format(j),cv2.IMREAD_GRAYSCALE)/255
            dir_mask = cv2.imread(output_path_file + '/mask/dir_mask{}.png'.format(dirs),cv2.IMREAD_GRAYSCALE)
            # if angle==0:
            #     dir_mask[::] = 255
            dir_mask,_ = rotate(dir_mask, -angle, pad_color=0)
            dir_mask[dir_mask<128]=0
            dir_mask[dir_mask>127]=1

            distensce = Gassian((1,int(h/period)+4), mean = period, var = 1)
            distensce = np.uint8(np.round(np.clip(distensce, period*0.8, period*1.25)))
            raw = -int(period/2)

            for i in np.squeeze(distensce).tolist():
                if raw < h:    
                    y = raw + 2*period # y < h+2*period
                    raw += i        
                    for interval in get_start_end(mask[y-2*period]*dir_mask[y-2*period]):

                        begin = interval[0]
                        end = interval[1]

                        # length = end - begin
                        
                        begin -= 2*period
                        end += 2*period

                        length = end - begin
                        stroke_temp['begin'] = begin
                        stroke_temp['end'] = end
                        stroke_temp['row'] = y-int(period/2)
                        #print(gradient_norm[y,interval[0]+2*period:interval[1]+2*period])
                        stroke_temp['importance'] = (255-stroke_temp['grayscale'])*torch.sum(gradient_norm[y:y+period,interval[0]+2*period:interval[1]+2*period]).numpy()

                        stroke_sequence.append(stroke_temp.copy())
                        # newline = Getline(distribution=distribution, length=length)
                        # if length<1000 or begin == -2*period or end == w-1+2*period:
                        #     temp = canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin:2*period+end]
                        #     m = np.minimum(temp, newline[:,:temp.shape[1]])
                        #     canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin:2*period+end] = m
                        # else:
                        #     temp = canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin-2*period:2*period+end+2*period]
                        #     m = np.minimum(temp, newline)
                        #     canvas[y-int(period/2):y-int(period/2)+2*period,2*period+begin-2*period:2*period+end+2*period] = m
                        
                    
                        # if step % Freq == 0:
                        #     if step > Freq: # not first time 
                        #         before = cv2.imread(output_path + "/process/{0:04d}.png".format(int(step/Freq)-1), cv2.IMREAD_GRAYSCALE)
                        #         now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
                        #         (H,W) = now.shape
                        #         now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
                        #         now = np.minimum(before,now)
                        #     else: # first time to save
                        #         now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
                        #         (H,W) = now.shape
                        #         now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
                            
                        #     cv2.imwrite(output_path + "/process/{0:04d}.png".format(int(step/Freq)), now)
                        #     # cv2.imshow('step', canvas)
                        #     # cv2.waitKey(0)

                        # now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
                        # (H,W) = now.shape
                        # now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]       
                        # now = np.minimum(now,now_)                   
                        # step += 1
                        # cv2.imshow('step', now_)
                        # cv2.waitKey(1)       
                        # now_ = now      

            # now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
            # (H,W) = now.shape
            # now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]                          
            # cv2.imwrite(output_path + "/pro/{}_{}.png".format(dirs,j), now)            

        # now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
        # (H,W) = now.shape
        # now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
        # cv2.imwrite(output_path + "/{:.1f}.png".format(angle), now)
        # cv2.destroyAllWindows()

    time_end=time.time()
    print('total time',time_end-time_start)
    print('stoke number',len(stroke_sequence))
    # cv2.imwrite(output_path + "/draw.png", now_)
    # cv2.imshow('draw', now_)
    # cv2.waitKey(0) 
    
    if random_order == True:
        random.shuffle(stroke_sequence)   


    if ETF_order == True:
        random.shuffle(stroke_sequence)   
        quickSort(stroke_sequence,0,len(stroke_sequence)-1)
    result = Gassian((h0,w0), mean=250, var = 3)
    canvases = []


    for dirs in range(direction):
        angle = -90+dirs*180/direction
        canvas,_ = rotate(result, -angle)
        # (h,w) = canvas.shape
        canvas = np.pad(canvas, pad_width=2*period, mode='constant', constant_values=(255,255))
        canvases.append(canvas)
        

    count = 0
    for stroke_temp in stroke_sequence:
        count+=1
        print("count:" + str(count))
        angle = stroke_temp['angle']
        dirs = int((angle+90)*direction/180)
        grayscale = stroke_temp['grayscale']
        distribution = ChooseDistribution(period=period,Grayscale=grayscale)
        row = stroke_temp['row']
        begin = stroke_temp['begin']
        end = stroke_temp['end']
        length = end - begin
        newline = Getline(distribution=distribution, length=length)

        canvas = canvases[dirs]

        if length<1000 or begin == -2*period or end == w-1+2*period:
            temp = canvas[row:row+2*period,2*period+begin:2*period+end]
            m = np.minimum(temp, newline[:,:temp.shape[1]])
            canvas[row:row+2*period,2*period+begin:2*period+end] = m
        # else:
        #     temp = canvas[row:row+2*period,2*period+begin-2*period:2*period+end+2*period]
        #     m = np.minimum(temp, newline)
        #     canvas[row:row+2*period,2*period+begin-2*period:2*period+end+2*period] = m
        
        now,_ = rotate(canvas[2*period:-2*period,2*period:-2*period], angle)
        (H,W) = now.shape
        now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]       
        result = np.minimum(now,result)     
        
        # if process_visible == True:
        #     cv2.imshow('step', result)
        #     #cv2.waitKey(1)   

    #     step += 1
    #     if step % Freq == 0:
    #         # if step > Freq: # not first time 
    #         #     before = cv2.imread(output_path + "/process/{0:04d}.png".format(int(step/Freq)-1), cv2.IMREAD_GRAYSCALE)
    #         #     now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
    #         #     (H,W) = now.shape
    #         #     now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
    #         #     now = np.minimum(before,now)
    #         # else: # first time to save
    #         #     now,_ = rotate(canvas[2*period:2*period+h,2*period:2*period+w], angle)
    #         #     (H,W) = now.shape
    #         #     now = now[int((H-h0)/2):int((H-h0)/2)+h0, int((W-w0)/2):int((W-w0)/2)+w0]
    #         trueStep+=1
    #         cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(trueStep), result)
    #        # cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(int(step/Freq)), result)
              
            
    #         # cv2.imshow('step', canvas)
    #         # cv2.waitKey(0)  
    # if step % Freq != 0:
    #     step = int(step/Freq)+1
    #     trueStep+=1
    #     cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(trueStep), result)
    #     #cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(step), result)  
        
        
    
    cv2.destroyAllWindows()
    time_end=time.time()
    print('total time',time_end-time_start)
    print('stoke number',len(stroke_sequence))
    cv2.imwrite(output_path_file + '/draw.jpg', result)



############ gen edge ###########                                                                                                                                                                                                                                                                                                 

# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# pc = PencilDraw(device=device, gammaS=1)
# pc(input_path)
# edge = cv2.imread('output/Edge.png', cv2.IMREAD_GRAYSCALE)

edge = genStroke(input_img,18)
edge = np.power(edge, deepen)
edge = np.uint8(edge*255)
if edge_CLAHE==True:
    edge = HistogramEqualization(edge)

cv2.imwrite(output_path_file + '/edge.jpg', edge)
cv2.imshow("edge",edge)


############# merge #############
edge = np.float32(edge)
now_ = cv2.imread(output_path_file + "/draw.jpg", cv2.IMREAD_GRAYSCALE)
result = res_cross= np.float32(now_)

result[1:,1:] = np.uint8(edge[:-1,:-1] * res_cross[1:,1:]/255)
result[0] = np.uint8(edge[0] * res_cross[0]/255)
result[:,0] = np.uint8(edge[:,0] * res_cross[:,0]/255)
result = edge*res_cross/255
result=np.uint8(result)  
# for x in range(max):
#     trueStep+=1
#     cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(trueStep), result)  
#     print(str(trueStep)+" final")
#cv2.imwrite(output_path + '/process/result.jpg', result)
#trueStep += 1
#cv2.imwrite(output_path + "/process/{0:04d}.png".format(step+1), result)
#cv2.imshow("result",result)


# deblue
deblue(result, output_path)

# RGB
img_rgb_original = cv2.imread(input_path, cv2.IMREAD_COLOR)

cv2.imwrite(output_path_file + "/input.jpg", img_rgb_original)
img_yuv = cv2.cvtColor(img_rgb_original, cv2.COLOR_BGR2YUV)
img_yuv[:,:,0] = result
img_rgb = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) 

#cv2.imshow("RGB",img_rgb).
#cv2.waitKey(0)
cv2.imwrite(output_path + "/process/"+file_name+".jpg", img_rgb)
# for x in range(max):
#     trueStep+=1
#     cv2.imwrite(output_path + "/process/{0:04d}.jpg".format(trueStep), img_rgb)
#     print(str(trueStep)+" rgb")
#cv2.imwrite(output_path + "/process/result_RGB.jpg",img_rgb)

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants