julia 调用python库_Julia调用Python实现超像素分割SLIC算法
最近想要在julia中實現 Simple Linear Iterative Clustering (SLIC) 算法對圖像進行超像素分割,關于SLIC超像素分割算法,請參考SLIC Superpixels Compared to State-of-the-art Superpixel Methods。在網上搜索了一番,找到了SLIC算法的python實現代碼,見laixintao仁兄SLIC算法分割超像素原理及Python實現。鑒于SLIC代碼還比較復雜,就不想再去費時間自己動手寫julia實現了,于是研究了一下如何通過julia調用python代碼。
julia與Python的混合編程需要利用PyCall包來調用python,官方說明文檔見Calling Python functions from the Julia language。(目前還沒有見到有中文版本說明。)通過PyCall包,可以引入任意的python模塊、調用Python函數、定義python的類、以及共享Python的數據類型。
首先需要在python中安裝PyCall包,運行
Pkg.add("PyCall")
系統(tǒng)中要安裝python,如果使用Linux系統(tǒng),如筆者的是ubuntu,系統(tǒng)默認安裝的是python3,這也是PyCall默認使用的版本。由于laixintao仁兄的代碼是基于Python2寫的,只能改變PyCall使用的python版本,通過以下命令實現
ENV["PYTHON"] = "/usr/bin/python2.7" # example for 2.7
(具體路徑可能不同,在linux終端可以通過命令 whereis python查看所有版本的位置。)
安裝并配置好PyCall后,通過pyimport命令就可以調用python內置的宏、包和函數,并且可以實現復雜數據類型之間的轉換,由于本人對Python不熟悉,所以有一些也看不懂。這里我關心的只是如何調用第三方寫好的python腳本。
其實實現起來也簡單,參見如下說明:
If you are already familiar with Python, it perhaps is easier to use py"..." and py"""...""" which are equivalent to Python's
py"""
import numpy as np
def sinpi(x):
return np.sin(np.pi * x)
"""
py"sinpi"(1)
When creating a Julia module, it is a useful pattern to define Python functions or classes in Julia's __init__ and then use it in Julia function with py"...".
module MyModule
using PyCall
function __init__()
py"""
import numpy as np
def one(x):
return np.sin(x) ** 2 + np.cos(x) ** 2
"""
end
two(x) = py"one"(x) + py"one"(x)
end
Note that Python code in py"..." of above example is evaluated in a Python namespace dedicated to MyModule. Thus, Python function one cannot be accessed outside MyModule.
這里我要總結兩點,
1)務必使用module模塊中把Python腳本的代碼包含進去,不然會出現不可描述的錯誤提示;
2)只能通過在 py""" 代碼 """ 后面另寫一個julia函數的方式(例如以上的two函數)調用python 函數,實現函數功能并輸出結果,調用python函數時 " " 引號只能引函數名,函數參量都在引號外,不然,也會報錯。
以下就是我的代碼,通過調用SLIC python代碼實現對圖像的超像素分割,并返回每一個超像素所包含的像素點坐標。py""" """之間是調用的python源代碼,后面的function是我寫的調用并輸出結果的函數,最后的clusters=...命令是實現對圖像操作。
module MyModule
using PyCall
function __init__()
py"""
import math
from skimage import io, color
import numpy as np
from tqdm import trange
class Cluster(object):
cluster_index = 1
def __init__(self, h, w, l=0, a=0, b=0):
self.update(h, w, l, a, b)
self.pixels = []
self.no = self.cluster_index
Cluster.cluster_index += 1
def update(self, h, w, l, a, b):
self.h = h
self.w = w
self.l = l
self.a = a
self.b = b
def __str__(self):
return "{},{}:{} {} {} ".format(self.h, self.w, self.l, self.a, self.b)
def __repr__(self):
return self.__str__()
class SLICProcessor(object):
@staticmethod
def open_image(path):
rgb = io.imread(path)
lab_arr = color.rgb2lab(rgb)
return lab_arr
@staticmethod
def save_lab_image(path, lab_arr):
rgb_arr = color.lab2rgb(lab_arr)
io.imsave(path, rgb_arr)
def make_cluster(self, h, w):
return Cluster(h, w,
self.data[h][w][0],
self.data[h][w][1],
self.data[h][w][2])
def __init__(self, filename, K, M):
self.K = K
self.M = M
self.mycount = 0
self.data = self.open_image(filename)
self.image_height = self.data.shape[0]
self.image_width = self.data.shape[1]
self.N = self.image_height * self.image_width
self.S = int(math.sqrt(self.N / self.K))
self.clusters = []
self.label = {}
self.dis = np.full((self.image_height, self.image_width), np.inf)
def init_clusters(self):
h = self.S / 2
w = self.S / 2
while h < self.image_height:
while w < self.image_width:
self.clusters.append(self.make_cluster(h, w))
self.mycount += 1
w += self.S
w = self.S / 2
h += self.S
def get_gradient(self, h, w):
if w + 1 >= self.image_width:
w = self.image_width - 2
if h + 1 >= self.image_height:
h = self.image_height - 2
gradient = self.data[w + 1][h + 1][0] - self.data[w][h][0] + \
self.data[w + 1][h + 1][1] - self.data[w][h][1] + \
self.data[w + 1][h + 1][2] - self.data[w][h][2]
return gradient
def move_clusters(self):
for cluster in self.clusters:
cluster_gradient = self.get_gradient(cluster.h, cluster.w)
for dh in range(-1, 2):
for dw in range(-1, 2):
_h = cluster.h + dh
_w = cluster.w + dw
new_gradient = self.get_gradient(_h, _w)
if new_gradient < cluster_gradient:
cluster.update(_h, _w, self.data[_h][_w][0], self.data[_h][_w][1], self.data[_h][_w][2])
cluster_gradient = new_gradient
def assignment(self):
for cluster in self.clusters:
for h in range(cluster.h - 2 * self.S, cluster.h + 2 * self.S):
if h < 0 or h >= self.image_height: continue
for w in range(cluster.w - 2 * self.S, cluster.w + 2 * self.S):
if w < 0 or w >= self.image_width: continue
L, A, B = self.data[h][w]
Dc = math.sqrt(
math.pow(L - cluster.l, 2) +
math.pow(A - cluster.a, 2) +
math.pow(B - cluster.b, 2))
Ds = math.sqrt(
math.pow(h - cluster.h, 2) +
math.pow(w - cluster.w, 2))
D = math.sqrt(math.pow(Dc / self.M, 2) + math.pow(Ds / self.S, 2))
if D < self.dis[h][w]:
if (h, w) not in self.label:
self.label[(h, w)] = cluster
cluster.pixels.append((h, w))
else:
self.label[(h, w)].pixels.remove((h, w))
self.label[(h, w)] = cluster
cluster.pixels.append((h, w))
self.dis[h][w] = D
def update_cluster(self):
for cluster in self.clusters:
sum_h = sum_w = number = 0
for p in cluster.pixels:
sum_h += p[0]
sum_w += p[1]
number += 1
_h = sum_h / number
_w = sum_w / number
cluster.update(_h, _w, self.data[_h][_w][0], self.data[_h][_w][1], self.data[_h][_w][2])
def save_current_image(self, name):
image_arr = np.copy(self.data)
for cluster in self.clusters:
for p in cluster.pixels:
image_arr[p[0]][p[1]][0] = cluster.l
image_arr[p[0]][p[1]][1] = cluster.a
image_arr[p[0]][p[1]][2] = cluster.b
image_arr[cluster.h][cluster.w][0] = 0
image_arr[cluster.h][cluster.w][1] = 0
image_arr[cluster.h][cluster.w][2] = 0
self.save_lab_image(name, image_arr)
def iterate_10times(self):
self.init_clusters()
self.move_clusters()
for i in trange(10):
self.assignment()
self.update_cluster()
name = 'lenna_M{m}_K{k}_loop{loop}.png'.format(loop=i, m=self.M, k=self.K)
self.save_current_image(name)
"""
end
function slic_preprocess(self_filename::String,self_K::Int,self_M::Int)
p = py"SLICProcessor"(self_filename, self_K, self_M)
p.iterate_10times()
out = Vector{Any}(undef, p.mycount)
for i = 1 : p.mycount
out[i] = p.clusters[i].pixels
end
out
end
clusters = Main.MyModule.slic_preprocess("Lenna.png",200,40)
程序運行結果如圖
總結
以上是生活随笔為你收集整理的julia 调用python库_Julia调用Python实现超像素分割SLIC算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中赋值不正确的_python
- 下一篇: matlab您的安装可能需要执行其他配置