Browse Source

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

pull/1/head
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 tempfile
import FreeCADGui as Gui
import FreeCAD as App
@ -52,57 +53,41 @@ class AHB_Render:
b = step
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()
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()
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
import time
lines = Image.open("/home/youen/dev_linux/vhelio-render/vhelio.png")
from PIL import Image, ImageFilter
#import time
lines = Image.open(temp_lines_file_name)
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
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()
startTime = time.perf_counter()
for xb in range(1, lines.size[0]-cacheHalfWidth, cacheHalfWidth):
for yb in range(1, lines.size[1]-1):
for y in range(0, 4):
cacheLine = cache[y]
for x in range(0, cacheWidth + 2):
cacheLine[x] = shapes.getpixel((xb*2+x-1, yb*2+y-1))
outlines = shapes.filter(ImageFilter.Kernel((3, 3), (-1, -1, -1, -1, 8, -1, -1, -1, -1), 1, 0))
outlines2 = shapes.filter(ImageFilter.Kernel((3, 3), (1, 1, 1, 1, -8, 1, 1, 1, 1), 1, 0))
outlines = outlines.convert("L")
outlines2 = outlines2.convert("L")
outlines = outlines.point(lambda p: 0 if p > 0 else 255)
outlines2 = outlines2.point(lambda p: 0 if p > 0 else 255)
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):
outline_color = 1.0
for xaa in range(0, 2):
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")
#endTime = time.perf_counter()
#print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s")
lines.save(filename)
from ahb_command import AHB_CommandWrapper
AHB_CommandWrapper.addGuiCommand('AHB_render', AHB_Render())

Loading…
Cancel
Save