halocn/C++ (第一篇)


 
 
在使用C++編寫halcon之前,確定自己有較好的C++基礎,並熟悉一套開發平台如VC
 
Programmers_guide.pdf chapter7中有關於creating Aplicatin with halcon/c++的詳細介紹
 
 
以vs2008為例 工具---》選項  (有圖介紹不配文字啦)
 
 
 
用C++寫例子  ~~取狒狒的眼睛
//
#include "stdafx.h"
#include "HalconCpp.h"
#pragma comment(lib,"halconcpp.lib")
 
 
int main()
{
    using namespace Halcon;
    HImage Mandrill("monkey");
    HWindow w;
    w.Display(Mandrill);
    w.Click();
    HRegion Bright = (Mandrill >=128); // 取亮度大於128的區域
    HRegionArray conn = Bright.Connection();
    //取面積在500~9000的區域,狒狒的大鼻子也是大於128的,顯然不是我們檢測的重點根據面積舍去它
    HRegionArray large = conn.SelectShape("area","and",500,9000); 
    //眼睛是橢圓形的anisometry 是離心率即ra/rb 兩半徑之比,目測來看取1.2 -1.7
    HRegionArray eyes = large.SelectShape("anisometry","and",1.2,1.7);
    eyes.Display(w);
    w.Click();
    return 0;
}
 
 
//~~ 提取字符
#include "stdafx.h"
#include "HalconCpp.h"
#include <iostream>
#pragma comment(lib,"halconcpp.lib")
using namespace std;
using namespace Halcon;
 
int main()
{
    HImage image("alpha1");
    HRegion region;
    HWindow w;
    long threshold;
    image.Display(w);
    w.Click();
    // image.GetDomain -- 獲得圖像region
    //sigma --- 高斯平滑系數
    //Percent --- 前景背景灰度不同百分比 95 代表 至多有5%是最大灰度值 
    region = image.CharThreshold(image.GetDomain(),2,95,&threshold);
    image.Display(w);
    region.Display(w);
    w.Click();
    cout<<"Threshold for 'alpha': "<<threshold;
    w.Click();
    return 0;
}
注意:參考手冊中的操作符不全,A complete list can be found in the file include\cpp\HCPPGlobal.h.
 
 
1.關於參數:
①.halcon的輸入參數 (input parameters)不會被操作符改變,也就是說是按值傳遞的
 
如下代碼示例
HImage original_image("monkey"), smoothed_image;
smoothed_image = original_image.MeanImage(11, 11);
 
meanimage不會改變original_image,而中值后的圖像保存在返回值 smoothed_image中
 
②.與之相反的是輸出參數(output parameters),所以傳的是指針或者引用
 
 
2.halcon /C++關於面向對象和過程
 
這是通過類來調用方法的 ,面向對象方式
HImage image("barcode/ean13/ean1301");
HTuple paramName, paramValue;
HBarCode barcode(paramName, paramValue);
HRegionArray code_region;
HTuple result;
code_region = barcode.FindBarCode(image, "EAN-13", &result);
code_region = image.FindBarCode(barcode, "EAN-13", &result);
 
面向過程的方式
Hobject image;
HTuple barcode;
Hobject code_region;
HTuple result;
read_image(&image, "barcode/ean13/ean1301");
create_bar_code_model(HTuple(), HTuple(), &barcode);
find_bar_code(image, &code_region, barcode, "EAN-13", &result);
 
3.halcon/c++所有的 對象 可以在C:\Program Files\MVTec\HALCON-10.0\include\cpp 中找到 (以halcon安裝在C盤為例)
 
4.halcon/c++的數組模式
 
  我們可以用操作符來 打開多個圖片 region 等等 看該操作是否支持 數組模式 要看它的定義了
 
如圖所示
 
 
 數組的幾點說明:
① 創建,初始化數組
    C++ 可以通過 [] 
    C 通過 gen_empty_obj 增加數組 concat_obj
 
②操縱對象
   C++ 通過[N] ,統計數組元素個數 num()
   C  select_obj       count_obj
 
③iconic 數組以 0 開始  , Hobject 數組 以 1 開始
 
④通過例子說明第四點
 
#include "stdafx.h"
#include "HalconCpp.h"
#include <iostream>
#pragma comment(lib,"halconcpp.lib")
using namespace std;
using namespace Halcon;
 
