<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
//
霍夫线变换
private
string
HoughLinesFeatureDetect() {
//
获取参数
HOUGH_TYPE method
=
rbHoughLinesSHT.Checked
?
HOUGH_TYPE.CV_HOUGH_STANDARD : (rbHoughLinesPPHT.Checked
?
HOUGH_TYPE.CV_HOUGH_PROBABILISTIC : HOUGH_TYPE.CV_HOUGH_MULTI_SCALE);
double
rho
=
double
.Parse(txtHoughLinesRho.Text);
double
theta
=
double
.Parse(txtHoughLinesTheta.Text);
int
threshold
=
int
.Parse(txtHoughLinesThreshold.Text);
double
param1
=
double
.Parse(txtHoughLinesParam1.Text);
double
param2
=
double
.Parse(txtHoughLinesParam2.Text); MemStorage storage
=
new
MemStorage();
int
linesCount
=
0
; StringBuilder sbResult
=
new
StringBuilder();
//
计算,先运行Canny边缘检测(参数来自Canny算子属性页),然后再用计算霍夫线变换
double
lowThresh
=
double
.Parse(txtCannyLowThresh.Text);
double
highThresh
=
double
.Parse(txtCannyHighThresh.Text);
int
apertureSize
=
int
.Parse((
string
)cmbCannyApertureSize.SelectedItem); Image
<
Gray, Byte
>
imageCanny
=
new
Image
<
Gray,
byte
>
(imageSourceGrayscale.Size); CvInvoke.cvCanny(imageSourceGrayscale.Ptr, imageCanny.Ptr, lowThresh, highThresh, apertureSize); Stopwatch sw
=
new
Stopwatch(); sw.Start(); IntPtr ptrLines
=
CvInvoke.cvHoughLines2(imageCanny.Ptr, storage.Ptr, method, rho, theta, threshold, param1, param2); Seq
<
LineSegment2D
>
linesSeq
=
null
; Seq
<
PointF
>
linesSeq2
=
null
;
if
(method
==
HOUGH_TYPE.CV_HOUGH_PROBABILISTIC) linesSeq
=
new
Seq
<
LineSegment2D
>
(ptrLines, storage);
else
linesSeq2
=
new
Seq
<
PointF
>
(ptrLines, storage); sw.Stop();
//
显示
Image
<
Bgr, Byte
>
imageResult
=
imageSourceGrayscale.Convert
<
Bgr, Byte
>
();
if
(linesSeq
!=
null
) { linesCount
=
linesSeq.Total;
foreach
(LineSegment2D line
in
linesSeq) { imageResult.Draw(line,
new
Bgr(255d, 0d, 0d),
4
); sbResult.AppendFormat(
"
{0}-{1},
"
, line.P1, line.P2); } }
else
{ linesCount
=
linesSeq2.Total;
foreach
(PointF line
in
linesSeq2) {
float
r
=
line.X;
float
t
=
line.Y;
double
a
=
Math.Cos(t), b
=
Math.Sin(t);
double
x0
=
a
*
r, y0
=
b
*
r;
int
x1
=
(
int
)(x0
+
1000
*
(
-
b));
int
y1
=
(
int
)(y0
+
1000
*
(a));
int
x2
=
(
int
)(x0
-
1000
*
(
-
b));
int
y2
=
(
int
)(y0
-
1000
*
(a)); Point pt1
=
new
Point(x1, y1); Point pt2
=
new
Point(x2, y2); imageResult.Draw(
new
LineSegment2D(pt1, pt2),
new
Bgr(255d, 0d, 0d),
4
); sbResult.AppendFormat(
"
{0}-{1},
"
, pt1, pt2); } } pbResult.Image
=
imageResult.Bitmap;
//
释放资源
imageCanny.Dispose(); imageResult.Dispose(); storage.Dispose();
//
返回
return
string
.Format(
"
·霍夫线变换,用时{0:F05}毫秒,参数(变换方式:{1},距离精度:{2},弧度精度:{3},阀值:{4},参数1:{5},参数2:{6}),找到{7}条直线\r\n{8}
"
, sw.Elapsed.TotalMilliseconds, method.ToString(
"
G
"
), rho, theta, threshold, param1, param2, linesCount, linesCount
!=
0
?
(sbResult.ToString()
+
"
\r\n
"
) :
""
); }
//
霍夫圆变换
private
string
HoughCirclesFeatureDetect() {
//
获取参数
double
dp
=
double
.Parse(txtHoughCirclesDp.Text);
double
minDist
=
double
.Parse(txtHoughCirclesMinDist.Text);
double
param1
=
double
.Parse(txtHoughCirclesParam1.Text);
double
param2
=
double
.Parse(txtHoughCirclesParam2.Text);
int
minRadius
=
int
.Parse(txtHoughCirclesMinRadius.Text);
int
maxRadius
=
int
.Parse(txtHoughCirclesMaxRadius.Text); StringBuilder sbResult
=
new
StringBuilder();
//
计算
Stopwatch sw
=
new
Stopwatch(); sw.Start(); CircleF[][] circles
=
imageSourceGrayscale.HoughCircles(
new
Gray(param1),
new
Gray(param2), dp, minDist, minRadius, maxRadius); sw.Stop();
//
显示
Image
<
Bgr, Byte
>
imageResult
=
imageSourceGrayscale.Convert
<
Bgr, Byte
>
();
int
circlesCount
=
0
;
foreach
(CircleF[] cs
in
circles) {
foreach
(CircleF circle
in
cs) { imageResult.Draw(circle,
new
Bgr(255d, 0d, 0d),
4
); sbResult.AppendFormat(
"
圆心{0}半径{1},
"
, circle.Center, circle.Radius); circlesCount
++
; } } pbResult.Image
=
imageResult.Bitmap;
//
释放资源
imageResult.Dispose();
//
返回
return
string
.Format(
"
·霍夫圆变换,用时{0:F05}毫秒,参数(累加器图像的最小分辨率:{1},不同圆之间的最小距离:{2},边缘阀值:{3},累加器阀值:{4},最小圆半径:{5},最大圆半径:{6}),找到{7}个圆\r\n{8}
"
, sw.Elapsed.TotalMilliseconds, dp, minDist, param1, param2, minRadius, maxRadius, circlesCount, sbResult.Length
>
0
?
(sbResult.ToString()
+
"
\r\n
"
) :
""
); }