MerlinEl
05-25-2011, 05:27 PM
Hi
Im' try to trace white areas on image and convert outline to shape.
steps
desaturation -- done
treshold --done
trace shape
-- crashing when I try put a vertex for every white pixel (for test)
-- I need some help here :-)
try DestroyDialog unnamedRollout catch ()
rollout unnamedRollout "Image to Shape v0.01" width:372 height:392 --default size
(
local grab_img, desatured_img, tresholded_img
bitmap bmp1 "Bitmap" pos:[4,76] width:364 height:312
spinner spn_treeshold "" pos:[192,36] width:84 height:16 range:[1,255,120] type:#integer scale:1
progressBar progbar "ProgressBar" pos:[4,60] width:364 height:12 color:green
button btn_capture_image "Capture" pos:[4,4] width:88 height:24
button btn_paste "Paste" pos:[4,32] width:88 height:24
button btn_desature "Desature" pos:[96,4] width:88 height:24
button btn_treeshold "Treeshold" pos:[96,32] width:88 height:24
button btn_trace "Shape Trace" pos:[188,4] width:88 height:24
button btn5 "4" pos:[280,4] width:88 height:24
button btn12 "5" pos:[280,32] width:88 height:24
fn getImage type =
(
local img = case type of
(
#capture: gw.getViewportDib()
#paste: getclipboardBitmap()
)
if img == undefined do return false
local w = img.width
local h = img.height
unnamedRollout.width = w
unnamedRollout.height = h
bmp1.width = w
bmp1.height = h
--local new_img = bitmap w h
--copy img new_img
bmp1.bitmap = img --new_img
progbar.value = 0
grab_img = img
)
fn desatureImage img =
(
local w = img.width
local h = img.height
local new_img = bitmap w h
for i = 0 to (h-1) do
(
local new_line = #()
local p = getpixels img [0,i] w --a horizontal pixel ilne
for j = 1 to w do
(
p[j].s = 0
new_line[j] = p[j]
)
setpixels new_img [0,i] new_line
progbar.value = 100.*i/h
)
bmp1.bitmap = new_img
desatured_img = new_img
)
fn treesholdImage img =
(
local w = img.width
local h = img.height
local new_img = bitmap w h
for i = 0 to (h-1) do
(
local new_line = #()
local p = getpixels img [0,i] w --a horizontal pixel ilne
for j = 1 to w do
(
if p[j].v > spn_treeshold.value then p[j].v = 255 else p[j].v = 0
new_line[j] = p[j]
)
setpixels new_img [0,i] new_line
progbar.value = 100.*i/h
)
bmp1.bitmap = new_img
tresholded_img = new_img
)
fn shapeTrace img =
(
local mem = heapSize -- in my pc is default 12m
if (mem as integer)/1000000 < 50 do heapSize = 50*1000000 -- up to 50m
local w = img.width
local h = img.height
local verts = #()
max create mode
for i = 0 to (h-1) do with redraw off
(
local p = getpixels img [0,i] w --a horizontal pixel ilne
for j = 1 to w do
(
--find first white pixel from left up corner
if p[j] == white do --white pixel found
(
--test
verts +=#([j,i,0])
)
progbar.value = 100.*i/h
)
local me = mesh vertices:verts faces:#()
update me
select me
CompleteRedraw()
)
heapSize = mem
)
on btn_capture_image pressed do (getImage #capture)
on btn_paste pressed do (getImage #paste)
on btn_desature pressed do (desatureImage grab_img)
on btn_treeshold pressed do (treesholdImage desatured_img)
on btn_trace pressed do (shapeTrace tresholded_img )
on spn_treeshold changed val do ()
)
CreateDialog unnamedRollout
Im' try to trace white areas on image and convert outline to shape.
steps
desaturation -- done
treshold --done
trace shape
-- crashing when I try put a vertex for every white pixel (for test)
-- I need some help here :-)
try DestroyDialog unnamedRollout catch ()
rollout unnamedRollout "Image to Shape v0.01" width:372 height:392 --default size
(
local grab_img, desatured_img, tresholded_img
bitmap bmp1 "Bitmap" pos:[4,76] width:364 height:312
spinner spn_treeshold "" pos:[192,36] width:84 height:16 range:[1,255,120] type:#integer scale:1
progressBar progbar "ProgressBar" pos:[4,60] width:364 height:12 color:green
button btn_capture_image "Capture" pos:[4,4] width:88 height:24
button btn_paste "Paste" pos:[4,32] width:88 height:24
button btn_desature "Desature" pos:[96,4] width:88 height:24
button btn_treeshold "Treeshold" pos:[96,32] width:88 height:24
button btn_trace "Shape Trace" pos:[188,4] width:88 height:24
button btn5 "4" pos:[280,4] width:88 height:24
button btn12 "5" pos:[280,32] width:88 height:24
fn getImage type =
(
local img = case type of
(
#capture: gw.getViewportDib()
#paste: getclipboardBitmap()
)
if img == undefined do return false
local w = img.width
local h = img.height
unnamedRollout.width = w
unnamedRollout.height = h
bmp1.width = w
bmp1.height = h
--local new_img = bitmap w h
--copy img new_img
bmp1.bitmap = img --new_img
progbar.value = 0
grab_img = img
)
fn desatureImage img =
(
local w = img.width
local h = img.height
local new_img = bitmap w h
for i = 0 to (h-1) do
(
local new_line = #()
local p = getpixels img [0,i] w --a horizontal pixel ilne
for j = 1 to w do
(
p[j].s = 0
new_line[j] = p[j]
)
setpixels new_img [0,i] new_line
progbar.value = 100.*i/h
)
bmp1.bitmap = new_img
desatured_img = new_img
)
fn treesholdImage img =
(
local w = img.width
local h = img.height
local new_img = bitmap w h
for i = 0 to (h-1) do
(
local new_line = #()
local p = getpixels img [0,i] w --a horizontal pixel ilne
for j = 1 to w do
(
if p[j].v > spn_treeshold.value then p[j].v = 255 else p[j].v = 0
new_line[j] = p[j]
)
setpixels new_img [0,i] new_line
progbar.value = 100.*i/h
)
bmp1.bitmap = new_img
tresholded_img = new_img
)
fn shapeTrace img =
(
local mem = heapSize -- in my pc is default 12m
if (mem as integer)/1000000 < 50 do heapSize = 50*1000000 -- up to 50m
local w = img.width
local h = img.height
local verts = #()
max create mode
for i = 0 to (h-1) do with redraw off
(
local p = getpixels img [0,i] w --a horizontal pixel ilne
for j = 1 to w do
(
--find first white pixel from left up corner
if p[j] == white do --white pixel found
(
--test
verts +=#([j,i,0])
)
progbar.value = 100.*i/h
)
local me = mesh vertices:verts faces:#()
update me
select me
CompleteRedraw()
)
heapSize = mem
)
on btn_capture_image pressed do (getImage #capture)
on btn_paste pressed do (getImage #paste)
on btn_desature pressed do (desatureImage grab_img)
on btn_treeshold pressed do (treesholdImage desatured_img)
on btn_trace pressed do (shapeTrace tresholded_img )
on spn_treeshold changed val do ()
)
CreateDialog unnamedRollout
