PIE call Python returns the histogram matrix array

  Some time ago I studied PIE SDK combined with the Python, Python has been successfully invoked by C #, to get a color histogram. (The essay has to share: https: //www.cnblogs.com/yuan1120/p/11126869.html)

  Based on the success in the last, this time I intend to call Python from C #, returns an array of the histogram matrix, convenient for calculating the value of various features.

The results shown below:

  First open a raster picture

 

  Development environment: vs2013 framework4, python 3.7

Through these three modules in PIL Python, numpy, matplotlib matrix array can be obtained histogram, Python code is as follows:

 1 # -*- coding: utf-8 -*-
 2 import sys
 3 from PIL import Image
 4 from PIL import ImageDraw
 5 import numpy as np
 6 import matplotlib.pyplot as plt
 7 
 8 #索引传入图片地址
 9 aaa=sys.argv[1]
10 im = Image.open(aaa)
11 r,g,b=im.split()
12 
13 width, height = im.size
14 rpix = r.load()
15 gpix = g.load()
16 bpix = b.load()
17 his1 = [0]*768
18 for aw in range(width):
19     for ah in range(height):
20         p_r = rpix[aw,ah]
21         p_g = gpix[aw,ah]+256
22         p_b = bpix[aw,ah]+512
23         his1[p_r] = his1[p_r]+1
24         his1[p_g] = his1[p_g]+1
25         his1[p_b] = his1[p_b]+1
26 
27 a=[str(i) for i in his1]
28 b=' '.join(a)    
29 #以字符串形式返回
30 print(b)

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.Exited += p_Exited;
 6 
 7                p.StartInfo.UseShellExecute = false;
 8                p.StartInfo.RedirectStandardOutput = true;//重定向输出
 9                p.StartInfo.RedirectStandardError = true;
10                //启动python.exe
11                p.StartInfo.FileName = @"G:\pythonOnhere\python.exe";//自己安装python.exe的路径
12                p.StartInfo.CreateNoWindow = true;            
13   
14                string m_InputFile1 = m_InputFile.Replace(@"\", "/");//已经打开的栅格文件路径,由于python识别的路径格式和C#有一点区别,注意转换格式
15                p.StartInfo.Arguments = @"E:\PIE开发\7.py" + " " + m_InputFile1; //构造参数,将算法文件(.py)和算法参数一并传入,以空格间隔
16                p.EnableRaisingEvents = true;
17                p.Start();
18    
19                //注意,如果想从python中返回获取字符串,一定要加入下面三句
20                p.BeginOutputReadLine();
21                p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
22                p.WaitForExit();
23              }    

  进程退出时,启动

        private void p_Exited(object sender, EventArgs e)
        {
            System.Diagnostics.Process p = sender as System.Diagnostics.Process;
            MessageBox.Show(histo);
         }   

        //输出打印的信息
        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                histo = e.Data + Environment.NewLine;
            }
        }     

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

 

Guess you like

Origin www.cnblogs.com/yuan1120/p/11417064.html