//~~ 提取字符
int main()
{
    HImageArray images;
    HRegionArray regions;
    HTuple thresholds;
    HWindow w;
    for (int i = 1; i <=2;++i)
    {
        //HTuple("alpha") +i 的解釋
        //HTuple 中有對 operater + 的重載 
        //HTuple("alpha") +i ~~~ alpha1 alpha2.....
        images[i-1] = HImage::ReadImage(HTuple("alpha") +i);
    }
 
     //這一句相當於對 images中所有的對象進行了 charthreshold操作
    regions = images.CharThreshold(images[0].GetDomain(),2,95,&thresholds);
 
    for (int i = 0 ; i<regions.Num();++i)
    {
        images[i].Display(w);
        w.Click();
        regions[i].Display(w);
        w.Click();
        cout<<"threshold : "<<thresholds[i].L()<<endl;
    }
    w.Click();
 
    return 0;
}
 
5. 關於參數轉換
• Converting Hobject into iconic parameter classes
 
Hobject p_image;
read_image(&p_image, "barcode/ean13/ean1301");
HImage o_image(p_image);
 
Iconic parameters can be converted from Hobject to, e.g., HImage simply by calling the construc-
tor with the procedural variable as a parameter.
 
• Converting handles into handle classes
 
HTuple p_barcode;
create_bar_code_model(HTuple(), HTuple(), &p_barcode);
HBarCode o_barcode;
o_barcode.SetHandle(p_barcode[0]);
o_code_region = o_barcode.FindBarCode(o_image, "EAN-13", &result);
 
Handles cannot be converted directly via a constructor; instead, you call the method SetHandle()
with the procedural handle as a parameter.
 
• Converting handle classes into handles
 
p_barcode = o_barcode.GetHandle();
 
Similarly, a handle can be extracted from the corresponding class via the method GetHandle().
You can even omit the method, as the handle classes provide cast operators which convert them
automatically into handles.       
 
 p_barcode = o_barcode;
 
•Converting iconic parameter classes into Hobject
 
Hobject p_code_region = o_code_region.Id();
 
Iconic parameters can be converted from classes like HRegion back into Hobject via the method
Id(). In contrast to the handle classes no cast operator is provided.
 
 
• Converting HWindow into a window handle
 
long p_window;
open_window(0, 0, width/2, height/2, 0, "visible", "", &p_window);
HWindow o_window(0, 0, 100, 100, 0, "visible", "");
p_window = o_window.WindowHandle();
disp_obj(p_code_region, p_window);
 
In contrast to other handles, procedural window handles cannot be converted into instances of the
class HWindow! However, you can extract the handle from an instance of HWindow via the method
WindowHandle().
 
 
6 . The halcon Parameter classes
 
6.1 Iconic Objects
 
    基類為Hobject
    HImage ,HRegion,HXLD 基類為Hobject
 
6.1.1 Reions
 
region 是圖像平面的一組坐標點 , 它可能不是連接的,可能含有洞,它可以比圖像還大。
通過一個例子來了解HRegion
 
#include "stdafx.h"
#include "HalconCpp.h"
#include <iostream>
#pragma comment(lib,"halconcpp.lib")
using namespace std;
using namespace Halcon;
/*
     例子介紹了region的用法 ,更詳細的介紹在 progarma_guide  chapter6.1.1
*/
int main()
{
    HImage image("mreut");
    HRegion region;
    HWindow w;
    region = image >=190;
    w.SetColor("red");
    region.Display(w);
    w.Click();
 
     //region.Fillup() 調用方法
    HRegion filled = region.FillUp();
    filled.Display(w);
    w.Click();
 
     //運算符重載 開運算 >> 腐蝕 <<膨脹 , 半徑4.5
    HRegion open = (filled >>4.5)<<4.5;
    w.SetColor("green");
    open.Display(w);
    w.Click();
 
    w.ClearWindow();
     //平移
    HDPoint2D trans(-100 , -150);
    HRegion moved = open + trans;
    HRegion zoomed = moved * 2.0;
    zoomed.Display(w);
    w.Click();
 
    return 0;
}
 
 
 
 
#include "stdafx.h"
#include "HalconCpp.h"
#include <iostream>
#pragma comment(lib,"halconcpp.lib")
using namespace std;
using namespace Halcon;
/*
     例子介紹了regionArry的用法
*/
int main()
{
    HImage image("fabrik");
    // 1 1 row col 越小越精細
    // 4 容差 種子和被檢測像素值差
    // 100 最小region size 
    HRegionArray regs = image.Regiongrowing(1,1,4,100);
    HWindow w;
    w.SetColored(12);
    image.Display(w);
    w.Click();
    regs.Display(w);
    w.Click();
 
 
    HRegionArray rect;
    for (int i = 0 ; i < regs.Num() ; ++ i )
    {
        // compactness ~~ 在圖像處理中經常叫做圓形度 而這里用作 region的緊密度
        //C = L^2 / (4 F pi)) L = 周長 F = 面積
        //C = 1 是 為圓 ,region 較長是 或者region 有洞洞時 C >1
        //這里就是得出了 比較方正的 沒小洞洞的 region
        if ( (regs[i].Area() > 1000) && ( regs[i].Compactness() < 1.5) )
            rect.Append(regs[i]); //RegionArry 添加元素哦
    }
    image.Display(w);
    rect.Display(w);
    w.Click();
 
    return 0;
}
 
 
6.1.2 Images
 
