我写了一段需要优化的代码.只是想与社区核实,看看该代码是否确实是最佳的.它填充了Hough变换的累加器.我实际上只是复制粘贴了OpenCV库中的大部分代码.谢谢!
int i,j,n,index;
for (i = 0;i<numrows;i++)
{
for (j = 0;j<numcols;j++)
{
if (img[i*numcols + j] == 100)
{
for (n = 300;n<600;n++)
{
index = cvRound(j*tabCos[n] + i * tabSin[n]) + (numrho-1)/2;
accum[(n+1) * (numrho+2) + index+1]++;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一些图像处理代码并使用C#进行低级像素操作.每隔一段时间就会发生一次accessViolationException.
有几种解决这个典型问题的方法,有些认为代码应该是健壮编写的,以便没有访问冲突异常,并且据我尝试,应用程序正常但是我想添加一个try catch,以便iff的东西是要发生这种情况,应用程序不会以太丑陋的方式失败.
到目前为止,我已经提供了一些示例代码来测试它
unsafe
{
byte* imageIn = (byte*)img.ImageData.ToPointer();
int inWidthStep = img.WidthStep;
int height = img.Height;
int width = img.Width;
imageIn[height * inWidthStep + width * 1000] = 100; // make it go wrong
}
Run Code Online (Sandbox Code Playgroud)
当我试着抓住这个陈述时,我仍然得到一个例外.有没有办法捕获不安全块中生成的异常?
编辑:如下所述,除非通过将此属性添加到函数并添加"using System.Runtime.ExceptionServices"来显式启用它们,否则不再处理此类型的异常.
[HandleProcessCorruptedStateExceptions]
public void makeItCrash(IplImage img)
{
try
{
unsafe
{
byte* imageIn = (byte*)img.ImageData.ToPointer();
int inWidthStep = img.WidthStep;
int height = img.Height;
int width = img.Width;
imageIn[height * inWidthStep + width * 1000] = 100; // to make it crash …Run Code Online (Sandbox Code Playgroud) 在这段代码中,似乎睡眠发生在隐藏按钮之前.这是为什么?
-(void)buttonPressed{
NSLog(@"Button pressed!");
button.hidden=true;
sleep(rand()%10);
NSLog(@"out of sleep!");
}
Run Code Online (Sandbox Code Playgroud)