最近在閱讀某工程文件時意外收穫到的一些算法:
1.辛普森積分公式
</pre><pre name="code" class="cpp">float Simpson1(const float a, const float b, const float eps, const float temp)
{
int n,k;
float h,t1,t2,s1,s2,ep,ep2,p,x;
n = 1;
h = b-a;
t1 = h*( Lbbr(temp, a) + Lbbr(temp, b) )/2.0;//此處以及下面lbbr是黑體輻射公式,如果需要計算其他積分請替換lbbr
s1 = t1;
ep = eps+1.0;
ep2 = eps+2.0;
while (ep >= eps)
{
p = 0.0;
for (k=0; k<=n-1; k++)
{
x = a + (k+0.5)*h;
p = p + Lbbr(temp, x);
}
t2 = (t1+h*p)/2.0;
s2 = (4.0*t2-t1)/3.0;
ep = fabs(s2-s1);
t1 = t2;
s1 = s2;
if (ep2<ep)
{
break;
}
ep2 = ep;
n = n+n;
h = h/2.0;
}
return(s2);
}
2.邊緣檢測(Roborts模板)
for(int index = 0, i=1; i< length-1; ++i )
{
for(int j=1; j<width-1; ++j )
{
index = i*width+j;
r0=Integrate[index]-Integrate[index+width+1];
r1=Integrate[index+1]-Integrate[index+width];
tmpbianyuan[i*(width-2)+j]=sqrt(float(r0*r0+r1*r1));//提取出的邊緣點。
}
}
3.回型模糊(25個點)
這個算法代碼實現中有令人迷茫的四個if,加上莫名的註釋,一開始都沒有看懂是在幹嘛
Average( const MY_POINT& point, const int R1, const int R2 )//R1,R2是2,point是上一部計算得出的邊緣點
{
<span style="white-space:pre"> </span>for ( int i=0; i<=R1; ++i )//由內向外按回型過渡,以當前點爲第0層
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for ( int j=-i; j<=i; ++j )//平均每一層
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//四邊信息
<span style="white-space:pre"> </span>float t=0,e=0,tg=0;
<span style="white-space:pre"> </span>int num=0;
<span style="white-space:pre"> </span>if ( ((point.y-i)*m_nWidth+(point.x+j) >=0) && ((point.y-i)*m_nWidth+(point.x+j) < m_nWidth*m_nHeight) )//上邊
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t=0;e=0;tg=0;num=0;
<span style="white-space:pre"> </span>for ( int m=-R2; m<=R2; ++m )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for ( int n=-R2; n<=R2; ++n)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if ( ((point.y-i+m)*m_nWidth+(point.x+j+n) >=0) && ((point.y-i+m)*m_nWidth+(point.x+j+n) < m_nWidth*m_nHeight) )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t += m_radiance[(point.y-i+m)*m_nWidth+(point.x+j+n)];
<span style="white-space:pre"> </span>++num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>m_radiance[(point.y-i)*m_nWidth+(point.x+j)] = t/num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if ( ((point.y+i)*m_nWidth+(point.x+j) >=0) && ((point.y+i)*m_nWidth+(point.x+j) < m_nWidth*m_nHeight) )//下邊
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t=0;e=0;tg=0;num=0;
<span style="white-space:pre"> </span>for ( int m=-R2; m<=R2; ++m )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for ( int n=-R2; n<=R2; ++n)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if ( ((point.y+i+m)*m_nWidth+(point.x+j+n) >=0) && ((point.y+i+m)*m_nWidth+(point.x+j+n) < m_nWidth*m_nHeight) )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t += m_radiance[(point.y+i+m)*m_nWidth+(point.x+j+n)];
<span style="white-space:pre"> </span>++num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>m_radiance[(point.y+i)*m_nWidth+(point.x+j)] = t/num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if ( ((point.y+j)*m_nWidth+(point.x-i) >=0) && ((point.y+j)*m_nWidth+(point.x-i) < m_nWidth*m_nHeight) )//左邊
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t=0;e=0;tg=0;num=0;
<span style="white-space:pre"> </span>for ( int m=-R2; m<=R2; ++m )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for ( int n=-R2; n<=R2; ++n)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if ( ((point.y+j+m)*m_nWidth+(point.x-i+n) >=0) && ((point.y+j+m)*m_nWidth+(point.x-i+n) < m_nWidth*m_nHeight) )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t += m_radiance[(point.y+j+m)*m_nWidth+(point.x-i+n)];
<span style="white-space:pre"> </span>++num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>m_radiance[(point.y+j)*m_nWidth+(point.x-i)] = t/num;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if ( ((point.y+j)*m_nWidth+(point.x+i) >=0) && ((point.y+j)*m_nWidth+(point.x+i) < m_nWidth*m_nHeight) )//右邊
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t=0;e=0;tg=0;num=0;
<span style="white-space:pre"> </span>for ( int m=-R2; m<=R2; ++m )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for ( int n=-R2; n<=R2; ++n)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if ( ((point.y+j+m)*m_nWidth+(point.x+i+n) >=0) && ((point.y+j+m)*m_nWidth+(point.x+i+n) < m_nWidth*m_nHeight) )
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>t += m_radiance[(point.y+j+m)*m_nWidth+(point.x+i+n)];
<span style="white-space:pre"> </span>++num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>m_radiance[(point.y+j)*m_nWidth+(point.x+i)] = t/num;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>
}
4.tiff文件讀取/保存
首先,存取都依賴於開源庫libtiff,w’in下生成libtiff方法見文章末尾,雖然是英文但是關鍵命令都單獨列出來了,此處要注意一點是:開源庫由於穩定性原因,強烈建議使用成熟的版本,不清楚的就是用數字較小的就行,因爲本人在調用的時候發現有的版本會出現莫名的錯誤無法編譯,但是換一個版本的庫文件就沒問題;
讀寫代碼目前我忘了放到哪裏,暫時可以參考:
IBM官方指導(可以下到read和write文件)
http://blog.csdn.net/augusdi/article/details/10397179
http://darkranger.no-ip.org/archives/v5/document/develop/libtiff_tutorial.htm#1
以上,
目前就這麼多,想起來再寫
tiff庫生成方法:
With MicrosoftVisual C++ installed, and properly configured for commandline use (you willlikely need to source VCVARS32.BAT in AUTOEXEC.bAT or somewhere similar) youshould be able to use the provided makefile.vc.
The sourcepackage is delivered using Unix line termination conventions, which work withMSVC but do not work with Windows ‘notepad’. If you use unzip from the Info-Zippackage, you can extract the files using Windows normal line terminationconventions with a command similar to:
unzip -aa -a tiff-4.0.0.zip
By defaultlibtiff expects that a pre-built zlib and jpeg library are provided by theuser. If this is not the case, then you may edit libtiff\tiffconf.h using atext editor (e.g. notepad) and comment out the entries for JPEG_SUPPORT, PIXARLOG_SUPPORT,and ZIP_SUPPORT. Ignore the comment at the top of the file which says that ithas no influence on the build, because the statement is not true for Windows.However, by taking this approach, libtiff will not be able to open some TIFFfiles.
To build usingthe provided makefile.vc you may use:
C:\tiff-4.0.0> nmake /f makefile.vcclean
C:\tiff-4.0.0> nmake /f makefile.vc
or (the hardway)
C:\tiff-4.0.0> cd port
C:\tiff-4.0.0\port> nmake /fmakefile.vc clean
C:\tiff-4.0.0\port> nmake /fmakefile.vc
C:\tiff-4.0.0> cd ../libtiff
C:\tiff-4.0.0\libtiff> nmake /fmakefile.vc clean
C:\tiff-4.0.0\libtiff> nmake /fmakefile.vc
C:\tiff-4.0.0\libtiff> cd ..\tools
C:\tiff-4.0.0\tools> nmake /fmakefile.vc clean
C:\tiff-4.0.0\tools> nmake /fmakefile.vc
This will buildthe library file libtiff\libtiff\libtiff.lib. This can be used in Win32programs. You may want to adjust the build options before start compiling. Allparameters contained in the nmake.opt file.This is a plain text file you canopen with your favorite text editor.
The makefilealso builds a DLL (libtiff.dll) with an associated import library(libtiff_i.lib). Any builds using libtiff will need to include theLIBTIFF\LIBTIFF directory in the include path.
Thelibtiff\tools\makefile.vc should build .exe’s for all the standard TIFF toolprograms.