public Form1()
{
InitializeComponent();
//////////////////////////////////////////////////////////////////////////
Bitmap BitmapLight = new Bitmap(@"C:\Documents and Settings\Administrator\桌面\BmpLight.bmp"); init(BitmapLight);
this.pictureBox1.Image = BitmapLight; }
public static bool init(Bitmap bmp)
{
........................
int t = 0;
t = max( a, b );
.......................
return true;
}
public static int max( int a, int b )
{
...........................
}调试的时候进入max方法,出来直接就跳到this.pictureBox1.Image = BitmapLight;而不回到t = max( a, b );中去请问怎么解决这个问题?调试c#方法调用
怎么解决啊?
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
那你要把public static int max( int a, int b )方法详细贴出来,你方法的逻辑是没问题的
怎么解决啊?你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
那你要把public static int max( int a, int b )方法详细贴出来,你方法的逻辑是没问题的using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//////////////////////////////////////////////////////////////////////////
Bitmap BitmapLight = new Bitmap(@"C:\Documents and Settings\Administrator\桌面\BmpLight.bmp"); int t = init(BitmapLight);
//this.pictureBox1.Image = BitmapLight;
label1.Text = t.ToString();
} public static int init(Bitmap bmp)
{
int t = 10;
try
{
//List<int[]> centers=new List<int[]>();
int[,] centers = new int[10,3]; int gate = 100;
int count = 0;
while (true)
{ bool touchCircle = false; //没有接触到圆
int point = 0;
bool foundPoint = false; //未找到顶点
int hposition, d, ox, oy, j;
//下面的循环找顶点
for (j = 5; j < bmp.Height - 5; j++) //之所以要留出5像素的框是因为边界颜色是淡灰色的,容易被误认为是圆内
{
for (int i = 5; i < bmp.Width - 5; i++) //先横向扫描
{
if (inFoundCircle(centers, i, j)) //如果这个点在已经发现的圆内了,那就没必要判断了,我们要找的是圆的定点
{
continue;
}
else
{
if (getPixelByGray(bmp, i, j) > gate && !touchCircle) //圆内了,而且是首次进入
{
point = i; //理论上圆的顶点是个点,但是图片中圆的顶部可能是条线,我们算它的中点作为顶点,所以要记收尾坐标
touchCircle = true;
}
else if (getPixelByGray(bmp, i, j) < gate && touchCircle) //出圆了
{
point = (point + i - 1) / 2; //顶点横坐标
foundPoint = true;
hposition = j; //顶点纵坐标
}
}
if (foundPoint)
{
break;
}
}
if (foundPoint)
{
break;
}
}
if (foundPoint) //找到顶点了,开始找直径
{
for (int i = j; i < bmp.Height - 5; i++) //后纵向扫描
{
if (getPixelByGray(bmp, point, i) < gate) //出圆了!
{
d = i - j - 1; //直径
ox = point; //圆心横坐标
oy = (i + j - 1) / 2; //圆心纵坐标
centers[count,0] = ox; //保存圆心的横坐标
centers[count,1] = oy; //保存圆心的纵坐标
centers[count,2] = d; //保存圆的直径
count++;
break;
}
}
}
else
{
break;
}
}
drawBoxes(bmp, centers);
return count; }
catch (System.Exception ex)
{
return t;
}
} public static bool inFoundCircle(int[,] centers, int x, int y)
{
for(int i=0; i<centers.Length; i++)
{
if (((x - centers[i,0]) * (x - centers[i,0]) + (y - centers[i,1]) * (y - centers[i,1])) * 4 <= (centers[i,2] + 6) * (centers[i,2] + 6)) //为了效率,避免了除法和开方,原公式为计算点到圆心距离与半径大小,这个经过了变形
{
return true;
}
}
return false;
} public static int getPixelByGray(Bitmap bmp, int x, int y)
{
Color color = bmp.GetPixel(x, y);
return color.R + color.G + color.B; //算平均值获得灰度,为了效率不除3了,反正只判断大小
} public static void drawBoxes(Bitmap bmp, int[,] centers)
{
Graphics g = Graphics.FromImage(bmp); for (int i = 0; i < centers.Length; i++)
{
g.DrawRectangle(Pens.Red, centers[i,0] - centers[i,2] / 2 - 5, centers[i,1] - centers[i,2] / 2 - 5, centers[i,2] + 10, centers[i,2] + 10);
}
} }
}
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
那你要把public static int max( int a, int b )方法详细贴出来,你方法的逻辑是没问题的全部代码 您慢慢看 目的是将近似黑色背景图片中的几个白色光斑用矩形框出
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
那你要把public static int max( int a, int b )方法详细贴出来,你方法的逻辑是没问题的全部代码 您慢慢看 目的是将近似黑色背景图片中的几个白色光斑用矩形框出
一眼看过去public static bool init(Bitmap bmp)
{
........................
int t = 0;
t = max( a, b );
.......................
return true;
}没有你说的这句 t = max( a, b );还有你说max(a,b)不返回值,重点的static int max( int a, int b )方法你也没有贴出来
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
那你要把public static int max( int a, int b )方法详细贴出来,你方法的逻辑是没问题的全部代码 您慢慢看 目的是将近似黑色背景图片中的几个白色光斑用矩形框出
一眼看过去public static bool init(Bitmap bmp)
{
........................
int t = 0;
t = max( a, b );
.......................
return true;
}没有你说的这句 t = max( a, b );还有你说max(a,b)不返回值,重点的static int max( int a, int b )方法你也没有贴出来
我只是用max打个比方,你看源代码就行了 运行到54行if (inFoundCircle(centers, i, j)) 就跳出了
怎么解决?
怎么解决啊?
你没看你自己写的逻辑么?static int max( int a, int b )这个方法完了就return true了然后就是下面这句this.pictureBox1.Image = BitmapLight;了。你是想达到什么效果
他没有运行return true,而且return true前面还有很多代码都没运行,就直接跳走了,就是说没有把max得到的数值返回到t中
那你要把public static int max( int a, int b )方法详细贴出来,你方法的逻辑是没问题的全部代码 您慢慢看 目的是将近似黑色背景图片中的几个白色光斑用矩形框出
一眼看过去public static bool init(Bitmap bmp)
{
........................
int t = 0;
t = max( a, b );
.......................
return true;
}没有你说的这句 t = max( a, b );还有你说max(a,b)不返回值,重点的static int max( int a, int b )方法你也没有贴出来
我只是用max打个比方,你看源代码就行了 运行到54行if (inFoundCircle(centers, i, j)) 就跳出了
int[,] centers = new int[10,3];你自己定义的数组都没有值,你想操作什么?