halcon视觉处理学习记录

By admin at 2018-01-06 • 0人收藏 • 2629人看过

下载了halcom 13.0版本

链接:https://pan.baidu.com/s/1hstQZdi 密码:hgma

安装完成软件之后,需要一个注册文件,

发送任意内容到system@51halcon.com这个邮件, 都可以得到一个为期一个月的试用注册文件, 如果再次提示需要注册,那么就再发送一次获取新的注册文件即可一直使用....

都弄好之后,软件里有很多视觉处理的示例.

那么我们如何用aardio来调用这个halcon的dll函数呢?


注意!!!

如果用aardio调用dll方式进行开发, 那么需要把halcon.dll和halconc.dll两个文件放到res目录,再在res目录中新建一个license文件夹,里面放halcon的许可证license.dat文件.

因此如果客户电脑上没有安装过halcon软件或者运行环境, 那么你一定要打包你的license许可证dat文件到exe中目录中,然后把这个许可证释放到和exe一个目录中....


但是,这样搞,貌似用户就可以看到你的license了.....这样感觉好坑.....

这样运行的软件能正常使用了.....


先这样, 等后面有其他方法在看看


halcon函数库在线文档:

http://www.mvtec.com/doc/halcon/13/en/index.html

17 个回复 | 最后更新于 2018-01-12
2018-01-06   #1

为了兼容,我安装halcon的时候选择的是32位的程序,这样aar就可以调用它的dll了

首先测试下利用aar+halcon能不能读取到图片

我为了懒省事, 把halcon中的所需dll都弄到了res目录下

然后,qq截图了一张图片,就是下面这张↓↓↓↓

5109.jpg

import console; 
var dll = ..raw.loadDll("\res\halconc.dll",,"cdecl");

read_image = dll.api("read_image","int(pointer& image,string filename)");
get_image_size = dll.api("get_image_size","int(pointer image,int& width,int& height)");

var imagehwnd = ..raw.malloc(1000);
var ret,imageH = read_image(imagehwnd,"res/5109");
console.log(ret,raw.tostring(imageH));
var ret,width,height = get_image_size(imageH,0,0);
console.log(ret,width,height)

console.pause(true);

blob.png

这里取到了图片的宽和高,和图片真实的对比,嗯..是一致的,第一步成功.

2018-01-06   #2

取得宽高之后就需要显示在界面上了.

这里我们换个更清晰的图片测试:

blob.png

import win.ui;
/*DSG{{*/
mainForm = win.form(text="halcon测试";right=731;bottom=413;border="dialog frame")
mainForm.add(
picturebox={cls="picturebox";left=108;top=27;right=567;bottom=333;bgcolor=0;z=1}
)
/*}}*/

//import console; 
var dll = ..raw.loadDll("\res\halconc.dll",,"cdecl");
//读取图片
read_image = dll.api("read_image","int(pointer& image,string filename)");
//获取图片高和宽
get_image_size = dll.api("get_image_size","int(pointer image,int& width,int& height)");
//显示图片
disp_obj = dll.api("disp_obj","int(pointer image, INT hwnd)");
//绘制一个显示窗口句柄
open_window = dll.api("open_window","int( int row,int col,int width,int height,INT fatherwindow,string mode,string machine, INT& windowhandle )");
//写入字符串
write_string = dll.api("write_string","int(INT windowhwnd , string name)");


var imagehwnd = ..raw.malloc(1000);
var ret,imageH = read_image(imagehwnd,"res\036.png");
//console.log(ret,raw.tostring(imageH));

var ret,width,height = get_image_size(imageH,0,0);
//console.log(ret,width,height)

var ret , WindowHandle = open_window(0, 0, width, height, mainForm.picturebox.hwnd, "visible", "",0); 
//console.log(ret , WindowHandle)
disp_obj(imageH , WindowHandle);
//中文字符需要格式化
write_string(WindowHandle,string.fromto("asdgf你好456",936,0));

mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();

blob.png


2018-01-06   #4

试了试官方提供的边缘示例,嗯...感觉不错, 后面可以慢慢研究了,哈

blob.png

import win.ui;
/*DSG{{*/
mainForm = win.form(text="halcon测试";right=1153;bottom=894;border="dialog frame")
mainForm.add(
picturebox={cls="picturebox";left=108;top=27;right=567;bottom=333;bgcolor=16777215;z=1};
picturebox2={cls="picturebox";left=608;top=27;right=1067;bottom=333;bgcolor=16777215;z=2};
picturebox3={cls="picturebox";left=103;top=396;right=562;bottom=702;bgcolor=16777215;z=3};
picturebox4={cls="picturebox";left=607;top=394;right=1066;bottom=700;bgcolor=16777215;z=4}
)
/*}}*/

