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")
系统中要安装python,如果使用Linux系统,如笔者的是ubuntu,系统默认安装的是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的功能介绍
欢迎补充!!!! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构 ...
- 图像分割:Python的SLIC超像素分割
图像分割:Python的SLIC超像素分割 1. 什么是超像素? 2. 为什么超像素在计算机视觉方面有重要的作用? 3. 简单线性迭代聚类(SLIC) 4. 效果图 5. 源码 参考 1. 什么是超像 ...
- python如何封装成可调用的库_在python中如何以异步的方式调用第三方库提供的同步API...
在关于asyncio的基本用法中提到,asyncio并不是多线程.在协程中调用同步(阻塞函数),都占用同一线程的CPU时间,即当前线程会被阻塞(即协程只会在等待一个协程时可能出让CPU,如果是普通函数 ...
- 导入python库linux_Linux下Python安装PyMySQL成功,但无法导入的问题
今天使用 Nginx 部署 Django应用.安装python库都显示成功安装. 尝试启动 uwsgi 服务,竟然报错 Traceback (most recent call last): File ...
- 利用Python库,把Python当PS用!
给大家带来一篇关于PIL的文章,PIL全称是Python Image Library,顾名思义,是用来做图像处理的.用这个库,可以实现很多PS里的效果,比如像图片模糊.寻找轮廓.边缘检测等等.我们来看 ...
- windows安装python库_Windows安装Python机器学习包
Python 虽然是一门脚本语言,但借助诸如 Numpy.Scipy 等功能强大的 package(包),如今 Python 在科学计算.机器学习.数据挖掘方面都有较为广泛的应用.本教程介绍如何在 W ...
- python什么模块动态调用链接库_Python调用C/C++动态链接库的方法详解
本文以实例讲解了Python调用C/C++ DLL动态链接库的方法,具体示例如下: 示例一: 首先,在创建一个DLL工程(本例创建环境为VS 2005),头文件: 1 2 3 4 5 6 7 8 9 ...
- python调用 matlab库_python调用matlab的搜索结果-阿里云开发者社区
2018python技术问答集锦,希望能给喜欢python的同学一些帮助 小编发现问答专区中有很多人在问关于python的问题,小编把这些问题汇总一下,希望能给喜欢python的大家一些启示和帮助 本 ...
- python库的调用三种方法_python中调用库的方法
python中调用库的方法 发布时间:2020-09-02 11:41:21 来源:亿速云 阅读:93 作者:小新 小编给大家分享一下python中调用库的方法,希望大家阅读完这篇文章后大所收获,下面 ...
最新文章
- (翻译)为你的MVC应用程序创建自定义视图引擎
- 在计算机领域做研究的一些想法-- 转载
- SAP Fiori Lead应用中Accept按钮显示隐藏的逻辑
- samba加入windows 2003域
- Python脚本实现启停app获取资源占比信息
- 小解系列-解决WebUploader在谷歌浏览器下弹出框打开慢,在Bootstrap模态框内部多次点击才能触发的问题
- JVM系列:生产环境参数实例及分析【生产环境实例增加中】
- 算法工程师书籍推荐——典藏版
- 奈奎斯特稳定性判据的步骤(含详细推导)
- 邮箱 手机号 格式要求
- EVA改进(点锐度算法)图像清晰度评价方法C++实现
- 关于学习的几个经典定律
- 总分公司之间的税务管理问题
- 与传统招聘方式相比,小程序招聘都有哪些优势?
- 微信多开_电脑PC端微信双开多开
- CCNA 初学(第一课)
- 幻影显示连接不到服务器,魂之幻影进不去怎么办 魂之幻影进不去解决方法
- linux 进阶命令笔记(12月26日)
- Ubuntu之五:如何使用Rhythmbox听歌?以及解决中文标签乱码问题
- 计算机前沿国际会议: 图灵测试测试题 ,人工智能得分:0分