#include "stdafx.h"
#include "HalconCpp.h"
#include <iostream>
#pragma comment(lib,"halconcpp.lib")
using namespace std;
using namespace Halcon;
/*
  drawregion 畫感興趣區域
  meanimage 做個中值濾波
  和中值濾波前圖像比較
*/
int main()
{
 
    HImage image("mreut");
    HWindow w;
    image.Display(w);
    cout<< " width = " << image.Width()<<endl;
    cout << " height = " << image.Height() <<endl;
 
    HRegion mask= w.DrawRegion();
 
  //  & 重載  取感興趣區域的圖像
    HImage reduced = image & mask;
    w.ClearWindow();
    reduced.Display(w);
    w.Click() ; 
 
    HImage mean = reduced.MeanImage(61,61);
    mean.Display(w);
 
    HRegion reg = reduced.DynThreshold(mean,3,"light");
    reg.Display(w);
    w.Click();
    return 0;
}
 
6.1.2.2  像素值
 
操縱像素的兩種方法
int main()
{
    HByteImage in("mreut");
    HByteImage out = in ;
    HWindow w;
    in.Display(w);
    w.Click() ; 
 
    int nwidht = in.Width();
    int nheight = in.Height();
    long end = nwidht * nheight;
 
    for (int k = 0 ; k < end ; k++)
    {
        //方法1
        //int pix = in.GetPixVal(k);
        //out.SetPixVal(k,255 - pix);
 
        //方法2
        out[k]= 255 - in[k];
    }
    out.Display(w);
    w.Click();
    return 0;
}
 
6.1.3 XLD Objects
 
 
XLD is the abbreviation for eXtended Line Description. This is a data structure used for describing areas
 
(e.g., arbitrarily sized regions or polygons) or any closed or open contour, i.e., also lines. In contrast to
 
regions, which represent all areas at pixel precision, XLD objects provide subpixel precision. There are
 
two basic XLD structures: contours and polygons.
 
Similarly to images, HALCON/C++ provides both a base class HXLD and a set of specialized classes
 
derived from HXLD, e.g., HXLDCont for contours or HXLDPoly for polygons. For all classes there exists
 
a corresponding container class, e.g., HXLDArray.
 
In contrast to the classes described in the previous sections, the XLD classes provide only member
 
functions corresponding to HALCON operators (see also section 5.2.2 on page 35).
 
 
6.1.4 Low - level iconic objects 底層iconic 對象
 
 the class Hobject is used for all iconic parameters, be it an image, a region, or even an image array
 
 In fact, the class Hobject is HALCON’s basic class for accessing the internal data management
 
Furthermore, Hobject serves as the basis for the class HObject and the derived classes, e.g., HImage.
 
As noted above, an instance of Hobject can also contain a tuple (array) of iconic objects. Unfortunately,
 
Hobject provides no special member functions to add objects or select them; instead, you must use the
 
operators gen_empty_obj, concat_obj, select_obj, and count_obj 。
 
一個Hobject實例可以包含一組iconic 對象,但Hobject 沒有提供增加或選擇它們的成員函數,所以必須通過
gen_empty_obj, concat_obj, select_obj, and count_obj 來解決。
 
 
6.2 Control parameters
 
 





附件列表

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM