PIE调用Python获得彩色直方图

        前段时间我一直在研究PIE SDK与Python的结合,因为在我的开发中,我想获取一张图片的统计直方图,虽然在SDK中有提供关于直方图的类接口(如IStatsHistogram 接口、HistogramStatDialog 类),但其中有些方法得到的结果数据是有些小问题的(已经向技术人员反应),所以打算自己写一个。

        我是通过PIE的官方博文(https://www.cnblogs.com/PIESat/p/10244229.html)进行研究的,使用的方法一:通过Main传参。在技术员姐姐的耐心指导下,用Python得到我想获得的直方图,再通过C#调用Python,最后成功获得直方图。

结果如下图所示:

  先打开一张栅格图片

       开发环境:vs2013 framework4、 python 3.7

通过Python中的这三个模块 PIL、numpy、matplotlib可以比较容易得到我想要的直方图,Python代码如下:

 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#代码如下:

   注意添加引用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         }            

有帮助的话,记得点个赞支持一下哦~
也欢迎各位评论,指点,交流

posted on 2019-07-03 15:43  隅谙公子  阅读(586)  评论(4编辑  收藏  举报