Halcon初探(C++版)之Edges

590 阅读5分钟

(Halcon初探(C++版)之Edges上)


1、一个简单的加载并显示图像的Halcon(C++)程序

结果

在这里插入图片描述

C++代码

#include <iostream>
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
static void test() {
	// Local iconic variables
	HObject  ho_Image;
	// Local control variables
	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() {
	// Local iconic variables
	HObject  ho_Image, ho_ImaAmp, ho_ImaDir, ho_Edges;
	HObject  ho_Skeleton, ho_Contours;
	// Local control variables
	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,   //Input image.
HObject* ImaAmp,        //Edge amplitude (gradient magnitude) image.
HObject* ImaDir,        //Edge direction image.
const HTuple& Filter,   //Edge operator to be applied.
const HTuple& Alpha,    //Filter parameter: small values result in strong smoothing, and thus less detail (opposite for 'canny').
const HTuple& NMS,      //Non-maximum suppression ('none', if not desired).
const HTuple& Low,      //Lower threshold for the hysteresis threshold operation (negative, if no thresholding is desired).
const HTuple& High      //Upper threshold for the hysteresis threshold operation (negative, if no thresholding is desired).
)

2、CloseEdges

结果

在这里插入图片描述

C++代码

#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;

static void test()
{
	// Local iconic variables
	HObject  ho_Image, ho_EdgeAmplitude, ho_Edges;
	HObject  ho_EdgesExtended;

	// Local control variables
	HTuple  hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
	// Local control variables
	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()
{
	// Local iconic variables
	HObject  ho_Image, ho_EdgeAmplitude, ho_Edges;
	HObject  ho_ClosedEdges;

	// Local control variables
	HTuple  hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
	// Local control variables
	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);
	//CloseEdgesLength........
	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());
	//Threshold........
	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()
{
	// Local iconic variables
	HObject  ho_Image, ho_EdgeAmplitude, ho_EdgeDirection;
	HObject  ho_ImageResult, ho_Region;
	// Local control variables
	HTuple  hv_Width, hv_Height, hv_WindowHandle, hv_WindowHandle1, hv_WindowHandle2;
	// Local control variables
	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
	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
	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
)

未完待续......