活到老学到老

现学现卖

博客园 首页 新随笔 联系 订阅 管理
  34 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

2010年9月3日 #

var winshell = WScript.CreateObject("WScript.Shell");

function shutdown30()
{
 WScript.Sleep(1000*60*30);
/*winshell.SendKeys("^+{ESC}");
WScript.Sleep(3000);
winshell.SendKeys("%f");*/
 winshell.SendKeys("^{ESC}{UP}~{DOWN}~");
}

if(winshell.Popup("Are you sure?",30,"Shutdown in 30 min",4+32)!=7)
{
 shutdown30(); 
}

 

如果要取消关机,打开任务管理器,终止windows script host的进程。

posted @ 2010-09-03 19:46 John Rambo 阅读(32) 评论(0) 编辑

2010年5月25日 #

把一幅图片中颜色相近的像素的分为一组,并把该组像素的颜色替换成组均值。

搞一幅比较小的bmp图像,例如128*128以内,要不然运行时间有点长。

用下面代码把像素信息导出成txt:

 

代码
   static void BmpToTxt(string path)
        {
            
using (StreamWriter sw = new StreamWriter("result.txt"))
            {
                var img 
= new Bitmap(path);
                sw.WriteLine(
"dimension:{0}x{1}", img.Width, img.Height);
                
for (int i = 0; i < img.Width; i++)
                {
                    
for (int j = 0; j < img.Height; j++)
                    {
                        var pix 
= img.GetPixel(i, j);
                        sw.WriteLine(
"{0} {1} {2}", pix.R, pix.G, pix.B);

                    }
                }
            }
        }

 

复制输出的文件为result.data,去掉第一行(图像尺寸信息)。

然后用下面的scilab代码对该数据进行meanshift:

 

//------------helper functions-----------------
function [feature,cls] = readdatapoint(mat,i,fieldcount,classification) //put the ith row of mat into a list
if classification == 0 then
  feature = zeros(fieldcount,1);
  for j=1:fieldcount
    feature(j) = mat(i,j); 
  end
  cls = -1;//not used for clustering
else
  feature = zeros(fieldcount-1,1);
  for j=1:fieldcount-1
    feature(j) = mat(i,j); 
  end
  cls = mat(i,fieldcount);
end
endfunction


function data = retrievedata(filepath, strfamat,classification)
  if ("string" <> typeof(filepath)) then
     error("filepath must be a string.");
  end
  fid = mopen(filepath, "r");
  if(fid == -1)
    error("can not open data file:" + filepath);
  end

  records = mfscanf(-1, fid,strfamat);
  recsize = size(records);
  reccount = recsize(1);
  fieldcount = recsize(2);
  data = list();
  for i=1:reccount
    [feature,cls] = readdatapoint(records,i,fieldcount, classification);
    data($+1) = struct("id",i,"feature",feature,"cls",cls,"comment",1);
  end
  mclose(fid);
endfunction


function setsd() //reset random seed
  dt = getdate();
  newseed = dt(3) * dt(9)^2 + dt(10);
  grand("setsd", newseed);
endfunction

//------------core functions-----------------
//compute mean shift, require global variable datapoints,n,flen
function m=calcms(y,h)
  upper = zeros(flen,1);
  lower = 0;
  for i=1:n
    xi = datapoints(i).feature;
    smallkarg = (y-xi)/h;
    temp = -exp(-sum(smallkarg .* smallkarg)/2)/2;
    lower = lower + temp;
    upper = upper + temp*xi;
  end
  m = lower^-1*upper-y;
endfunction

//stop if the distance between two yi and yi+1 < threshold
function y=converge(xi,h,threshold)
  y = xi;
  oldy = y + 10000;
  while norm(y-oldy) > threshold
    oldy = y;
    y = y + calcms(y,h);
  end
endfunction

