工程代码拾遗

时间:2022-07-02 05:44:02

最近在阅读某工程文件时意外收获到的一些算法:

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.