Browse Source

reimplemented edge detection with pillow native functions for better performances (x30)

master
Youen Toupin 3 years ago
parent
commit
a99adfad7b
  1. 69
      ahb_cmd_render.py

69
ahb_cmd_render.py

@ -1,4 +1,5 @@
import os import os
import tempfile
import FreeCADGui as Gui import FreeCADGui as Gui
import FreeCAD as App import FreeCAD as App
@ -52,57 +53,41 @@ class AHB_Render:
b = step b = step
def Activated(self): def Activated(self):
resolution = (1024, 1024) resolution = (2048, 2048)
filename = "/home/youen/dev_linux/vhelio-render/vhelio.png"
temp_lines_file_name = tempfile.gettempdir() + "/ahb_temp_lines.png"
temp_shapes_file_name = tempfile.gettempdir() + "/ahb_temp_shapes.png"
self.set_render_lines() self.set_render_lines()
Gui.ActiveDocument.ActiveView.saveImage("/home/youen/dev_linux/vhelio-render/vhelio.png", resolution[0], resolution[1], "#ffffff") Gui.ActiveDocument.ActiveView.saveImage(temp_lines_file_name, resolution[0], resolution[1], "#ffffff")
self.set_render_outlines() self.set_render_outlines()
Gui.ActiveDocument.ActiveView.saveImage("/home/youen/dev_linux/vhelio-render/vhelio-shapes.png", resolution[0]*2, resolution[1]*2, "#ffffff") Gui.ActiveDocument.ActiveView.saveImage(temp_shapes_file_name, resolution[0]*2, resolution[1]*2, "#ffffff")
from PIL import Image from PIL import Image, ImageFilter
import time #import time
lines = Image.open("/home/youen/dev_linux/vhelio-render/vhelio.png") lines = Image.open(temp_lines_file_name)
lines = lines.convert("L") lines = lines.convert("L")
shapes = Image.open("/home/youen/dev_linux/vhelio-render/vhelio-shapes.png") shapes = Image.open(temp_shapes_file_name)
cacheWidth = 8 #startTime = time.perf_counter()
cacheHalfWidth = int(cacheWidth/2)
cache = []
for y in range(0, 4):
cacheLine = []
for x in range(0, cacheWidth + 2):
cacheLine.append((0.0,0.0,0.0))
cache.append(cacheLine)
startTime = time.perf_counter() outlines = shapes.filter(ImageFilter.Kernel((3, 3), (-1, -1, -1, -1, 8, -1, -1, -1, -1), 1, 0))
for xb in range(1, lines.size[0]-cacheHalfWidth, cacheHalfWidth): outlines2 = shapes.filter(ImageFilter.Kernel((3, 3), (1, 1, 1, 1, -8, 1, 1, 1, 1), 1, 0))
for yb in range(1, lines.size[1]-1): outlines = outlines.convert("L")
for y in range(0, 4): outlines2 = outlines2.convert("L")
cacheLine = cache[y] outlines = outlines.point(lambda p: 0 if p > 0 else 255)
for x in range(0, cacheWidth + 2): outlines2 = outlines2.point(lambda p: 0 if p > 0 else 255)
cacheLine[x] = shapes.getpixel((xb*2+x-1, yb*2+y-1)) outlines.paste(outlines2, None, outlines2.point(lambda p: 255 - p))
#outlines.save("/home/youen/dev_linux/vhelio-render/vhelio-outlines.png")
lines = lines.resize(outlines.size, Image.NEAREST)
lines.paste(outlines, None, outlines.point(lambda p: 255 - p))
lines = lines.resize(lines.size, Image.BILINEAR)
for xa in range(0, cacheHalfWidth): #endTime = time.perf_counter()
outline_color = 1.0 #print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s")
for xaa in range(0, 2): lines.save(filename)
x = xa*2 + xaa
for y in range(0, 2):
c = cache[y+1][x+1]
border = False
if c != cache[y][x]: border = True
if c != cache[y][x+1]: border = True
if c != cache[y][x+2]: border = True
if c != cache[y+1][x]: border = True
if c != cache[y+1][x+2]: border = True
if c != cache[y+2][x]: border = True
if c != cache[y+2][x+1]: border = True
if c != cache[y+2][x+2]: border = True
if border: outline_color = outline_color - 0.25
lines.putpixel((xb+xa, yb), min(lines.getpixel((xb+xa, yb)), int(outline_color * 255.0)))
endTime = time.perf_counter()
print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s")
lines.save("/home/youen/dev_linux/vhelio-render/vhelio-final.png")
from ahb_command import AHB_CommandWrapper from ahb_command import AHB_CommandWrapper
AHB_CommandWrapper.addGuiCommand('AHB_render', AHB_Render()) AHB_CommandWrapper.addGuiCommand('AHB_render', AHB_Render())

Loading…
Cancel
Save