import console; 
var dll = ..raw.loadDll("\res\halconc.dll",,"cdecl");
//读取图片
read_image = dll.api("read_image","int(pointer& image,string filename)");
//获取图片高和宽
get_image_size = dll.api("get_image_size","int(pointer image,int& width,int& height)");
//显示图片
disp_obj = dll.api("disp_obj","int(pointer image, INT hwnd)");
//绘制一个显示窗口句柄
open_window = dll.api("open_window","int( int row,int col,int width,int height,INT fatherwindow,string mode,string machine, INT& windowhandle )");
//写入字符串
write_string = dll.api("write_string","int(INT windowhwnd , string name)");
//边界查找
sobel_amp = dll.api("sobel_amp","int(pointer image ,pointer& imageB , string FilterType , int size)");
//
threshold = dll.api("threshold","int(pointer image ,pointer& imageB , int minGray , int maxGray)");
//
background_seg = dll.api("background_seg","int(pointer image ,pointer& imageB )");
//
fill_up_shape = dll.api("fill_up_shape","int(pointer image ,pointer& imageB , string FilterType ,int minGray , int maxGray)");

var imagehwnd = ..raw.malloc(1000);
var ret,imageH = read_image(imagehwnd,"res\036.png");
//console.log(ret,raw.tostring(imageH));

var ret,width,height = get_image_size(imageH,0,0);
//console.log(ret,width,height)

var ret , WindowHandle = open_window(0, 0, width, height, mainForm.picturebox.hwnd, "visible", "",0); 
//console.log(ret , WindowHandle)
disp_obj(imageH , WindowHandle);

//中文字符需要格式化
//write_string(WindowHandle,string.fromto("asdgf你好456",936,0));

var ret , BwindowHandle = open_window(0, 0, width, height, mainForm.picturebox2.hwnd, "visible", "",0); 
var imagehwndB = ..raw.malloc(1000);
var ret , Bimagehwnd = sobel_amp(imageH,imagehwndB,"thin_sum_abs",3);
disp_obj(Bimagehwnd , BwindowHandle)

var ret , CwindowHandle = open_window(0, 0, width, height, mainForm.picturebox3.hwnd, "visible", "",0); 
var imagehwndC = ..raw.malloc(1000);
var ret , Cimagehwnd = threshold(Bimagehwnd,imagehwndC,5,255);
disp_obj(Cimagehwnd , CwindowHandle)

var ret , DwindowHandle = open_window(0, 0, width, height, mainForm.picturebox4.hwnd, "visible", "",0); 
var imagehwndD = ..raw.malloc(1000);
var ret , Dimagehwnd = background_seg(Cimagehwnd,imagehwndD);
//disp_obj(Dimagehwnd , DwindowHandle)


//var ret , DwindowHandle = open_window(0, 0, width, height, mainForm.picturebox4.hwnd, "visible", "",0); 
var imagehwndE = ..raw.malloc(1000);
var ret , Eimagehwnd = fill_up_shape(Dimagehwnd,imagehwndE,"area",1,40);
disp_obj(Eimagehwnd , DwindowHandle)

mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();


2018-01-06   #5
//线性边界
bandpass_image = dll.api("bandpass_image","int(pointer image ,pointer& imageB , string FilterType )");

//调用
var ret , DwindowHandle = open_window(0, 0, width, height, mainForm.picturebox4.hwnd, "visible", "",0); 
var imagehwndD = ..raw.malloc(1000);
var ret , Dimagehwnd = bandpass_image(imageH,imagehwndD,"lines");
disp_obj(Dimagehwnd , DwindowHandle)

blob.png

2018-01-06   #6
//闭合边界
close_edges = dll.api("close_edges","int(pointer Edges ,pointer EdgeImage ,pointer& RegionResult , int MinAmplitude )");


2018-01-07   #7
//RGB彩色图转为灰度图
rgb1_to_gray= dll.api("rgb1_to_gray","int( pointer Image,pointer& GrayImage  )");

调用:

var ret , BwindowHandle = open_window(0, 0, width, height, mainForm.picturebox2.hwnd, "visible", "",0); 
var imagegrayhwnd = ..raw.malloc(1000);
var ret,imagegrayH = rgb1_to_gray(imageH,imagegrayhwnd);
disp_obj(imagegrayH , BwindowHandle);

blob.png

2018-01-07   #8
//释放所有操作
release_all_compute_devices = dll.api("release_all_compute_devices","int()");

调用:

mainForm.wndproc = function(hwnd,message,wPatam,lParam){
    select wParam{
        case 0xF060/*_SC_CLOSE*/ {
            process().terminate();//强制关闭窗体进程,防止进程残留
            release_all_compute_devices();
        }
    }
}


2018-01-07   #9

重要更新:

以上所有point image 和pointer& image 均可以写为ADDR image 和ADDR& image

