Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
ReelCounter
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 8511f266
由
SK
编写于
2019-04-12 09:58:21 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
距离变换找圆法和与料盘中心距离相结合统计数量,
1 个父辈
2671fba1
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
207 行增加
和
24 行删除
AccImage/AccImage.csproj
AccImage/ImageUtil.cs
AccImage/SplitItem.cs
Demo/MainForm.cs
Demo/MainForm.designer.cs
AccImage/AccImage.csproj
查看文件 @
8511f26
...
@@ -64,6 +64,7 @@
...
@@ -64,6 +64,7 @@
<ItemGroup>
<ItemGroup>
<Compile Include="ImageUtil.cs" />
<Compile Include="ImageUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SplitItem.cs" />
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml" />
<Content Include="FodyWeavers.xml" />
...
...
AccImage/ImageUtil.cs
查看文件 @
8511f26
此文件的差异被折叠,
点击展开。
AccImage/SplitItem.cs
0 → 100644
查看文件 @
8511f26
using
OpenCvSharp
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
AccImage
{
public
class
SplitItem
{
public
bool
isEnd
=
false
;
public
double
avgRadius
=
0
;
public
int
centerX
=
0
;
public
int
centerY
=
0
;
public
double
currentMaxRadius
=
0
;
public
List
<
Circle
>
circles
=
new
List
<
Circle
>();
/// <summary>
/// 一遍结束后调用
/// </summary>
public
void
calOneItem
(
double
theFixRadius
=-
1
)
{
//该半径是否有效
bool
isValid
=
false
;
if
(
avgRadius
==
0
)
{
isValid
=
true
;
}
else
{
//与平均半径差值在10%以内,认为OK
double
diff
=
avgRadius
-
currentMaxRadius
;
if
(
diff
/
avgRadius
<=
0.2
)
{
isValid
=
true
;
}
}
if
(
isValid
)
{
Circle
circle
=
new
Circle
();
if
(
theFixRadius
!=
-
1
&&
currentMaxRadius
>
theFixRadius
)
{
currentMaxRadius
=
theFixRadius
;
}
circle
.
radius
=
currentMaxRadius
;
circle
.
x
=
centerX
;
circle
.
y
=
centerY
;
circles
.
Add
(
circle
);
currentMaxRadius
=
0
;
centerX
=
0
;
centerY
=
0
;
//平均半径
double
total
=
0
;
foreach
(
Circle
c
in
circles
)
{
total
=
total
+
c
.
radius
;
}
avgRadius
=
total
/
circles
.
Count
;
}
else
{
isEnd
=
true
;
}
}
public
List
<
List
<
Circle
>>
groupCircles
(
double
w
,
double
h
,
Point2d
center
)
{
foreach
(
Circle
c
in
circles
)
{
c
.
calDistanceToCenter
(
center
);
}
List
<
List
<
Circle
>>
allGroupCircle
=
new
List
<
List
<
Circle
>>();
List
<
int
>
allreadyGroup
=
new
List
<
int
>();
while
(
true
)
{
List
<
Circle
>
groupCircle
=
new
List
<
Circle
>();
Circle
labelCircle
=
null
;
for
(
int
i
=
0
;
i
<
circles
.
Count
;
i
++)
{
if
(!
allreadyGroup
.
Contains
(
i
))
{
Circle
c
=
circles
[
i
];
if
(
labelCircle
==
null
)
{
labelCircle
=
c
;
allreadyGroup
.
Add
(
i
);
groupCircle
.
Add
(
c
);
}
else
{
//到圆心的距离差小于H
if
(
Math
.
Abs
(
c
.
distanceToCenter
-
labelCircle
.
distanceToCenter
)
<
h
)
{
//两圆心之间的距离+直径小于W认为是同一个元器件
if
(
c
.
distanceToCircle
(
labelCircle
)
<
w
)
{
groupCircle
.
Add
(
c
);
allreadyGroup
.
Add
(
i
);
}
}
}
}
}
allGroupCircle
.
Add
(
groupCircle
);
if
(
allreadyGroup
.
Count
==
circles
.
Count
)
{
break
;
}
}
return
allGroupCircle
;
}
/// <summary>
/// 该点是否与其他圆内,或与圆的中心距离在半径内
/// </summary>
/// <param name="px"></param>
/// <param name="py"></param>
/// <returns></returns>
public
double
minDistanceToCircles
(
int
px
,
int
py
,
Point2d
reelCenter
,
double
oneBlobWidth
=-
1
,
double
oneBlobRadius
=
-
1
)
{
Point2d
point
=
new
Point2d
(
px
,
py
);
double
minDistanceToCircle
=
-
1
;
foreach
(
Circle
c
in
circles
)
{
Point2d
circleCenter
=
new
Point2d
(
c
.
x
,
c
.
y
);
double
distanceToCircle
=
point
.
DistanceTo
(
circleCenter
);
if
(
distanceToCircle
<=
c
.
radius
)
{
return
0
;
}
//当前点到料盘中心与圆心到料盘中心的距离在直径内,忽略
//if(oneBlobRadius != -1 && oneBlobWidth != -1)
//{
// double distanceToReelCenter = point.DistanceTo(reelCenter);
// double circleCenterToReelCenter = circleCenter.DistanceTo(reelCenter);
// if (Math.Abs(distanceToReelCenter - circleCenterToReelCenter) < oneBlobRadius)
// {
// if (distanceToCircle < oneBlobWidth)
// {
// return 0;
// }
// }
//}
if
(
minDistanceToCircle
==
-
1
||
distanceToCircle
<
minDistanceToCircle
)
{
minDistanceToCircle
=
distanceToCircle
-
c
.
radius
;
}
}
return
minDistanceToCircle
;
}
};
public
class
Circle
{
public
int
x
;
public
int
y
;
public
double
radius
;
public
double
distanceToCenter
=
0
;
public
double
distanceToCircle
(
Circle
c
)
{
double
distance
=
new
Point2d
(
x
,
y
).
DistanceTo
(
new
Point2d
(
c
.
x
,
c
.
y
));
return
distance
+
radius
+
c
.
radius
;
}
public
void
calDistanceToCenter
(
Point2d
center
)
{
if
(
center
.
X
>
0
&&
distanceToCenter
<=
0
)
{
distanceToCenter
=
center
.
DistanceTo
(
new
Point2d
(
x
,
y
));
}
}
}
}
Demo/MainForm.cs
查看文件 @
8511f26
...
@@ -69,7 +69,16 @@ namespace Acc.Demo
...
@@ -69,7 +69,16 @@ namespace Acc.Demo
{
{
theshValue
=
int
.
Parse
(
textBoxThesh
.
Text
);
theshValue
=
int
.
Parse
(
textBoxThesh
.
Text
);
}
}
Image
result
=
ImageUtil
.
Threshhold
(
orginalImage
,
theshValue
,
false
);
Image
result
;
result
=
ImageUtil
.
Threshhold
(
orginalImage
,
theshValue
);
//if (theshValue == -1)
//{
// result = ImageUtil.Threshhold(imageBox.Image, theshValue);
//}
//else
//{
// result = ImageUtil.DistanceTransform(imageBox.Image);
//}
imageBox
.
Image
=
result
;
imageBox
.
Image
=
result
;
}
}
...
@@ -105,26 +114,12 @@ namespace Acc.Demo
...
@@ -105,26 +114,12 @@ namespace Acc.Demo
private
void
Count
()
private
void
Count
()
{
{
ClearStatusBar
();
ClearStatusBar
();
int
theshValue
=
-
1
;
if
(!
checkBoxAutoThresh
.
Checked
)
{
theshValue
=
int
.
Parse
(
textBoxThesh
.
Text
);
}
Task
.
Factory
.
StartNew
(
delegate
{
Task
.
Factory
.
StartNew
(
delegate
{
//Image image = ImageUtil.FindCircle(orginalImage, theshValue, inv);
//imageBox.Image = image;
Stopwatch
sw
=
new
Stopwatch
();
Stopwatch
sw
=
new
Stopwatch
();
sw
.
Start
();
sw
.
Start
();
int
itemFeature
=
int
.
Parse
(
textBoxFeature
.
Text
);
int
itemFeature
=
int
.
Parse
(
textBoxFeature
.
Text
);
Image
image
=
orginalImage
;
Image
image
=
orginalImage
;
if
(!
ImageUtil
.
selectB
)
int
count
=
ImageUtil
.
CountItems
(
ref
image
,
itemFeature
);
{
itemFeature
=
ImageUtil
.
GetItemFeatureAuto
(
image
,
markX
,
markY
,
theshValue
);
}
ImageUtil
.
selectB
=
false
;
int
count
=
ImageUtil
.
CountItems
(
ref
image
,
itemFeature
,
theshValue
);
sw
.
Stop
();
sw
.
Stop
();
labelTime
.
Text
=
"耗时:"
+
sw
.
ElapsedMilliseconds
+
" ms"
;
labelTime
.
Text
=
"耗时:"
+
sw
.
ElapsedMilliseconds
+
" ms"
;
labelCount
.
Text
=
"数量:"
+
count
;
labelCount
.
Text
=
"数量:"
+
count
;
...
@@ -152,14 +147,23 @@ namespace Acc.Demo
...
@@ -152,14 +147,23 @@ namespace Acc.Demo
private
void
元件特征
ToolStripMenuItem_Click
(
object
sender
,
EventArgs
e
)
private
void
元件特征
ToolStripMenuItem_Click
(
object
sender
,
EventArgs
e
)
{
{
ClearStatusBar
();
ClearStatusBar
();
int
theshValue
=
-
1
;
Image
image
=
orginalImage
;
if
(!
checkBoxAutoThresh
.
Checked
)
int
feature
=
ImageUtil
.
GetFeature
(
ref
image
,
markX
,
markY
);
{
textBoxFeature
.
Text
=
feature
.
ToString
();
theshValue
=
int
.
Parse
(
textBoxThesh
.
Text
);
}
ImageUtil
.
selectB
=
true
;
Image
image
=
ImageUtil
.
Mark
(
orginalImage
,
markX
,
markY
,
theshValue
);
imageBox
.
Image
=
image
;
imageBox
.
Image
=
image
;
//int theshValue = -1;
//if (!checkBoxAutoThresh.Checked)
//{
// theshValue = int.Parse(textBoxThesh.Text);
//}
//Stopwatch sw = new Stopwatch();
//sw.Start();
//Image image = ImageUtil.Mark(orginalImage, markX, markY, theshValue);
//imageBox.Image = image;
//sw.Stop();
//Console.WriteLine("总耗时:" + sw.ElapsedMilliseconds + " ms");
}
}
private
void
保存当前图片
ToolStripMenuItem_Click
(
object
sender
,
EventArgs
e
)
private
void
保存当前图片
ToolStripMenuItem_Click
(
object
sender
,
EventArgs
e
)
...
...
Demo/MainForm.designer.cs
查看文件 @
8511f26
...
@@ -83,7 +83,7 @@
...
@@ -83,7 +83,7 @@
//
//
this
.
元件特征
ToolStripMenuItem
.
Name
=
"元件特征ToolStripMenuItem"
;
this
.
元件特征
ToolStripMenuItem
.
Name
=
"元件特征ToolStripMenuItem"
;
this
.
元件特征
ToolStripMenuItem
.
Size
=
new
System
.
Drawing
.
Size
(
180
,
22
);
this
.
元件特征
ToolStripMenuItem
.
Size
=
new
System
.
Drawing
.
Size
(
180
,
22
);
this
.
元件特征
ToolStripMenuItem
.
Text
=
"
标记记数
"
;
this
.
元件特征
ToolStripMenuItem
.
Text
=
"
元件特征
"
;
this
.
元件特征
ToolStripMenuItem
.
Click
+=
new
System
.
EventHandler
(
this
.
元件特征
ToolStripMenuItem_Click
);
this
.
元件特征
ToolStripMenuItem
.
Click
+=
new
System
.
EventHandler
(
this
.
元件特征
ToolStripMenuItem_Click
);
//
//
// 计数ToolStripMenuItem
// 计数ToolStripMenuItem
...
...
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论