2016年8月31日 星期三

C++ 讀寫 CSV檔

在C++ 中讀寫 CSV的辦法

細節就看看 source code吧


#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main()
{
vector<double> matrix;
//readfile
fstream file;
file.open("read.csv");
string line
while (getline( file, line,'\n')) //讀檔讀到跳行字元
{
istringstream templine(line); // string 轉換成 stream
string data;
while (getline( templine, data,',')) //讀檔讀到逗號
{
matrix.push_back(atof(data.c_str())); //string 轉換成數字
}
}
file.close();
//writefile
file.open("write.csv");
for (int i=0;i<matrix.size();i++)
{
file << matrix[i]<<",";
}
file.close();
return 0;
}
view raw gistfile1.txt hosted with ❤ by GitHub

2016年4月13日 星期三

Non local means filter

同樣都是去掉雜訊的方法

non local 和 local filter 最大的區別在於

Local 方法是只和要周圍的點 (Research window 內) 的相似度做比較而決定權重

而 Non Local 方法則是另外在每個點的周圍定義一個區塊 (Patch window),

比較 Patch 與Patch 間的相似度而決定該patch 的中心點的權重

好處是比較能真實反映點與點間的相似度

數學結構與原理如下

Reference: http://www.ipol.im/pub/art/2011/bcm_nlm/?utm_source=doi

Source code (C++ with OPENCV API)


Mat showDepth1(480, 640, CV_8U, Scalar(0));
//non local filter
//Mat showDepth1;
//showDepth.copyTo(showDepth1);
//fastNlMeansDenoising(showDepth, showDepth, 3, 3, 11 );
int iheight=480;
int iwidth=640;
int patchwindow=1; // total window 2f+1
int researchwindow=3; // total window 2r+1
int sigma=5; // Noise parameter
int fh=0.4*sigma; // filter parameter
int sigma2=sigma*sigma;
int Maxnoisedst=9*sigma2; //Max noise signal
//tabulate the exp(-x)
double *exptable =new double[Maxnoisedst];
double fh2=fh*fh;
for (int x=0;x<=Maxnoisedst;x++)
exptable[x]=exp((-1*x)/(fh2));
// for each pixel x,y
for (int x=0; x < iwidth;x++)
{
for (int y=0; y < iheight;y++)
{
// Reduce the patch window if we meet the boundary
int rPW=min(min(patchwindow,min(x,y)) ,min(iheight-1-y,iwidth-1-x));
//Reduce the Research window if we meet the boundary
int imax=min(x+researchwindow,iwidth-1-rPW);
int imin=max(x-researchwindow,rPW);
int jmax=min(y+researchwindow,iheight-1-rPW);
int jmin=max(y-researchwindow,rPW);
double totalweight=0;
double maxweight=0;
double depthdata=0;
for (int i=imin;i<=imax ; i++) // sum all the point in the research window
{
for (int j=jmin; j<=jmax;j++)
{
if (i!=x || j!=y)
{
double weight=0;
double dist=0;
for (int ii=-rPW;ii<=rPW;ii++)// calculate the distance between the patch (x,y), (i,j)
{
for (int jj=-rPW;jj<=rPW;jj++)
{
int px=showDepth.at<uchar>(y+jj,x+ii);
int qx=showDepth.at<uchar>(j+jj,i+ii);
int dst=px-qx;
dist+=dst*dst;
}
}
// normalized distance
dist/=(2*rPW+1)*(2*rPW+1);
// max distance^2-2*sigma^2 of rPW , 0.0
dist=max(dist -2*sigma2,0.0);
if (dist>Maxnoisedst)
weight=0;
else
{
int temp=(int)floor((double)dist);
double weight1=exptable[temp];
double weight2=exptable[temp+1];
weight=weight1 +(weight2-weight1)*(dist-temp);
}
if (weight> maxweight) maxweight=weight;
totalweight+=weight;
int temp1=showDepth.at<uchar>(j,i);
depthdata+=temp1*weight;
}
}
}
int temp=showDepth.at<uchar>(y,x);
depthdata+=temp*maxweight;
totalweight+=maxweight;
if (totalweight > 1e-10)
showDepth1.at<uchar>(y,x)=(int) floor(depthdata/totalweight);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

2016年3月17日 星期四