在HALCON/C++中,HObject是一個基類,可以表示映像變數。另外還有三種類繼承自HObject. Class HImage 處理映像 Class HRegion 處理地區 Class HXLD 處理多邊形 Regions
一個region是映像平面座標點的集合。這樣一個地區不需要被連通,而且可能還有好多洞。a region可以比實際的映像大。地區在HALCON中可以用所謂的行程編碼實現。類HRegion代表HALCON/C++中的一個地區。HRegion的成員函數如下(列舉幾個重要的): HRegion(void)
預設構造器。創造一個空的region,即region的面積是0.並不是所有的運算元都可以將空region作為輸入參數,例如一些shape property 操作。 HRegion(const HRegion ®)
拷貝建構函式 HRegion &operator = (const HRegion ®)
賦值運算子 void Display(const HWindow &w) const
在一個視窗中輸出region
地區的幾何變換 HRegion operator * (double scale) const
放縮地區到任何一個尺度。放縮中心為(0,0) HRegion operator + (const HDPoint2D &point) const
HRegion &operator += (const HDPoint2D &point)
平移
地區的形態學處理: HRegion operator >> (double radius) const
HRegion &operator >>= (double radius)
用一個圓腐蝕,同erosion_circle HRegion operator << (double radius) const
HRegion &operator <<= (double radius)
用一個圓膨脹,同 dilation_circle. HRegion &operator ++ (void)
用一個含有五個點的十字交叉去膨脹。 HRegion &operator -- (void)
用一個含有五個點的十字交叉去腐蝕。 HRegion operator + (const HRegion ®) const
HRegion &operator += (const HRegion ®)
與另一個地區的Minkowsky 加和, 同 minkowski_add1. HRegion operator - (const HRegion ®) const
HRegion &operator -= (const HRegion ®)
與另一個地區的Minkowsky 減法, 同 minkowski_sub1.
地區的屬性: double Phi(void) const
一個等價的橢圓的角度,同 elliptic_axis. double Ra(void) const
一個地區的等價的橢圓的長半軸,同 elliptic_axis. double Rb(void) const
一個地區的等價的橢圓的短半軸,同elliptic_axis. long Area(void) const
地區的面積,即所包含的像素數,見 area_center. double X(void) const
double Y(void) const
地區的中心點座標,見area_center. HRectangle1 SmallestRectangle1(void) const
地區的最小包圍盒,此包圍盒平行於座標軸,同 smallest_rectangle1. HBool In(const HDPoint2D &p) const
檢驗一個點是否在地區內部,同 test_region_point. HBool IsEmpty(void) const;
檢驗地區是否為空白,也就是地區面積是否為0
例1
#include "HalconCpp.h"using namespace Halcon;void main(){ HImage image("E:\\halcon\\images\\mreut.png"); // Reading an aerial image HRegion region = image >= 190; // Calculating a threshold HWindow w; // Display window w.SetColor("red"); // Set color for regions region.Display(w); // Display the region HRegion filled = region.FillUp(); // Fill holes in region filled.Display(w); // Display the region // Opening: erosion followed by a dilation with a circle mask HRegion open = (filled >> 4.5) << 4.5; w.SetColor("green"); // Set color for regions open.Display(w); // Display the region HDPoint2D trans(-100, -150); // Vector for translation HRegion moved = open + trans; // Translation HRegion zoomed = moved * 2.0; // Zooming the region}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
First, an aerial image (mreut.png) is read from a file. All pixels with a gray value ≥ 190 are selected. This results in one region (region). This region is transformed by the next steps: All holes in the region are filled (FillUp), small parts of the region are eliminated by two morphological operations, first an erosion, a kind of shrinking the region, followed by a dilation, a kind of enlarging the region. The last step is the zooming of the region. For that the region is first shifted by a translation vector ( − 100, − 150) to the upper left corner and then zoomed by the factor two. Figure 6.2 shows the input image and the result of the opening operation. Region Arrays
HRegionArray是一個包含Region的容器。代表成員函數如下: long Num(void)
數列的個數,最大序號是Num() − 1. HRegion const &operator [] (long index) const
讀取數組的第i個元素,序號是 0 … Num() − 1. HRegion &operator [] (long index)
將一個region賦值給地區的第j個元素,The index index can be ≥ Num(). HRegionArray operator () (long min, long max) const
選取介於min與max之間的資料 HRegionArray &Append(const HRegion ®)
將一個region附加到region array的後面
許多接受region的運算元都接受region array作為輸入參數。如形態學操作。
例2
#include "HalconCpp.h"using namespace Halcon;void main(){ HImage image("E:\\halcon\\images\\control_unit.png"); // 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}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
原圖
變換後映像
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;#endifvoid 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);}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
本例首先從檔案中讀取一幅灰階映像,目的是提取暗的部分。截取部分地區處理只是為了節省時間。可以通過滑鼠任意畫出部分地區來選擇我們處理的部分映像地區。這部分地區掩模用於作為輸入去截取映像(運算子&).帶有61x61大小的均指掩模被應用於最後截取的映像獲得均值映像。暗的像素通過應用運算元<= 選取。所有的像素不超過均值-3的都選取。 具體可以參考 dyn_threshold. Pixel Values
HPixVal被用來訪問類HImage的像素值。灰階值可以獨立於他們的類型而返回和設定。
如下介紹常見的成員函數:
//構造HPixVal(void)Default constructor. HPixVal(const HComplex &Val)Constructing a pixel value from a complex number. HPixVal(int Val)Constructing a pixel value from an integer (int). HPixVal(long Val)Constructing a pixel value from a long (long). HPixVal(HByte Val)Constructing a pixel value from a byte (byte). HPixVal(double Val)Constructing a pixel value from a double (double). HPixVal(const HPixVal &Val)Copy constructor. HPixVal &operator = (const HPixVal &grey)Assignment operator. operator HByte(void) constConverting a pixel value to byte (0 … 255). //強制轉換operator int(void) constConverting a pixel value to int. operator long(void) constConverting a pixel value to long. operator