Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
ReelCounter
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit ca84d094
由
SK
编写于
2019-04-23 08:53:36 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
获取元件面积时使用直方图二值化
1 个父辈
9e086d51
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
33 行增加
和
47 行删除
AccImage/ImageUtil.cs
AccImage/SplitItem.cs
AccImage/ImageUtil.cs
查看文件 @
ca84d09
...
...
@@ -251,9 +251,6 @@ namespace Acc.Img
}
}
private
static
void
LabelBlobsInCircle
(
ref
string
[]
labels
,
List
<
CvBlob
>
blobList
,
double
centerX
,
double
centerY
,
double
radius
)
{
int
labelCount
=
0
;
...
...
@@ -382,9 +379,13 @@ namespace Acc.Img
return
Math
.
Sqrt
(
avgArea
);
}
private
static
CvBlobs
AutoThreshBlobs
(
ref
Mat
imageMat
,
int
blobArea
)
private
static
CvBlobs
AutoThreshBlobs
(
ref
Mat
imageMat
,
int
blobArea
=
-
1
)
{
if
(
blobArea
==
-
1
)
{
//获取元器件特征时,假定的blob面积
blobArea
=
3
;
}
Mat
[]
mats
=
new
Mat
[]
{
imageMat
};
//一张图片,初始化为panda
Mat
hist
=
new
Mat
();
//用来接收直方图
int
[]
channels
=
new
int
[]
{
0
};
//一个通道,初始化为通道0
...
...
@@ -406,7 +407,7 @@ namespace Acc.Img
for
(
int
i
=
0
;
i
<
256
;
i
++)
//直方图
{
double
len
=
hist
.
Get
<
double
>(
i
);
if
(
len
>
100
)
if
(
len
>
100
)
{
//灰度值的像素数小于100的忽略
percent
=
percent
+
len
/
total
;
if
(
startIndex
==
-
1
)
...
...
@@ -422,56 +423,37 @@ namespace Acc.Img
}
}
int
avgIndex
=
(
startIndex
+
endIndex
)
/
2
;
//int avgIndex = (startIndex + endIndex) / 2;
//Mat threshMat = new Mat();
//Cv2.Threshold(imageMat, threshMat, avgIndex, 255, ThresholdTypes.BinaryInv);
//CvBlobs resultBlobs = new CvBlobs();
//resultBlobs.Label(threshMat);
//List<CvBlob> autoBlobList = resultBlobs.Values.Where(b => b.Area > blobArea).ToList();
//int blobCount = resultBlobs.Count();
Mat
threshMat
=
new
Mat
();
Cv2
.
Threshold
(
imageMat
,
threshMat
,
avgIndex
,
255
,
ThresholdTypes
.
BinaryInv
)
;
int
blobCount
=
0
;
CvBlobs
resultBlobs
=
new
CvBlobs
();
resultBlobs
.
Label
(
threshMat
);
List
<
CvBlob
>
autoBlobList
=
resultBlobs
.
Values
.
Where
(
b
=>
b
.
Area
>
blobArea
).
ToList
();
int
blobCount
=
resultBlobs
.
Count
();
int
threshIndex
=
avgIndex
;
int
threshIndex
=
(
startIndex
+
endIndex
)
/
2
;
double
theArea
=
blobArea
*
0.8
;
if
(
theArea
<
1
)
theArea
=
1
;
while
(
true
)
Console
.
WriteLine
(
"Avg Thresh: "
+
threshIndex
);
for
(
int
index
=
startIndex
;
index
<
endIndex
;
index
++
)
{
//阈值向下走,找满足条件Blob数量最多的
threshIndex
=
threshIndex
-
1
;
Cv2
.
Threshold
(
imageMat
,
threshMat
,
threshIndex
,
255
,
ThresholdTypes
.
BinaryInv
);
Mat
tempThreshMat
=
new
Mat
();
Cv2
.
Threshold
(
imageMat
,
tempThreshMat
,
threshIndex
,
255
,
ThresholdTypes
.
BinaryInv
);
CvBlobs
blobs
=
new
CvBlobs
();
blobs
.
Label
(
threshMat
);
blobs
.
Label
(
t
empT
hreshMat
);
List
<
CvBlob
>
blobList
=
blobs
.
Values
.
Where
(
b
=>
b
.
Area
>
theArea
).
ToList
();
if
(
blobList
.
Count
>
blobCount
)
{
threshMat
=
tempThreshMat
;
threshIndex
=
index
;
resultBlobs
=
blobs
;
blobCount
=
blobList
.
Count
;
}
else
{
break
;
}
}
threshIndex
=
avgIndex
;
while
(
true
)
{
//阈值向上走,找满足条件Blob数量最多的
threshIndex
=
threshIndex
+
1
;
Cv2
.
Threshold
(
imageMat
,
threshMat
,
threshIndex
,
255
,
ThresholdTypes
.
BinaryInv
);
CvBlobs
blobs
=
new
CvBlobs
();
blobs
.
Label
(
threshMat
);
List
<
CvBlob
>
blobList
=
blobs
.
Values
.
Where
(
b
=>
b
.
Area
>
theArea
).
ToList
();
if
(
blobList
.
Count
>
blobCount
)
{
resultBlobs
=
blobs
;
blobCount
=
blobList
.
Count
;
}
else
{
break
;
}
}
imageMat
=
threshMat
;
Console
.
WriteLine
(
threshIndex
+
"==== Blob: "
+
blobCount
+
" Area:"
+
theArea
);
Console
.
WriteLine
(
"result thresh: "
+
threshIndex
+
"==== Blob: "
+
blobCount
+
" Area:"
+
theArea
);
return
resultBlobs
;
}
...
...
@@ -1055,12 +1037,16 @@ namespace Acc.Img
Mat
dst
=
new
Mat
();
Cv2
.
CvtColor
(
imageMat
,
dst
,
ColorConversionCodes
.
RGB2GRAY
);
//全局二值化
Cv2
.
Threshold
(
dst
,
dst
,
0
,
255
,
ThresholdTypes
.
Otsu
|
ThresholdTypes
.
BinaryInv
);
//Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.BinaryInv);
//image = BitmapConverter.ToBitmap(dst);
//CvBlobs blobs = new CvBlobs();
//blobs.Label(dst);
CvBlobs
blobs
=
AutoThreshBlobs
(
ref
dst
);
image
=
BitmapConverter
.
ToBitmap
(
dst
);
CvBlobs
blobs
=
new
CvBlobs
();
blobs
.
Label
(
dst
);
int
blobArea
=
-
1
;
foreach
(
CvBlob
blob
in
blobs
.
Values
)
{
foreach
(
CvBlob
blob
in
blobs
.
Values
)
{
if
(
blob
.
Rect
.
Contains
(
new
OpenCvSharp
.
Point
(
markX
,
markY
)))
{
if
(
blob
.
Area
<
blobArea
||
blobArea
==
-
1
)
...
...
AccImage/SplitItem.cs
查看文件 @
ca84d09
...
...
@@ -31,7 +31,7 @@ namespace AccImage
{
//与平均半径差值在10%以内,认为OK
double
diff
=
avgRadius
-
currentMaxRadius
;
if
(
diff
/
avgRadius
<=
0.
4
)
if
(
diff
/
avgRadius
<=
0.
5
)
{
isValid
=
true
;
}
...
...
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论