这样调用就可以直接写数字0,代替raw.malloc(1000)了

如:

//读取图片
read_image = dll.api("read_image","int(ADDR& image,string filename)");

调用:

var ret,imageH = read_image(0,"res\olympic_stadium");

返回的imageH还是可以直接使用:

var ret,width,height = get_image_size(imageH,0,0);


2018-01-07   #10
//设置输出颜色
set_color = dll.api("set_color","int(INT windowhwnd , string name)");

调用要注意, 先设置颜色,然后调用disp显示

var ret , BwindowHandle = open_window(0, 0, width, height, mainForm.picturebox2.hwnd, "visible", "",0); 
//var imagegrayhwnd = ..raw.malloc(1000);
var ret,imagegrayH = rgb1_to_gray(imageH,0);
var ret,EdgesColorH = edges_color_sub_pix (imageH, 0, 'canny', 1, 20, 40);
set_color(BwindowHandle,"red");
//disp_obj(imagegrayH , BwindowHandle);
disp_obj(EdgesColorH , BwindowHandle);

边缘像素化:

edges_color_sub_pix= dll.api("edges_color_sub_pix","int(ADDR Image, ADDR& Edges, string Filter, double Alpha, double Low, double High)");

blob.png

2018-01-07   #11
//放大图片部分区域
set_part = dll.api("set_part","int(INT windowhwnd , int Row1, int Column1, int Row2, int Column2)");

调用

set_part(BwindowHandle,220,215,399,489);
disp_obj(imageH , BwindowHandle);

blob.png

2018-01-10   #12
import win.ui;
/*DSG{{*/
mainForm = win.form(text="aardio form";right=833;bottom=525)
mainForm.add(
button={cls="button";text="button";left=9;top=9;right=170;bottom=59;z=2};
picturebox={cls="picturebox";left=182;top=0;right=834;bottom=526;bgcolor=8421376;z=1}
)
/*}}*/

import console;

var dll = ..raw.loadDll("\res\halconc.dll","halconlib","cdecl");
T_open_window= dll.api("T_open_window","int(struct Row, struct Column, struct Width, struct Height, struct FatherWindow, struct Mode, struct Machine, struct& WindowHandle)");
open_window= dll.api("open_window","int(int Row, int Column, int Width, int Height, int FatherWindow, string Mode, string Machine, int& WindowHandle)");
T_read_image= dll.api("T_read_image","int(ADDR& Image, struct FileName)");
read_image= dll.api("read_image","int(ADDR& Image, string FileName)");
T_get_image_size= dll.api("T_get_image_size","int(ADDR Image, struct& Width, struct& Height)");
get_image_size= dll.api("get_image_size","int(ADDR Image, int& Width, int& Height)");
T_disp_obj= dll.api("T_disp_obj","int(ADDR Object, struct WindowHandle)");
disp_obj= dll.api("disp_obj","int(ADDR Object, int WindowHandle)");
T_fast_threshold= dll.api("T_fast_threshold","int(ADDR Image, ADDR& Region, struct MinGray, struct MaxGray, struct MinSize)");
fast_threshold= dll.api("fast_threshold","int(ADDR Image, ADDR& Region, double MinGray, double MaxGray, int MinSize)");
T_disp_region= dll.api("T_disp_region","int(ADDR DispRegions, struct WindowHandle)");
disp_region= dll.api("disp_region","int(ADDR DispRegions, int WindowHandle)");
T_set_colored= dll.api("T_set_colored","int(struct WindowHandle, struct NumberOfColors)");
set_colored= dll.api("set_colored","int(int WindowHandle, int NumberOfColors)");
T_set_color= dll.api("T_set_color","int(struct WindowHandle, struct Color)");
set_color= dll.api("set_color","int(int WindowHandle, string Color)");
//console.open();
mainForm.button.oncommand = function(id,event){
	
	var ret,Image = read_image(0,"res/double_circle");
	var ret,width,height = get_image_size(Image,0,0);
	//mainForm.picturebox.setPos(,,width,height);
	//console.log(ret,width,height);
	var ret,Hwindow = open_window(0,0,width,height,mainForm.picturebox.hwnd,"visible", "",0);
	var ret = disp_obj(Image,Hwindow);
	var ret,Region = fast_threshold(Image,0,0,120,7);
	set_colored(Hwindow,6);
	disp_region(Region,Hwindow)
	
	
}

mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();

blob.png

2018-01-10   #13
import win.ui;
/*DSG{{*/
mainForm = win.form(text="aardio form";right=833;bottom=525)
mainForm.add(
button={cls="button";text="button";left=9;top=9;right=170;bottom=59;z=2};
picturebox={cls="picturebox";left=182;top=0;right=834;bottom=526;bgcolor=8421376;z=1}
)
/*}}*/