//require global var datapoints
function clusters = runmeanshift(h,threshold,aggregatedistance)
  pointcount = size(datapoints);
  clusters = list();

  for i=1:pointcount
    pcount = i;
    y = converge(datapoints(i).feature,h,threshold);
    clustercount = size(clusters);
    clusterid = 0; //haven't put in a cluster
    clusterdistance = aggregatedistance; //a variable help to select the best cluster
    for j=1:clustercount
      distance = norm(clusters(j).len^-1*clusters(j).sigmay -y);//distance to cluster center
      if distance <= clusterdistance then //within cluster radius
          clusterdistance = distance;
          clusterid=j;
      end
    end
    if clusterid == 0 then //put in a new cluster
      clusters($+1) = struct("sigmay",0, "len", 1, "elements",list(),"cls",-1);
      clusters($).sigmay = y;
      clusters($).elements($+1) = struct("id",i,"y",y);
    else
      clusters(clusterid).sigmay = clusters(clusterid).sigmay + y;
      clusters(clusterid).len = clusters(clusterid).len + 1;
      clusters(clusterid).elements($+1) = struct("id",i,"y",y);
    end
  end
endfunction


//require global clusters and datapoints
function createbmptxt(filepath)
  pointnumber = size(datapoints);
  pixellist = zeros(pointnumber,3);
  clustercount = size(clusters);
  for i=1:clustercount
    clusterlength = clusters(i).len;
    clustermean = clusters(i).len^-1 * clusters(i).sigmay;
    clustermean = clustermean';
   
    for j=1:clusterlength
      pixellist(clusters(i).elements(j).id,:) = clustermean;
    end
  end
  fid = mopen(filepath, "w");
  if(fid == -1)
    error("can not open data file:" + filepath);
  end
  for i=1:pointnumber
    mfprintf(fid,"%d %d %d\n",pixellist(i,:));  
  end
  mclose(fid);
endfunction


stacksize('max');

//image process code----------------------
function testimg()
  datapoints = retrievedata("D:\result.data","%d %d %d",0);
  flen = size(datapoints(1).feature);
  flen = flen(1);
  n = size(datapoints);
  clusters = runmeanshift(1,50,50);
  createbmptxt("D:\shiftedimg.txt");
endfunction
testimg();

 

  可以调整runmeanshift函数的第一个参数来改变图像效果,其越大最后生成图片的颜色数就越小。

 上面的代码用C#也完全可以实现,而且用C#可以使用Brahma进行GPU加速。Scilib里矩阵操作很方便,但性能好象就有点问题。

在shiftedimg.txt的第一行把图像的尺寸信息再插回去,格式和result.txt中的一样。然后用下面的代码把此文本文件转换回bmp:

 

代码
static void TxtToBmp(string path)
        {
            
using (StreamReader sr = new StreamReader(path))
            {
                
string line = sr.ReadLine();
                
int ind1 = line.IndexOf(":");
                
int ind2 = line.IndexOf("x");
                
int width = int.Parse(line.Substring(ind1 + 1, ind2 - ind1 - 1));
                
int height = int.Parse(line.Substring(ind2 + 1));
                Bitmap bmp 
= new Bitmap(width, height);
                
int linecount = 0;
                
while ((line = sr.ReadLine()) != null)
                {
                    
string[] parts = line.Split(' ');

                    
int r = (int)Math.Round(double.Parse(parts[0]));
                    
int g = (int)Math.Round(double.Parse(parts[1]));
                    
int b = (int)Math.Round(double.Parse(parts[2]));

                    bmp.SetPixel(linecount 
/ height, linecount % height, Color.FromArgb(r, g, b));
                    linecount
++;
                }
                bmp.Save(
"result.bmp");
            }
        }

 

 

大功告成, 最后看一下对比:

  ->

 

posted @ 2010-05-25 12:24 John Rambo 阅读(820) 评论(0) 编辑

该算法用于近似计算离散信道的容量。参数pyx是信道的转移概率矩阵p(y|x)。

 

 

 

 

 

posted @ 2010-05-25 11:41 John Rambo 阅读(91) 评论(0) 编辑

 
QuickSort是一种递归排序的算法,每一次迭代的过程是,
从序列中选出一个元素midvalue,把所有比它小的放在它的前面,大于等于它的放在它的后面。
然后再对前半个序列和后半个序列分别做同样的事。
参数start和endlist中要进行排序的子列的起始元素下标和结束元素下标。
以下实现中,一趟排序的过程为:
选第一个元素的值作为midvalue;
border是一个下标变量,意义是border右边的元素大于等于midvalue,此时border=1;
依次判断后面的所有元素,如果大于等于midvalue,就应当插入到border的右边,同时border+1;
当所有元素判断完后,是这么个情况:
[midvalue] [values < midvalue] [values >= than midvalue]
                     border->|
