Skip to content

Commit 0e3be4c

Browse files
author
yxdragon
committed
ai brush
1 parent 0706b08 commit 0e3be4c

File tree

12 files changed

+401
-49
lines changed

12 files changed

+401
-49
lines changed

imagepy/core/manager/colormanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_front(cls, one=False):
5050
return np.dot((cls.wr,cls.wg,cls.wb), cls.frontcolor)
5151

5252
@classmethod
53-
def get_back(cls, one):
53+
def get_back(cls, one=False):
5454
if not one:return cls.backcolor
5555
return np.dot((cls.wr,cls.wg,cls.wb), cls.backcolor)
5656

imagepy/core/mark/mark.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def draw_text(pts, dc, f, **key):
251251
brush.SetColour(pen.GetColour())
252252
brush.SetStyle(100)
253253
if 'fcolor' in pts:
254-
print('hahaha')
255254
dc.SetTextBackground(pts['fcolor'])
256255
if 'size' in pts:
257256
font.SetPointSize(pts['size'])

imagepy/ipyalg/graph/connect.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from numba import jit
3+
import random
34
from scipy.ndimage import generate_binary_structure
45

56
def neighbors(shape, conn=1):
@@ -13,26 +14,45 @@ def neighbors(shape, conn=1):
1314
return np.dot(idx, acc[::-1])
1415

1516
@jit(nopython=True)
16-
def search(img, nbs):
17+
def unique(idx):
18+
msk = idx[:,0]<idx[:,1]
19+
key = idx[:,0]<<16
20+
key += idx[:,1]
21+
sort = np.argsort(key)
22+
idx[:] = idx[sort]
23+
s = i = 1
24+
while s<len(idx):
25+
if key[sort[s]]!=key[sort[s-1]]:
26+
idx[i,0] = idx[s,0]
27+
idx[i,1] = idx[s,1]
28+
i += 1
29+
s += 1
30+
return i
31+
32+
@jit(nopython=True)
33+
def search(img, nbs, back):
1734
s, line = 0, img.ravel()
18-
rst = np.zeros((len(line),2), img.dtype)
35+
rst = np.zeros((len(line)//2, 2), img.dtype)
1936
for i in range(len(line)):
2037
if line[i]==0:continue
2138
for d in nbs:
22-
if line[i+d]==0: continue
39+
if not back and line[i+d]==0: continue
2340
if line[i]==line[i+d]: continue
2441
rst[s,0] = line[i]
2542
rst[s,1] = line[i+d]
2643
s += 1
44+
45+
if s==len(rst):
46+
s = unique(rst)
2747
return rst[:s]
28-
29-
def connect(img, conn=1):
48+
49+
def connect_graph(img, conn=1, back=False):
3050
buf = np.pad(img, 1, 'constant')
3151
nbs = neighbors(buf.shape, conn)
32-
rst = search(buf, nbs)
33-
if len(rst)==0: return rst
34-
rst.sort()
35-
return np.unique(rst, axis=0)
52+
rst = search(buf, nbs, back)
53+
if len(rst)<2: return rst
54+
rst.sort(axis=1)
55+
return rst[:unique(rst)].copy()
3656

3757
def mapidx(idx):
3858
dic = {}
@@ -47,5 +67,5 @@ def mapidx(idx):
4767
[1,1,2,2,1],
4868
[1,3,0,0,1],
4969
[1,3,1,1,4]])
50-
rst = connect(img, 2)
70+
rst = connect_graph(img, 2, True)
5171
print(rst)

imagepy/ipyalg/graph/render.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random
2+
3+
def node_render(conmap, n=4, rand=10, shuffle=True):
4+
nodes = list(conmap.keys())
5+
colors = dict(zip(nodes, [0]*len(nodes)))
6+
counter = dict(zip(nodes, [0]*len(nodes)))
7+
if shuffle: random.shuffle(nodes)
8+
while len(nodes)>0:
9+
k = nodes.pop(0)
10+
counter[k] += 1
11+
hist = [1e4] + [0] * n
12+
for p in conmap[k]:
13+
hist[colors[p]] += 1
14+
if min(hist)==0:
15+
colors[k] = hist.index(min(hist))
16+
counter[k] = 0
17+
continue
18+
hist[colors[k]] = 1e4
19+
minc = hist.index(min(hist))
20+
if counter[k]==rand:
21+
counter[k] = 0
22+
minc = random.randint(1,4)
23+
colors[k] = minc
24+
for p in conmap[k]:
25+
if colors[p] == minc:
26+
nodes.append(p)
27+
return colors

imagepy/menus/Analysis/Region Analysis/connect_plg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
from skimage.measure import regionprops
55
from imagepy.core.mark import GeometryMark
66
from scipy.ndimage import label, generate_binary_structure
7-
from imagepy.ipyalg.graph.connect import connect, mapidx
7+
from imagepy.ipyalg.graph.connect import connect_graph, mapidx
88
import pandas as pd
99

1010
# center, area, l, extent, cov
1111
class Plugin(Simple):
1212
title = 'Connective Analysis'
1313
note = ['8-bit', '16-bit', 'int']
14-
para = {'con':'8-connect', 'labled':False, 'slice':False}
14+
para = {'con':'8-connect', 'labled':False, 'nozero':True, 'slice':False}
1515
view = [(list, 'con', ['4-connect', '8-connect'], str, 'conection', 'pix'),
1616
(bool, 'labled', 'it is a label image'),
17+
(bool, 'nozero', 'nonzero'),
1718
(bool, 'slice', 'slice')]
1819