import console;

var dll = ..raw.loadDll("\res\halconc.dll","halconlib","cdecl");
T_open_window= dll.api("T_open_window","int(struct Row, struct Column, struct Width, struct Height, struct FatherWindow, struct Mode, struct Machine, struct& WindowHandle)");
open_window= dll.api("open_window","int(int Row, int Column, int Width, int Height, int FatherWindow, string Mode, string Machine, int& WindowHandle)");
T_read_image= dll.api("T_read_image","int(ADDR& Image, struct FileName)");
read_image= dll.api("read_image","int(ADDR& Image, string FileName)");
T_get_image_size= dll.api("T_get_image_size","int(ADDR Image, struct& Width, struct& Height)");
get_image_size= dll.api("get_image_size","int(ADDR Image, int& Width, int& Height)");
T_disp_obj= dll.api("T_disp_obj","int(ADDR Object, struct WindowHandle)");
disp_obj= dll.api("disp_obj","int(ADDR Object, int WindowHandle)");
T_fast_threshold= dll.api("T_fast_threshold","int(ADDR Image, ADDR& Region, struct MinGray, struct MaxGray, struct MinSize)");
fast_threshold= dll.api("fast_threshold","int(ADDR Image, ADDR& Region, double MinGray, double MaxGray, int MinSize)");
T_disp_region= dll.api("T_disp_region","int(ADDR DispRegions, struct WindowHandle)");
disp_region= dll.api("disp_region","int(ADDR DispRegions, int WindowHandle)");
T_set_colored= dll.api("T_set_colored","int(struct WindowHandle, struct NumberOfColors)");
set_colored= dll.api("set_colored","int(int WindowHandle, int NumberOfColors)");
T_set_color= dll.api("T_set_color","int(struct WindowHandle, struct Color)");
set_color= dll.api("set_color","int(int WindowHandle, string Color)");
T_boundary= dll.api("T_boundary","int(ADDR Region, ADDR& RegionBorder, struct BoundaryType)");
boundary= dll.api("boundary","int(ADDR Region, ADDR& RegionBorder, string BoundaryType)");
T_clip_region_rel= dll.api("T_clip_region_rel","int(ADDR Region, ADDR& RegionClipped, struct Top, struct Bottom, struct Left, struct Right)");
clip_region_rel= dll.api("clip_region_rel","int(ADDR Region, ADDR& RegionClipped, int Top, int Bottom, int Left, int Right)");
T_dilation_circle= dll.api("T_dilation_circle","int(ADDR Region, ADDR& RegionDilation, struct Radius)");
dilation_circle= dll.api("dilation_circle","int(ADDR Region, ADDR& RegionDilation, double Radius)");
T_reduce_domain= dll.api("T_reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
reduce_domain= dll.api("reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");

//console.open();
mainForm.button.oncommand = function(id,event){
	
	var ret,Image = read_image(0,"res/double_circle");
	var ret,width,height = get_image_size(Image,0,0);
	//mainForm.picturebox.setPos(,,width,height);
	//console.log(ret,width,height);
	var ret,Hwindow = open_window(0,0,width,height,mainForm.picturebox.hwnd,"visible", "",0);
	var ret = disp_obj(Image,Hwindow);
	win.delay(1000)
	var ret,Region = fast_threshold(Image,0,0,120,7);
	set_colored(Hwindow,6);
	disp_region(Region,Hwindow)
	win.delay(1000)
	var ret,RegionBorder = boundary(Region,0,"inner");
	//disp_region(RegionBorder,Hwindow)
	var ret,RegionClipped = clip_region_rel(RegionBorder,0,5,5,5,5);
	var ret,RegionDilation = dilation_circle(RegionClipped,0,2.5);
	var ret,ImageReduced = reduce_domain(Image,RegionDilation,0);
	var ret = disp_obj(ImageReduced,Hwindow);
}

mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();

blob.png

2018-01-10   #14
import win.ui;
/*DSG{{*/
mainForm = win.form(text="aardio form";right=833;bottom=525)
mainForm.add(
button={cls="button";text="button";left=9;top=9;right=170;bottom=59;z=2};
picturebox={cls="picturebox";left=182;top=0;right=834;bottom=526;bgcolor=8421376;z=1};
snumber={cls="static";text="0";left=28;top=376;right=160;bottom=412;font=LOGFONT(h=-21);transparent=1;z=3}
)
/*}}*/

import console;

var dll = ..raw.loadDll("\res\halconc.dll","halconlib","cdecl");
T_open_window= dll.api("T_open_window","int(struct Row, struct Column, struct Width, struct Height, struct FatherWindow, struct Mode, struct Machine, struct& WindowHandle)");
open_window= dll.api("open_window","int(int Row, int Column, int Width, int Height, int FatherWindow, string Mode, string Machine, int& WindowHandle)");
T_read_image= dll.api("T_read_image","int(ADDR& Image, struct FileName)");
read_image= dll.api("read_image","int(ADDR& Image, string FileName)");
T_get_image_size= dll.api("T_get_image_size","int(ADDR Image, struct& Width, struct& Height)");
get_image_size= dll.api("get_image_size","int(ADDR Image, int& Width, int& Height)");
T_disp_obj= dll.api("T_disp_obj","int(ADDR Object, struct WindowHandle)");
disp_obj= dll.api("disp_obj","int(ADDR Object, int WindowHandle)");
T_fast_threshold= dll.api("T_fast_threshold","int(ADDR Image, ADDR& Region, struct MinGray, struct MaxGray, struct MinSize)");
fast_threshold= dll.api("fast_threshold","int(ADDR Image, ADDR& Region, double MinGray, double MaxGray, int MinSize)");
T_disp_region= dll.api("T_disp_region","int(ADDR DispRegions, struct WindowHandle)");
disp_region= dll.api("disp_region","int(ADDR DispRegions, int WindowHandle)");
T_set_colored= dll.api("T_set_colored","int(struct WindowHandle, struct NumberOfColors)");
set_colored= dll.api("set_colored","int(int WindowHandle, int NumberOfColors)");
T_set_color= dll.api("T_set_color","int(struct WindowHandle, struct Color)");
set_color= dll.api("set_color","int(int WindowHandle, string Color)");
T_boundary= dll.api("T_boundary","int(ADDR Region, ADDR& RegionBorder, struct BoundaryType)");
boundary= dll.api("boundary","int(ADDR Region, ADDR& RegionBorder, string BoundaryType)");
T_clip_region_rel= dll.api("T_clip_region_rel","int(ADDR Region, ADDR& RegionClipped, struct Top, struct Bottom, struct Left, struct Right)");
clip_region_rel= dll.api("clip_region_rel","int(ADDR Region, ADDR& RegionClipped, int Top, int Bottom, int Left, int Right)");
T_dilation_circle= dll.api("T_dilation_circle","int(ADDR Region, ADDR& RegionDilation, struct Radius)");
dilation_circle= dll.api("dilation_circle","int(ADDR Region, ADDR& RegionDilation, double Radius)");
T_reduce_domain= dll.api("T_reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
reduce_domain= dll.api("reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
T_edges_sub_pix= dll.api("T_edges_sub_pix","int(ADDR Image, ADDR& Edges, struct Filter, struct Alpha, struct Low, struct High)");
edges_sub_pix= dll.api("edges_sub_pix","int(ADDR Image, ADDR& Edges, string Filter, double Alpha, int Low, int High)");
T_segment_contours_xld= dll.api("T_segment_contours_xld","int(ADDR Contours, ADDR& ContoursSplit, struct Mode, struct SmoothCont, struct MaxLineDist1, struct MaxLineDist2)");
segment_contours_xld= dll.api("segment_contours_xld","int(ADDR Contours, ADDR& ContoursSplit, string Mode, int SmoothCont, double MaxLineDist1, double MaxLineDist2)");
T_count_obj= dll.api("T_count_obj","int(ADDR Objects, struct& Number)");
count_obj= dll.api("count_obj","int(ADDR Objects, int& Number)");
T_set_draw= dll.api("T_set_draw","int(struct WindowHandle, struct Mode)");
set_draw= dll.api("set_draw","int(int WindowHandle, string Mode)");
T_select_obj= dll.api("T_select_obj","int(ADDR Objects, ADDR& ObjectSelected, struct Index)");
select_obj= dll.api("select_obj","int(ADDR Objects, ADDR& ObjectSelected, int Index)");
T_get_contour_global_attrib_xld= dll.api("T_get_contour_global_attrib_xld","int(ADDR Contour, string Name, struct& Attrib)");
T_fit_circle_contour_xld= dll.api("T_fit_circle_contour_xld","int(ADDR Contours, struct Algorithm, struct MaxNumPoints, struct MaxClosureDist, struct ClippingEndPoints, struct Iterations, struct ClippingFactor, struct& Row, struct& Column, struct& Radius, struct& StartPhi, struct& EndPhi, struct& PointOrder)");
fit_circle_contour_xld= dll.api("fit_circle_contour_xld","int(ADDR Contours, string Algorithm, int MaxNumPoints, double MaxClosureDist, int ClippingEndPoints, int Iterations, double ClippingFactor, double& Row, double& Column, double& Radius, double& StartPhi, double& EndPhi, string& PointOrder)");
T_gen_circle_contour_xld= dll.api("T_gen_circle_contour_xld","int(ADDR& ContCircle, struct Row, struct Column, struct Radius, struct StartPhi, struct EndPhi, struct PointOrder, struct Resolution)");
gen_circle_contour_xld= dll.api("gen_circle_contour_xld","int(ADDR& ContCircle, double Row, double Column, double Radius, double StartPhi, double EndPhi, string PointOrder, double Resolution)");
T_get_contour_attrib_xld= dll.api("T_get_contour_attrib_xld","int(ADDR Contour, string Name, struct& Attrib)");

//console.open();
mainForm.button.oncommand = function(id,event){
	
	var ret,Image = read_image(0,"res/double_circle");
	var ret,width,height = get_image_size(Image,0,0);
	//mainForm.picturebox.setPos(,,width,height);
	//console.log(ret,width,height);
	var ret,Hwindow = open_window(0,0,width,height,mainForm.picturebox.hwnd,"visible", "",0);
	var ret = disp_obj(Image,Hwindow);
	win.delay(1000)
	var ret,Region = fast_threshold(Image,0,0,120,7);
	set_colored(Hwindow,6);
	disp_region(Region,Hwindow)
	win.delay(1000)
	var ret,RegionBorder = boundary(Region,0,"inner");
	//disp_region(RegionBorder,Hwindow)
	var ret,RegionClipped = clip_region_rel(RegionBorder,0,5,5,5,5);
	var ret,RegionDilation = dilation_circle(RegionClipped,0,2.5);
	var ret,ImageReduced = reduce_domain(Image,RegionDilation,0);
	var ret = disp_obj(ImageReduced,Hwindow);
	win.delay(1000)
	var ret,Edges = edges_sub_pix(ImageReduced,0,"canny",2,20,60);
	var ret = disp_obj(Edges,Hwindow);
	win.delay(1000)
	var ret,ContoursSplit = segment_contours_xld(Edges,0,"lines_circles",5,4,3);
	//var ret = disp_obj(ContoursSplit,Hwindow);
	var ret = disp_obj(Image,Hwindow);
	var ret,Number = count_obj(ContoursSplit,0);
	//mainForm.snumber.text = Number;
	set_draw(Hwindow,"margin");
	set_color(Hwindow,"white");
	
	for(i=1;Number;1){
		var ret,ObjectSelected = select_obj(ContoursSplit,0,i);
		var ret,Attrib = T_get_contour_global_attrib_xld(ObjectSelected,"cont_approx",{});
		if(Attrib > 0){
			var ret,Row, Column, Radius, StartPhi, EndPhi, PointOrder = fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, 0, 0, 0, 0, 0, 0)
        	var ret,ContCircle = gen_circle_contour_xld (0, Row, Column, Radius, 0, math.rad(360), 'positive', 1.0)
			var ret = disp_obj(ContCircle,Hwindow);
		}
	}
	
	
}

mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();

错误:第89行内存错误....

2018-01-11   #15

Using control parameter tuples in C isn't as elegant as using image object tuples. To circumvent the missing generic lists in C, it was necessary to introduce two different working modes into HALCON/C: The simple mode and the tuple mode. If a tuple is necessary for at least one control parameter, the tuple mode has to be used for operator calls. In tuple mode, all control parameters of an operator must be passed as type Htuple (Mixing of the two modes is not possible). The tuple mode also has to be used if the number or type of values that a operators calculates isn't known beforehand.


上面意思大概是说,元组模式和普通模式不能混用...

the description of the HALCON interface in the file HProto.h.

在上面的h文件里又对元组和普通的声明

Sometimes the manuals mention more than one possible type. If only integers and floating point numbers are allowed for a parameter, values have to be passed as parameters of type double. For all other combinations of types, the tuple mode has to be used.

有时候如果只是整型或者浮点型的元素,一般类型就是double,其他都是元组

针对下面的普通模式:

Hlong for integers (HALCON type LONG_PAR),

double for floating point numbers (DOUBLE_PAR) or

char* for character arrays (strings, STRING_PAR).


上面三个是常用的普通模式类型定义,分别代表:整型, 浮点型, 字符型

Hlong and double input control parameters are passed by value as usual, the corresponding output control parameters are passed by reference, using the &-operator. String parameters are pointers to char in both cases. Please note, that the memory for output control parameters (in particular strings) has to be provided by the caller! We recommend to allocate memory for at least 1024 characters for string parameters of unknown length. Output parameter values that are of no further interest can be set to NULL (e.g.,as in the call get_mbutton in figure 20.1).

整型和浮点型一般作为输入, 而字符串元素部分是指向char的指针, 必需注意:输出控件部分的内存必需调用者要提供, 对于不知道返回长度的元素部分,内存至少分配1024个字节,输出参数值不会在有null空值了.具体去看第21.1和20.1章节


针对元组模式:

Hlong* for integer arrays (HALCON type LONG_PAR),

double* for floating point arrays (DOUBLE_PAR) or

char** for string arrays (strings, STRING_PAR)


整型数组,浮点数组,字符串数组

Control parameter tuples must be created, deleted, and manipulated using the appropriate HALCON/C procedures only (overview in figures 21.2, figure 21.3, and figure 21.4).

The following sections describe the five most important steps when calling a HALCON operator in tuple mode:


allocate memory (section “Allocate Memory”)

construct input parameters (section “Create Input Parameters”)

call operator (section “Call Operator”)

process output parameters (section “Process Output Parameters”)

free memory (section “Free Memory”)







2018-01-11   #16

blob.png

继续以上元组部分:

要使用元组操作,必需一下五步:

  1. 分配内存

  2. 构建输入参数

  3. 调用操作

  4. 获取输出参数

  5. 释放内存


2018-01-12   #17

功能: 查找图片的边界, 然后根据边界绘制出圆形
工程下载链接:https://pan.baidu.com/s/1bqMNTZh 密码:lxoq

GIF.gif

import win.ui;
/*DSG{{*/
mainForm = win.form(text="Halcon函数circle测试";right=833;bottom=525)
mainForm.add(
button={cls="button";text="button";left=9;top=9;right=170;bottom=59;z=2};
picturebox={cls="picturebox";left=182;top=0;right=834;bottom=526;bgcolor=8421376;z=1};
snumber={cls="static";text="0";left=18;top=397;right=150;bottom=433;font=LOGFONT(h=-21);transparent=1;z=3}
)
/*}}*/

//import console;
var dll = ..raw.loadDll("\res\halconc.dll","halconlib","cdecl");
T_open_window= dll.api("T_open_window","int(struct Row, struct Column, struct Width, struct Height, struct FatherWindow, struct Mode, struct Machine, struct& WindowHandle)");
open_window= dll.api("open_window","int(int Row, int Column, int Width, int Height, int FatherWindow, string Mode, string Machine, int& WindowHandle)");
T_read_image= dll.api("T_read_image","int(ADDR& Image, struct FileName)");
read_image= dll.api("read_image","int(ADDR& Image, string FileName)");
T_get_image_size= dll.api("T_get_image_size","int(ADDR Image, struct& Width, struct& Height)");
get_image_size= dll.api("get_image_size","int(ADDR Image, int& Width, int& Height)");
T_disp_obj= dll.api("T_disp_obj","int(ADDR Object, struct WindowHandle)");
disp_obj= dll.api("disp_obj","int(ADDR Object, int WindowHandle)");
T_fast_threshold= dll.api("T_fast_threshold","int(ADDR Image, ADDR& Region, struct MinGray, struct MaxGray, struct MinSize)");
fast_threshold= dll.api("fast_threshold","int(ADDR Image, ADDR& Region, double MinGray, double MaxGray, int MinSize)");
T_disp_region= dll.api("T_disp_region","int(ADDR DispRegions, struct WindowHandle)");
disp_region= dll.api("disp_region","int(ADDR DispRegions, int WindowHandle)");
T_set_colored= dll.api("T_set_colored","int(struct WindowHandle, struct NumberOfColors)");
set_colored= dll.api("set_colored","int(int WindowHandle, int NumberOfColors)");
T_set_color= dll.api("T_set_color","int(struct WindowHandle, struct Color)");
set_color= dll.api("set_color","int(int WindowHandle, string Color)");
T_boundary= dll.api("T_boundary","int(ADDR Region, ADDR& RegionBorder, struct BoundaryType)");
boundary= dll.api("boundary","int(ADDR Region, ADDR& RegionBorder, string BoundaryType)");
T_clip_region_rel= dll.api("T_clip_region_rel","int(ADDR Region, ADDR& RegionClipped, struct Top, struct Bottom, struct Left, struct Right)");
clip_region_rel= dll.api("clip_region_rel","int(ADDR Region, ADDR& RegionClipped, int Top, int Bottom, int Left, int Right)");
T_dilation_circle= dll.api("T_dilation_circle","int(ADDR Region, ADDR& RegionDilation, struct Radius)");
dilation_circle= dll.api("dilation_circle","int(ADDR Region, ADDR& RegionDilation, double Radius)");
T_reduce_domain= dll.api("T_reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
reduce_domain= dll.api("reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
T_edges_sub_pix= dll.api("T_edges_sub_pix","int(ADDR Image, ADDR& Edges, struct Filter, struct Alpha, struct Low, struct High)");
edges_sub_pix= dll.api("edges_sub_pix","int(ADDR Image, ADDR& Edges, string Filter, double Alpha, int Low, int High)");
T_segment_contours_xld= dll.api("T_segment_contours_xld","int(ADDR Contours, ADDR& ContoursSplit, struct Mode, struct SmoothCont, struct MaxLineDist1, struct MaxLineDist2)");
segment_contours_xld= dll.api("segment_contours_xld","int(ADDR Contours, ADDR& ContoursSplit, string Mode, int SmoothCont, double MaxLineDist1, double MaxLineDist2)");
T_count_obj= dll.api("T_count_obj","int(ADDR Objects, struct& Number)");
count_obj= dll.api("count_obj","int(ADDR Objects, int& Number)");
T_set_draw= dll.api("T_set_draw","int(struct WindowHandle, struct Mode)");
set_draw= dll.api("set_draw","int(int WindowHandle, string Mode)");
T_select_obj= dll.api("T_select_obj","int(ADDR Objects, ADDR& ObjectSelected, struct Index)");
select_obj= dll.api("select_obj","int(ADDR Objects, ADDR& ObjectSelected, int Index)");
T_get_contour_global_attrib_xld= dll.api("T_get_contour_global_attrib_xld","int(ADDR Contour, string Name, struct& Attrib)");
T_fit_circle_contour_xld= dll.api("T_fit_circle_contour_xld","int(ADDR Contours, struct Algorithm, struct MaxNumPoints, struct MaxClosureDist, struct ClippingEndPoints, struct Iterations, struct ClippingFactor, struct& Row, struct& Column, struct& Radius, struct& StartPhi, struct& EndPhi, struct& PointOrder)");
fit_circle_contour_xld= dll.api("fit_circle_contour_xld","int(ADDR Contours, string Algorithm, int MaxNumPoints, double MaxClosureDist, int ClippingEndPoints, int Iterations, double ClippingFactor, double& Row, double& Column, double& Radius, double& StartPhi, double& EndPhi, string& PointOrder)");
T_gen_circle_contour_xld= dll.api("T_gen_circle_contour_xld","int(ADDR& ContCircle, struct Row, struct Column, struct Radius, struct StartPhi, struct EndPhi, struct PointOrder, struct Resolution)");
gen_circle_contour_xld= dll.api("gen_circle_contour_xld","int(ADDR& ContCircle, double Row, double Column, double Radius, double StartPhi, double EndPhi, string PointOrder, double Resolution)");
T_get_contour_attrib_xld= dll.api("T_get_contour_attrib_xld","int(ADDR Contour, string Name, struct& Attrib)");
T_set_line_width= dll.api("T_set_line_width","int(struct WindowHandle, struct Width)");
set_line_width= dll.api("set_line_width","int(int WindowHandle, double Width)");

 
//console.open();
mainForm.button.oncommand = function(id,event){
     
    var ret,Image = read_image(0,"res/double_circle");
    var ret,width,height = get_image_size(Image,0,0);
    var ret,Hwindow = open_window(0,0,width,height,mainForm.picturebox.hwnd,"visible", "",0);
    var ret = disp_obj(Image,Hwindow);
    win.delay(1000)
    var ret,Region = fast_threshold(Image,0,0,120,7);
    set_colored(Hwindow,6);
    disp_region(Region,Hwindow)
    win.delay(1000)
    var ret,RegionBorder = boundary(Region,0,"inner");
    var ret,RegionClipped = clip_region_rel(RegionBorder,0,5,5,5,5);
    var ret,RegionDilation = dilation_circle(RegionClipped,0,2.5);
    var ret,ImageReduced = reduce_domain(Image,RegionDilation,0);
    var ret = disp_obj(ImageReduced,Hwindow);
    win.delay(1000)
    var ret,Edges = edges_sub_pix(ImageReduced,0,"canny",2,20,60);
    var ret = disp_obj(Edges,Hwindow);
    win.delay(1000)
    var ret,ContoursSplit = segment_contours_xld(Edges,0,"lines_circles",5,4,3);
    //var ret = disp_obj(ContoursSplit,Hwindow);
    var ret = disp_obj(Image,Hwindow);
    var ret,Number = count_obj(ContoursSplit,0);
    //mainForm.snumber.text = Number;
    set_draw(Hwindow,"margin");
    set_color(Hwindow,"orange red");
    set_line_width(Hwindow,4.0);
    for(i=1;Number;1){
        var ret,ObjectSelected = select_obj(ContoursSplit,0,i);
        //var ret,Attrib = T_get_contour_global_attrib_xld(ObjectSelected,"cont_approx",{});
        //if(Attrib > 0){
            var ret,Row, Column, Radius, StartPhi, EndPhi, PointOrder = fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, 0, 0, 0, 0, 0, 0)
            var ret,ContCircle = gen_circle_contour_xld (0, Row, Column, Radius, 0, math.rad(360), 'positive', 1.0)
            var ret = disp_obj(ContCircle,Hwindow);
            win.delay(1000)
       // }
    }
}
 
mainForm.enableDpiScaling();
mainForm.show();
 
return win.loopMessage();



登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...