1、一个简单的加载并显示图像的Halcon(C++)程序
结果
C++代码
#include <iostream>
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
static void test() {
HObject ho_Image;
HTuple hv_Width, hv_Height, hv_WindowHandle;
HTuple path = "E:/picture/renwu1.jpg";
ReadImage(&ho_Image, path);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
OpenWindow(0, 0, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
}
int main()
{
test();
system("pause");
return 0;
}
总结
- 1、读取图像:ReadImage
- 2、Open a graphics window:OpenWindow
- 3、显示图像:DispObj
2、边缘检测器
1、EdgesImage
Lanser结果
canny结果
C++代码
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
static void test() {
HObject ho_Image, ho_ImaAmp, ho_ImaDir, ho_Edges;
HObject ho_Skeleton, ho_Contours;
HTuple hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
HTuple path = "E:/picture/renwu1.jpg";
ReadImage(&ho_Image, path);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetWindowAttr("background_color", "black");
EdgesImage(ho_Image, &ho_ImaAmp, &ho_ImaDir, "lanser2", 0.5, "nms", 12, 22);
Threshold(ho_ImaAmp, &ho_Edges, 1, 255);
Skeleton(ho_Edges, &ho_Skeleton);
GenContoursSkeletonXld(ho_Skeleton, &ho_Contours, 1, "filter");
OpenWindow(100, 100, hv_Width / 2, hv_Height / 2, "root", "visible", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
OpenWindow(100, 100 + hv_Width, hv_Width / 2, hv_Height / 2, 0, "", "", &hv_WindowHandle2);
HDevWindowStack::Push(hv_WindowHandle2);
if (HDevWindowStack::IsOpen())
DispObj(ho_ImaAmp, HDevWindowStack::GetActive());
OpenWindow(100, 100+ hv_Width / 2, hv_Width / 2, hv_Height / 2, 0, "", "", &hv_WindowHandle1);
HDevWindowStack::Push(hv_WindowHandle1);
if (HDevWindowStack::IsOpen())
DispObj(ho_Contours, HDevWindowStack::GetActive());
}
int main()
{
test();
system("pause");
return 0;
}
总结
EdgesImage 使用递归实现的过滤器(根据 Deriche、Lanser 和 Shen)或 Canny 提出的传统实现的“高斯导数”过滤器(使用过滤器掩码)检测阶梯边缘。 此外,可以使用 Sobel 滤波器的一种非常快速的变体。 因此,可以使用以下边缘运算符:
void EdgesImage
(
const HObject& Image,
HObject* ImaAmp,
HObject* ImaDir,
const HTuple& Filter,
const HTuple& Alpha,
const HTuple& NMS,
const HTuple& Low,
const HTuple& High
)
2、CloseEdges
结果
C++代码
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
static void test()
{
HObject ho_Image, ho_EdgeAmplitude, ho_Edges;
HObject ho_EdgesExtended;
HTuple hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
HTuple path = "E:/picture/renwu1.jpg";
ReadImage(&ho_Image, path);
if (HDevWindowStack::IsOpen())
CloseWindow(HDevWindowStack::Pop());
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetWindowAttr("background_color", "black");
OpenWindow(0, 0, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
SobelAmp(ho_Image, &ho_EdgeAmplitude, "thin_sum_abs", 3);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
OpenWindow(0, hv_Width, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle1);
HDevWindowStack::Push(hv_WindowHandle1);
Threshold(ho_EdgeAmplitude, &ho_Edges, 30, 255);
CloseEdges(ho_Edges, ho_EdgeAmplitude, &ho_EdgesExtended, 15);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "green");
if (HDevWindowStack::IsOpen())
DispObj(ho_EdgesExtended, HDevWindowStack::GetActive());
OpenWindow(0, hv_Width+hv_Width, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle2);
HDevWindowStack::Push(hv_WindowHandle2);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "red");
if (HDevWindowStack::IsOpen())
DispObj(ho_Edges, HDevWindowStack::GetActive());
}
int main()
{
test();
system("pause");
return 0;
}
总结
CloseEdges使用边缘幅度图像关闭边缘间隙。 CloseEdges 关闭边缘检测器输出中的间隙,从而尝试生成完整的对象轮廓。 这是通过检查每个边缘点的邻居来确定具有最大幅度(即最大梯度)的点,如果其幅度大于通过 MinAmplitude 的最小幅度,则将该点添加到边缘。 该算子期望典型边缘算子(例如 EdgesImage 或 SobelAmp)返回的边缘 (Edges) 和幅度图像 (EdgeImage) 作为输入。 CloseEdges 不考虑边缘运算符可能返回的边缘方向。 因此,在梯度几乎恒定的区域,边缘可能会变得相当“摇摆不定”。
void CloseEdges
(
const HObject& Edges,
const HObject& EdgeImage,
HObject* RegionResult,
const HTuple& MinAmplitude
)
3、CloseEdgesLength
结果
C++代码
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
static void test()
{
HObject ho_Image, ho_EdgeAmplitude, ho_Edges;
HObject ho_ClosedEdges;
HTuple hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
HTuple path = "E:/picture/renwu1.jpg";
ReadImage(&ho_Image, path);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
if (HDevWindowStack::IsOpen())
CloseWindow(HDevWindowStack::Pop());
SetWindowAttr("background_color", "black");
OpenWindow(0, 0, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
SobelAmp(ho_Image, &ho_EdgeAmplitude, "thin_sum_abs", 3);
Threshold(ho_EdgeAmplitude, &ho_Edges, 30, 255);
CloseEdgesLength(ho_Edges, ho_EdgeAmplitude, &ho_ClosedEdges, 8, 100);
OpenWindow(0, 0+ hv_Width, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle1);
HDevWindowStack::Push(hv_WindowHandle1);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "green");
if (HDevWindowStack::IsOpen())
DispObj(ho_ClosedEdges, HDevWindowStack::GetActive());
OpenWindow(0, hv_Width+ hv_Width, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle2);
HDevWindowStack::Push(hv_WindowHandle2);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "red");
if (HDevWindowStack::IsOpen())
DispObj(ho_Edges, HDevWindowStack::GetActive());
}
int main()
{
test();
system("pause");
return 0;
}
总结
CloseEdgesLength - 使用边缘幅度图像关闭边缘间隙 CloseEdgesLength 关闭边缘检测器输出中的间隙,从而尝试生成完整的对象轮廓。该算子期望由典型边缘算子(例如 EdgesImage 或 SobelAmp)返回的边缘 (Edges) 和幅度图像 (??Gradient) 作为输入。
轮廓的闭合分两步:首先,输入轮廓中一个像素宽的间隙被闭合,孤立点被消除。在此之后,通过添加边缘点将开放轮廓最多扩展 MaxGapLength 点,直到轮廓闭合或找不到更多重要的边缘点。如果梯度大于 MinAmplitude,则认为它是显着的。作为可能的新边缘点检查的相邻点是轮廓方向上的点及其在8邻域中的两个相邻点。对于这些点中的每一个,计算其梯度和该点三个可能邻居的最大梯度的总和(向前看长度 1)。然后选择总和最大的点作为新的边缘点。
void CloseEdgesLength
(
const HObject& Edges,
const HObject& Gradient,
HObject* ClosedEdges,
const HTuple& MinAmplitude,
const HTuple& MaxGapLength
)
4、DerivateGauss
DerivateGauss — 用高斯的导数对图像进行卷积。
DerivateGauss 将图像与高斯的导数进行卷积,并计算从中导出的各种特征。 Sigma 是高斯的参数(即平滑量)。 如果在 Sigma 中传递一个值,则在列和行方向上的平滑量是相同的。 如果在 Sigma 中传递两个值,第一个值指定列方向的平滑量,而第二个值指定行方向的平滑量。 Component的可能值为:
见:Halcon边缘提取之高斯导数卷积图像——derivate_gauss.hdev
5、SobelDir
结果
C++代码
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
static void test()
{
HObject ho_Image, ho_EdgeAmplitude, ho_EdgeDirection;
HObject ho_ImageResult, ho_Region;
HTuple hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
HTuple path = "E:/picture/renwu1.jpg";
ReadImage(&ho_Image, path);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
OpenWindow(0, 0, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
SobelDir(ho_Image, &ho_EdgeAmplitude, &ho_EdgeDirection, "sum_sqrt", 13);
OpenWindow(0, 0 + hv_Width, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle1);
HDevWindowStack::Push(hv_WindowHandle1);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "green");
if (HDevWindowStack::IsOpen())
DispObj(ho_EdgeDirection, HDevWindowStack::GetActive());
NonmaxSuppressionDir(ho_EdgeAmplitude, ho_EdgeDirection, &ho_ImageResult, "nms");
Threshold(ho_ImageResult, &ho_Region, 10, 255);
OpenWindow(0, hv_Width + hv_Width, hv_Width, hv_Height, 0, "", "", &hv_WindowHandle2);
HDevWindowStack::Push(hv_WindowHandle2);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "red");
if (HDevWindowStack::IsOpen())
DispObj(ho_Region, HDevWindowStack::GetActive());
}
int main()
{
test();
system("pause");
return 0;
}
总结
SobelDir - 使用 Sobel 算子检测边缘(幅度和方向)。 SobelDir 计算图像的一阶导数并用作边缘检测器。 该过滤器基于以下过滤器掩码:
void SobelDir
(
const HObject& Image,
HObject* EdgeAmplitude,
HObject* EdgeDirection,
const HTuple& FilterType,
const HTuple& Size
)
未完待续…
|