1920
#process
@@ -27,7 +28,7 @@ def run(self, ips, imgs, para = None):
2728
for i in range(len(imgs)):
2829
if para['labled']: buf = imgs[i]
2930
else: label(imgs[i], generate_binary_structure(2, 1), output=buf)
30-
conarr = connect(buf, 1 if para['con']=='4-connect' else 2)
31+
conarr = connect(buf, 1 if para['con']=='4-connect' else 2, not self.para['nozero'])
3132
conmap = mapidx(conarr)
3233

3334
ls = regionprops(buf)

imagepy/menus/Analysis/label_plg.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,56 @@
55
"""
66
import numpy as np
77
from scipy.ndimage import label, generate_binary_structure
8-
98
from imagepy import IPy
10-
from imagepy.core.engine import Filter
11-
from imagepy.ui.canvasframe import CanvasFrame
9+
from imagepy.core import ImagePlus
10+
from imagepy.core.engine import Filter, Simple
11+
from imagepy.ipyalg.graph import connect, render
1212

13-
class Plugin(Filter):
13+
class Label(Simple):
1414
title = 'Label Image'
15-
note = ['8-bit', 'not_slice', 'preview']
16-
17-
para = {'thr':128, 'con':'4-Connect'}
18-
view = [('slide', 'thr', (0,255), 0, 'Threshold'),
19-
(list, 'con', ['4-Connect','8-Connect'], str, 'Structure', 'connect')]
20-
21-
def load(self, ips):
22-
self.lut = ips.lut
23-
ips.lut = self.lut.copy()
24-
return True
25-
26-
def preview(self, ips, para):
27-
ips.lut[:] = self.lut
28-
ips.lut[para['thr']:] = [255,0,0]
29-
ips.update()
30-
31-
def run(self, ips, snap, img, para = None):
32-
if para == None: para = self.para
33-
ips.lut = self.lut
34-
msk = img>para['thr']
35-
con = 1 if para['con']=='4-Connect' else 2
36-
strc = generate_binary_structure(2, con)
37-
msk = label(msk, strc, output = np.int16)
15+
note = ['8-bit', '16-bit']
16+
para = {'slice':False, 'con':'4-Connect'}
17+
view = [(list, 'con', ['4-Connect','8-Connect'], str, 'Structure', 'connect'),
18+
(bool, 'slice', 'slice')]
3819

39-
IPy.show_img([msk[0]], ips.title+'-label')
20+
def run(self, ips, imgs, para = None):
21+
if not para['slice']: imgs = [ips.img]
22+
labels = []
23+
for i in range(len(imgs)):
24+
self.progress(i, len(imgs))
25+
con = 1 if para['con']=='4-Connect' else 2
26+
strc = generate_binary_structure(2, con)
27+
lab, n = label(imgs[i], strc, output = np.int32)
28+
labels.append(lab)
29+
IPy.show_img(labels, ips.title+'-label')
4030

31+
class Render(Simple):
32+
title = 'Label Render'
33+
note = ['8-bit', '16-bit', 'int']
34+
para = {'slice':False, 'con':'8-Connect', 'colors':6, 'back':False}
35+
view = [(list, 'con', ['4-Connect','8-Connect'], str, 'Structure', 'connect'),
36+
(int, 'colors', (4, 255), 0, 'colors', 'n'),
37+
(bool, 'back', 'background'),
38+
(bool, 'slice', 'slice')]
4139

42-
40+
def run(self, ips, imgs, para = None):
41+
if not para['slice']: imgs = [ips.img]
42+
print(para)
43+
labels = []
44+
for i in range(len(imgs)):
45+
self.progress(i, len(imgs))
46+
con = 1 if para['con']=='4-Connect' else 2
47+
idx = connect.connect_graph(imgs[i], con, para['back'])
48+
idx = connect.mapidx(idx)
49+
cmap = render.node_render(idx, para['colors'], 10)
50+
51+
lut = np.ones(imgs[i].max()+1, dtype=np.uint8)
52+
lut[0] = 0
53+
for j in cmap: lut[j] = cmap[j]
54+
labels.append(lut[imgs[i]])
55+
56+
ips = ImagePlus(labels, ips.title+'-render')
57+
ips.range = (0, para['colors'])
58+
IPy.show_ips(ips)
59+
60+
plgs = [Label, Render]

imagepy/menus/Process/Segment/shift_plgs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Felzenszwalb(Filter):
3737
note = ['all', 'not_slice', 'auto_snap', 'auto_msk', 'not_channel', 'preview']
3838

3939
para = {'scale':1.0, 'sigma':0.8, 'min_size':20}
40-
view = [(float, 'scale', (0.01, 10), 2, 'scale', ''),
40+
view = [(float, 'scale', (0.01, 1024), 2, 'scale', ''),
4141
(float, 'sigma', (0, 30), 2, 'sigma', ''),
4242
(int, 'min_size', (1, 1024), 0, 'min_size', '')]
4343

@@ -51,7 +51,7 @@ class SLICLab(Simple):
5151

5252
para = {'n_segments':100, 'compactness':10.0, 'max_iter':10, 'sigma':0, 'stack':False}
5353
view = [(int, 'n_segments', (1, 1e8), 0, 'segments', 'n'),
54-
(float, 'compactness', (0.01, 100), 2, 'campactness', 'color-space'),
54+
(float, 'compactness', (0.01, 1024), 2, 'campactness', 'color-space'),
5555
(int, 'max_iter', (3, 50), 0, 'max_iter', 'n'),
5656
(float, 'sigma', (0, 30), 1, 'sigma', 'smooth'),
5757
(bool, 'stack', 'stack')]

imagepy/tools/Draw/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
catlog = ['magic_tol', 'floodfill_tol', 'flood3d_tol']
1+
catlog = ['magic_tol', 'floodfill_tol', 'flood3d_tol', 'aibrush_tol']

0 commit comments

Comments
 (0)