少有人走的路

勇哥的工业自动化技术网站

解决HALCON中的分割显示问题

今天终于解决了困扰我很久的一个问题,在VC中调用HALCON中的分割函数后,在最后返回显示时总是报错,让我郁闷了很久,Undefined gray in get_image_pointer3 或Undefined gray in get_image_pointer。

      原来问题出在对于bin_threshold、threshold等这些分割函数的返回值上面,把返回值当成Image变量直接赋给图像数据buffer,今天通过多次试验发现bin_threshold、threshold返回的是被分割后的Regions,因此从Rgions获取get_image_pointer1或get_image_pointer3时就会报错,提示函数没有灰度值。            这时需要将分割后的Regions连接起来生成一个大的区域,然后将区域进行相关操作转化成Image,HALCON提供了三种方法:region_to_bin、region_to_label、region_to_mean.

1. region_to_bin(Region,BinImage,ForegroundGray,BackgroundGray,Width,Height)

它将一个区域转化成一个二进制字节图像。给区域内的所有像素赋给前景灰度值,如果输入区域大于生成的图像,则会在图像边界处截断;


2. region_to_label(Region,ImageLabel,Type,Width,Height)

它将区域转化为一个标签图像,通过索引值:第一个区域赋予灰度值1,第二个区域赋予灰度值2,依此类推……这里仅仅使用正的灰度值,直到256。区域大于生成图像则会被适当地截断。如果区域重叠,则较高值的图像会被输出。如果想重叠,可以调用expand_region进行处理。Type='int2'、'int4'、‘byte'

3. region_to_mean(Regions,Image,ImageMean)

用它们的均值来填充图像区域,返回Image。这个操作符主要用来可视化分割结果(正是我想要的^_^)       


在VC中调用,分别针对256色灰度图像和24位灰度图像分别调用不同的函数,如下面例子所示:

针对256色灰度图像,只是截取主要部分程序,其他只是打开一幅图像,将图像数据放到m_pImageBuffer中。

 char Type[MAX_STRING];gen_image1(&hImage,"byte",width,height,(long)m_pImageBuffer); //将缓冲区的数据生成一幅Image

threshold(hImage,&Region,160,255); //对Image进行阈值分割

connection(Region,&ConnectedRegions); //将小区域连接成一个大区域

region_to_mean(ConnectedRegions,hImage,&ImageMean);  //转化为Image

get_image_pointer1(ImageMean,(long*)&m_pRed,Type,&width1,&height1); //从HALCON的Image中获得处理后的图像数据

for(i=size-1,j=0;i>=0;i--,j++)     

m_pImageBuffer[j]=m_pRed[size-i-1];  //将数据放回buffer中,返回显示出结果 


针对24位RGB图像

ExtractRGB24Channels(m_pImageBuffer,m_pRed,m_pGreen,m_pBlue); //从打开的图像数据中分别提取出R、G、B分量,用于生成HALCON的Imagegen_image3(&hImage,"byte",width,height,(long)m_pRed,(long)m_pGreen,(long)Blue);  //获得Image

threshold(hImage,&Region,160,255); //对Image进行阈值分割

connection(Region,&ConnectedRegions); //将小区域连接成一个大区域

region_to_mean(ConnectedRegions,hImage,&ImageMean);  //转化为Image

get_image_pointer3(ImageMean,(long*)&m_pRed,(long*)&m_pGreen,(long*)&m_pBlue,Type,&width1,&height1); //从ImageMean中获得R、G、B三个分量的像素值

ComposeRGB24(m_pRed,m_pGreen,m_pBlue,m_pImageBuffer); //将处理后的图像数据放到buffer中,返回显示处理结果 

附:

//从24位位图数据中提取红、绿、蓝三个分量

BOOL CTest1Doc::ExtractRGB24Channels(BYTE *data, BYTE *pRed, BYTE *pGreen, BYTE *pBlue)
{
 int i,j,size;
 size = width*height; 
 for (i=size-1,j=0; i>= 0; i--,j+=3)
 {
  pBlue[i] = data[j];
 }
 for (i=size-1,j=1; i>=0; i--,j+=3)
 {
  pGreen[i] = data[j];
 }
 for (i=size-1,j=2; i>=0; i--,j+=3)
 {
  pRed[i] = data[j];
 
 return TRUE;
}//将红、绿、蓝三个分量合成24位位图数据
BOOL CTest1Doc::ComposeRGB24(BYTE *pRed, BYTE *pGreen, BYTE *pBlue, BYTE *data)
{
 int i,j,size;
 size = width*height; 
 for (i=size-1,j=0; i>= 0; i--,j+=3)
 {
  data[j] = pBlue[i];
 }
 for (i=size-1,j=1; i>=0; i--,j+=3)
 {
  data[j] = pGreen[i];
 }
 for (i=size-1,j=2; i>=0; i--,j+=3)
 {
  data[j] = pRed[i];
 }
 return TRUE;
}
 


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2026年1月    »
1234
567891011
12131415161718
19202122232425
262728293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864