您的位置:首页 > 编程语言 > C语言/C++

Halcon/C++使用技巧

2014-10-12 08:49 176 查看
转自:http://qing.blog.sina.com.cn/2316220871/8a0eb9c733002v6x.html

首先,需要声明halcon中的每一步进行,在图形窗口都会有相应的变化来表示这一步的作用个,但是要在vsc++中也要有同样的效果,需要单独使用dev_display等控制命令,来对导出的cpp添加相应的显示功能,下面就是8个examples中的用的到的技巧:

1添加左键点击的等待,这样可以在调试的时候有时间观察结果

2线性改变图片方法

3非线性操作图片

4 region的膨胀

5 利用&操作制造圆形ROI

6 图像的移动,局部增值

7 自定义模板滤波,这里是sobel纵向

8 watersheds的使用,

9 直方图的建立和数据的保存

10 部分显示图片以及鼠标点击的操作

1添加左键点击的等待,这样可以在调试的时候有时间观察结果

void click(const Halcon::HWindow &w)

{

cout << "Click with mouse button to continue ..."; cout.flush();

w.Click();

cout << " ok\n"; cout.flush();

}

2线性改变图片方法

double t1,t2;

in.Display(w);

HByteImage out = in;

int width = out.Width();

int height = out.Height();

long end = width * height;

cout << "- copy image bit by bit via linear bit access" << endl;

count_seconds(&t1);

for (int k = 0; k < end; k++)

out[k] = 255 - in[k];

count_seconds(&t2);

cout << " --> (linear) time = " << (t2-t1)*1000 << " msec" << endl;

out.Display(w);

click(w);

3非线性操作图片

count_seconds(&t1);

for (int y=0; y<height; y++)

for (int x=0; x<width; x++)

out(x,y) = 255 - out(x,y);

count_seconds(&t2);

4 region的膨胀

HByteImage g("monkey");

HWindow w(0,0,g.Width(),g.Height());

w.SetPart(0,0,g.Height()-1,g.Width()-1);

HRegion reg = g >= 128; // threshold

w.SetColor("yellow");

w.SetInsert("xor");

reg.Display(w);

for (int i=0; i<100; i++)

(++reg).Display(w); // stepwise dilation of region

5 利用&操作制造圆形ROI

void RegionOfInterest()

{

using namespace Halcon;

cout << endl << "Restriction of the definition range" << endl;

HByteImage image("mreut");

HWindow w(0,0,image.Width(),image.Height());

w.SetPart(0,0,image.Height()-1,image.Width()-1);

int i;

for (i=1; i<30; i += 1)

(image & HRegion::GenCircle(255,255,i)).Display(w);

for (; i<80; i += 2)

(image & HRegion::GenCircle(255,255,i)).Display(w);

for (; i<255; i += 4)

(image & HRegion::GenCircle(255,255,i)).Display(w);

click(w);

}

6 图像的移动,局部增值

void Move(int x, int y)

{

using namespace Halcon;

cout << endl << "Move image with offset x=" << x << " and y=" << y << endl;

HByteImage in("mreut");

HWindow w(0,0,in.Width(),in.Height());

w.SetPart(0,0,in.Height()-1,in.Width()-1);

in.Display(w);

HByteImage out = in;

for (HImageIterator it(in,-x,-y,x,y); it.Continue(); ++it)

out[it] = in[it(x,y)];

//这里是图片的每一个像素点都进行移动

out.Display(w);

click(w);

}

void Add(int val)

{

using namespace Halcon;

cout << endl << "Add gray value " << val << " to inner image part" << endl;

HByteImage in("mreut");

HWindow w(0,0,in.Width(),in.Height());

w.SetPart(0,0,in.Height()-1,in.Width()-1);

in.Display(w);

HByteImage out = in;

HCircle circle(HDPoint2D(256,256),200);

for (HImageIterator it(in,HRegion(circle)); it.Continue(); ++it)

out[it] = HClipByte(in[it] + val);

//这里图片中相应的圆的范围增加相应的val值

out.Display(w);

click(w);

}

7 自定义模板滤波,这里是sobel纵向

void SobelY()

{

using namespace Halcon;

cout << endl << "Apply simple sobel filter in Y-direction" << endl;

HByteImage in("mreut");

HWindow w(0,0,in.Width(),in.Height());

w.SetPart(0,0,in.Height()-1,in.Width()-1);

in.Display(w);

HByteImage out = in;

for (HImageIterator it(in,3,3); it.Continue(); ++it)

{

int val = -2 * (int)in[it(-1,-1)] -

(int)in[it(0,-1)] -

2 * (int)in[it(1,-1)] +

2 * (int)in[it(-1,1)] +

(int)in[it(0,1)] +

2 * (int)in[it(1,1)];

out[it] = HClipByte(HAbs((long)val));

}

out.Display(w);

click(w);

}

Note:除了click(w)来等待点击,另一种方法是直接使用w.Click()

8 watersheds的使用,

HByteImage gauss = image.GaussImage(9);

HByteImage invert = gauss.InvertImage();

HRegion sheds;

cout << endl << "Starting watersheds algorithm." << endl;

cout << "Attention: Dependent on your hardware this may take some time!" << endl;

HRegionArray cells = invert.Watersheds(&sheds);

cout << cells.Num() << " cells found!" << endl << endl;

9 直方图的建立和数据的保存

enum { MAX_WIDTH = 1300, ADD = 10 };

// Building distribution:

int histo[MAX_WIDTH+2*ADD];

int i;

for (i=0; i<MAX_WIDTH+2*ADD; i++)

histo[i] = 0; //初始化

for (i=0; i<cells.Num(); i++) //这里的cells是8中的结果

for (int k=-ADD; k<=ADD; k++) //从-Add到Add的数据统计为一组

histo[(int)(cells[i].X())+k+ADD]++; //直方图的赋值,这里的X()得到的是第i个------------------------------------------------region的中心点的行坐标,那么直方图中向右------------------------------偏移ADD个单位(为了不从小于0的位置开始)所在的一个宽度------------------------------为2*ADD的柱状图(高度+1)

note: 这里每行的region个数被统计,并且以一个宽度为2*ADD的柱状图来表示个数

// Writing distribution into file:

ofstream out("cells.dat");

for (i=0; i<MAX_WIDTH+2*ADD; i++)

out << i-ADD << " " << histo[i] << endl;

cout << "Distribution of cell sizes saved in file 'cells.dat'." << endl;

cout << endl;

cout << "Click with mouse button to continue ..." << endl;

w.Click();

cout << "The End." << endl;

10 部分显示图片以及鼠标点击的操作

@部分显示图片

cout << "- Display image" << endl;

Mandrill.Display(w);

cout << "- Display image parts" << endl;

w.SetPart ( 0,100,312,412); Mandrill.Display (w);

w.SetPart ( 50,150,262,362); Mandrill.Display (w);

w.SetPart (100,200,212,312); Mandrill.Display (w);

w.SetPart (150,250,162,262); Mandrill.Display (w);

w.SetPart (155,255,157,257); Mandrill.Display (w);

w.SetPart ( 0, 0,511,511); Mandrill.Display (w);

@鼠标点击事件

cout << "Press left, middle, or right mouse button!" << endl;

(void)w.GetMbutton(NULL,&Button);

switch (Button[0].I()) {

case 1:

cout << "-> Left button pressed." << endl;

break;

case 2:

cout << "-> Middle button pressed." << endl;

break;

case 4:

cout << "-> Right button pressed." << endl;

break;

default:

cout << "-> Multiple buttons pressed." << endl;

break;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: