"); // Reading an image from file
// Segmentation by regiongrowing
HRegionArray regs = image.Regiongrowing(1, 1, 4, 100);
HWindow w; // Display window
w.SetColored(12); // Set colors for regions
regs.Display(w); // Display the regions
HRegionArray rect; // New array
for (long i = 0; i < regs.Num(); i++) // For all regions in array
{ // Test size and shape of each region
if ((regs[i].Area() > 1000) && (regs[i].Compactness() < 1.5))
rect.Append(regs[i]); // If test true, append region
}
image.Display(w); // Display the image
rect.Display(w); // Display resulting regions
}
原图
变换后图像
The first step is to read an image. In this case it shows a control unit in a manufacturing environment, see figure 6.4 on the left side. By applying a regiongrowing algorithm from the HALCON library the image is segmented into regions. Each region inside the resulting region array regs is now selected according to its size and its compactness. Each region of a size larger than 1000 pixels and of a compactness value smaller than 1.5 is appended to the region array rect. After the processing of the for loop only the regions showing on the right side of figure 6.4 are left.
Images
在Halcon中,矩阵叫做通道,一个图像可能由若干个通道组成。比如灰度图像由一个通道,而彩色图像由3个通道组成。通道的类型不仅仅是8位(btype),而且可以有其他类型(比如16 int2)以及实数类型。除了保存像素信息,每一个HALCON图像也存储所谓的domain,就是上面介绍的那种Region格式。region可以理解为感兴趣的区域,即ROI。一般除了些许异常外,halcon算子都运行在region上处理。这一点与sepera相同。
Image Objects
类HImage是所有继承的图像类的根类。通过使用HImage,所有不同的像素类型都可以被以统一的格式处理(多态性)。类HImage不是虚类,因此可以被实例化。如下列举了几个重要的成员函数:
HImage(void)
默认构造函数,空的图像。 HImage(const char* file)
从一个文件中读取图像,同read_image HImage(int width,int height,const char* type)
构造一幅图像,指定了大小和类型。同gen_image_const HImage(void *ptr, int width, int height, const char *type)
构造一幅图像,使用拷贝的方式,指定图像的大小和类型,同gen_image1. irtual const char *PixType(void) const
像素类型,同 get_image_type. int Width(void) const
图像宽,见get_image_size. int Height(void) const
图像高,见 get_image_size. HPixVal GetPixVal(int x, int y) const
获取(x,y)处的像素值,见 get_grayval. HPixVal GetPixVal(long k) const
线性获得第k个像素值 virtual void SetPixVal(int x, int y, const HPixVal &val)
设置第(x,y)个像素值,同 set_grayval. virtual void SetPixVal(long k, const HPixVal &val)
设置第k个像素值 virtual void Display(const HWindow &w) const
在一个窗口上显示图像
几何运算
- HImage operator & (const HRegion ) const
裁剪图像的一部分区域,然后返回此部分的图像。同reduce_domain.
点评: 此函数设计的还是很好的,比较符合直觉。一幅图像与区域做&运算,就是获取共同的部分,也就是裁剪后的图像。
HImage operator + (const HImage &add) const
图像相加,同 add_image. HImage operator - (const HImage &sub) const
图像相减,同 sub_image. HImage operator * (const HImage &mult) const
图像相乘,同 mult_image. HImage operator - (void) const
图像取反, invert_image. HImage operator + (double add) const
HImage operator - (double sub) const
HImage operator * (double mult) const
HImage operator / (double div) const
图像加减乘除,同scale_image HRegion operator >= (const HImage &image) const
HRegion operator <= (const HImage &image) const
Selecting all pixel with gray values brighter than or equal to (or darker than or equal to, respectively) those of the input image, see**dyn_threshold**
点评:动态阈值,局部阈值处理,一般和均指图像做比较。 HRegion operator >= (double thresh) const
HRegion operator <= (double thresh) const
HRegion operator == (double thresh) const
HRegion operator != (double thresh) const
阈值处理,同 threshold.
例3
#include "HalconCpp.h"
using namespace Halcon;
#include "HIOStream.h"
#if !defined(USE_IOSTREAM_H)
using namespace std;
#endif
void main()
{
HImage image("E:\\halcon\\images\\mreut.png"); // Aerial image
HWindow w; // Output window
image.Display(w); // Display image
// Returning the size of the image
cout << "width = " << image.Width();
cout << "height = " << image.Height() << endl;
// Interactive drawing of a region by using the mouse
HRegion mask = w.DrawRegion();
// Reduce the domain of the image to the mask
HImage reduced = image & mask;
w.ClearWindow(); // Clear the window
reduced.Display(w); // Display the reduced image
// Applying the mean filter in the reduced image
HImage mean = reduced.MeanImage(61, 61);
mean.Display(w);
HRegion reg = reduced <= (mean -3);
reg.Display(w);
}
本例首先从文件中读取一幅灰度图像,目的是提取暗的部分。截取部分区域处理只是为了节省时间。可以通过鼠标任意画出部分区域来选择我们处理的部分图像区域。这部分区域掩模用于作为输入去截取图像(运算符&).带有61x61大小的均指掩模被应用于最后截取的图像获得均值图像