前段时间我一直在研究PIE SDK与Python的结合,由于在个人开发中,我想获取一张图片的统计直方图,虽然在SDK中有提供关于直方图的类接口(如IStatsHistogram 接口、HistogramStatDialog 类),但其中有些方法获得的结果数据是有些小问题的(已经向技术人员反应),因此打算本身写一个。html
我是经过PIE的官方博文(https://www.cnblogs.com/PIESat/p/10244229.html)进行研究的,使用的方法一:经过Main传参。在技术员姐姐的耐心指导下,用Python获得我想得到的直方图,再经过C#调用Python,最后成功得到直方图。python
结果以下图所示:算法
先打开一张栅格图片spa
开发环境:vs2013 framework四、 python 3.7code
经过Python中的这三个模块 PIL、numpy、matplotlib能够比较容易获得我想要的直方图,Python代码以下:htm
1 #-*- coding: UTF-8 -*- 2 3 import sys 4 from PIL import Image 5 import numpy as np 6 import matplotlib.pyplot as plt 7 8 #索引传入的图片地址 9 aaa=sys.argv[1] 10 11 src=Image.open(aaa) 12 r,g,b=src.split() 13 plt.figure("彩色直方图") 14 ar=np.array(r).flatten() 15 plt.hist(ar, bins=256, density=1,facecolor='r',edgecolor='r') 16 ag=np.array(g).flatten() 17 plt.hist(ag, bins=256, density=1, facecolor='g',edgecolor='g') 18 ab=np.array(b).flatten() 19 plt.hist(ab, bins=256, density=1, facecolor='b',edgecolor='b') 20 21 #显示直方图窗口 22 plt.show()
C#代码以下:blog
注意添加引用System.Threading.Tasks索引
1 private void 外部调用ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 //启动一个进程 4 System.Diagnostics.Process p = new System.Diagnostics.Process(); 5 p.StartInfo.UseShellExecute = false; 6 p.StartInfo.RedirectStandardOutput = true;//重定向输出 7 p.StartInfo.RedirectStandardError = true; 8 //启动python.exe 9 p.StartInfo.FileName = @"G:\pythonOnhere\python.exe";//本身安装python.exe的路径 10 p.StartInfo.CreateNoWindow = true; 11 12 string m_InputFile1 = m_InputFile.Replace(@"\", "/");//已经打开的栅格文件路径,因为python识别的路径格式和C#有一点区别,注意转换格式 13 p.StartInfo.Arguments = @"E:\PIE开发\2.py" + " " + m_InputFile1; //构造参数,将算法文件(.py)和算法参数一并传入,以空格间隔 14 p.EnableRaisingEvents = true; 15 p.Start(); 16 }
有帮助的话,记得点个赞支持一下哦~
也欢迎各位评论,指点,交流接口