此时border是最后一个小于midvalue的元素的下标。
最后把第一个元素和border一交换,大功告成。
代码
static void Main(string[] args)
        {
            
//test1();
            
//List<int> li = new List<int>();
            
//li[18] = 99;
            
//test2();

            
//int[] list = { 5, 4, 1, 3, 2, 7, 6 };
            
//int[] list = {6,5};
            
//int[] list = { 5,5,5};
            
//int[] list = {7,6,5,4,3,2,1 };
            
//int[] list = { 7,6,6,5,5,4,4,3,3,2,2,1,1};
            
//int[] list = { 6, 5, 1, 2, 7, 3, 6, 9, 8 };
            int[] list = new int[200];
            Random rand 
= new Random();
            
for (int i = 0; i < 200; i++)
            {
                list[i] 
= rand.Next(120);
            }
            QuickSort(list, 
0, list.Length - 1);

            Console.Read();


        }

        
private static void QuickSort(int[] list, int start, int end)
        {
            
if (start >= end) return;
            
int midvalue = list[start];//为了性能
            int border = start;//border右边是大于等于midvalue的第一个元素
            for (int i = start + 1; i <= end; i++)
            {
                
if (list[i] < midvalue) //需要放到border左边
                {
                    Swap(list, i, 
++border);
                }
            }
            Swap(list, border, start);
            QuickSort(list, start, border 
- 1);
            QuickSort(list, border 
+ 1, end);

        }

        
private static void Swap(int[] list, int i, int j)
        {
            
int temp = list[i];
            list[i] 
= list[j];
            list[j] 
= temp;
        }

 

posted @ 2010-05-25 11:22 John Rambo 阅读(308) 评论(0) 编辑

2010年2月20日 #

摘要: PermutationGen用来枚举{1,...,n}的所有全排列。D类用来计算行列式,只能对数值进行计算。代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->usingSystem;usingSystem.Collections.Generic;us...阅读全文
posted @ 2010-02-20 13:58 John Rambo 阅读(380) 评论(0) 编辑

2010年2月6日 #

摘要: 不错的MIDL的语法教程的翻译阅读全文
posted @ 2010-02-06 22:06 John Rambo 阅读(905) 评论(1) 编辑

2009年8月7日 #

最近要做Assignment, 规定必须能在Unix上编译,所以想装一个Redhat9的虚拟机。
问题是怎么和主机交换数据。现解决方案如下:
虚拟机设置Networking->NAT
Redhat中查看Device Browser,找到网卡型号为DEC 211140
在System Tools里面选Internet Wizzard,配置网络,类似windows
最后激活网络

网络可用后,还可以用mout -t smb -o在主机和虚拟机之间建立一个共享文件夹,可读可写。
还可以通过虚拟floopy传递文件,不过这个有1.38m的限制,而且还要通过一个DOS虚拟机把文件拷回主机...比较麻烦

这样交换数据的问题就应该是解决了。实际上都不用交换了,直接通过linux把assignment交上就完了...
posted @ 2009-08-07 10:14 John Rambo 阅读(119) 评论(0) 编辑

2009年7月31日 #

摘要: 传递给基元类型的ToString(…IFormatterProvider…)应该是CultureInfo或者是NumberFormatInfo,最终只能是NumberFormatInfo。有ICustomFormatter这么个接口,但是没有一般性的IFormatter接口。因此一般来说IFormatterProvider实例返回的对象仅仅是一个普通的Object,这个Ob...阅读全文
posted @ 2009-07-31 20:11 John Rambo 阅读(36) 评论(0) 编辑

2008年12月18日 #

摘要: Because it was a nightmare.[代码][代码]测试代码:阅读全文
posted @ 2008-12-18 18:31 John Rambo 阅读(65) 评论(0) 编辑

2008年12月10日 #

摘要: 同样一段c++代码生成的汇编指令可能会不一样。有多种原因,例如编译器、调用约定或者底层平台。今天要分析的是cdecl在x86机器上用visual c++ 2005上的编译结果。首先需要设置一下项目配置以得到从源代码生成的汇编代码。项目属性->配置属性->c/c++->输出文件->汇编输出 = Assembly With Source Code (/FAs)。要被编译的源文件...阅读全文
posted @ 2008-12-10 15:40 John Rambo 阅读(531) 评论(7) 编辑