【 HALCON 】 将多张图片按指定位置合并平铺成一张图,读取并显示多张图片
此方法旨在满足部分区域拍照后,想输出成一张图,对重叠区域要求不是很高。区别于图像拼接方法,请注意。
在这里,我在一个文件夹中保存了9张图,遍历读取,从而合成显示。
图片合成方法
-
public void ImageJigsaw(string path = "C:/Users/team/Desktop/4/"
-
{
-
HObject ho_Rectangle;
-
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
-
HTuple hv_Rows = new HTuple(), hv_Cols = new HTuple();
-
HOperatorSet.GenEmptyObj(out ho_Rectangle);
-
HOperatorSet.ReadImage(out h_Image, path + 1 + ".bmp");
-
hv_Width.Dispose();
-
hv_Height.Dispose();
-
HOperatorSet.GetImageSize(h_Image, out hv_Width, out hv_Height);
-
//生成一个3倍大小的空白图像
-
h_ImageJig.Dispose();
-
HOperatorSet.GenImageConst(out h_ImageJig, "byte", hv_Width * 3, hv_Height * 3);
-
//生成图像全部区域
-
ho_Rectangle.Dispose();
-
HOperatorSet.GenRectangle1(out ho_Rectangle, 0, 0, hv_Height - 1, hv_Width - 1);
-
//从区域生成所有的像素坐标
-
hv_Rows.Dispose();
-
hv_Cols.Dispose();
-
HOperatorSet.GetRegionPoints(ho_Rectangle, out hv_Rows, out hv_Cols);
-
for (int i = 1; i < 10; i++)
-
{
-
HObject ho_Image;
-
HTuple Rows = hv_Rows;
-
HTuple Cols = hv_Cols;
-
HTuple Width=new HTuple();
-
HTuple Height=new HTuple();
-
HTuple hv_Grayvals = new HTuple();
-
HOperatorSet.GenEmptyObj(out ho_Image);
-
//读取图像
-
ho_Image.Dispose();
-
HOperatorSet.ReadImage(out ho_Image, path + i + ".bmp");
-
HOperatorSet.Rgb1ToGray(ho_Image, out ho_Image);
-
//灰度值转化
-
HOperatorSet.GetImageSize(ho_Image,out Width,out Height);
-
HOperatorSet.SetPart(h_hWindowHandle,0,0,Height-1,Width-1);
-
HOperatorSet.DispObj(ho_Image, h_hWindowHandle);
-
//读取图像的全部像素
-
hv_Grayvals.Dispose();
-
HOperatorSet.GetGrayval(ho_Image, Rows, Cols, out hv_Grayvals);
-
switch (i)
-
{
-
//将所有像素坐标加上偏移量
-
case 1:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows;
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols;
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 2:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows;
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols + (hv_Width);
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 3:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows;
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols + (hv_Width * 2);
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 4:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows + (hv_Height);
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols + (hv_Width * 2);
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 5:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows + (hv_Height);
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols + (hv_Width);
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 6:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows + (hv_Height);
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols;
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 7:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows + (hv_Height * 2);
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols;
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 8:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows + (hv_Height * 2);
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols + (hv_Width);
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
case 9:
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Rows = Rows + (hv_Height * 2);
-
Rows.Dispose();
-
Rows = ExpTmpLocalVar_Rows;
-
}
-
}
-
using (HDevDisposeHelper dh = new HDevDisposeHelper())
-
{
-
{
-
HTuple
-
ExpTmpLocalVar_Cols = Cols + (hv_Width * 2);
-
Cols.Dispose();
-
Cols = ExpTmpLocalVar_Cols;
-
}
-
}
-
break;
-
}
-
//将图像的所有像素覆盖到空白图像的指定区域
-
HOperatorSet.SetGrayval(h_ImageJig, Rows, Cols, hv_Grayvals);
-
hv_Grayvals.Dispose();
-
ho_Image.Dispose();
-
Rows.Dispose();
-
Cols.Dispose();
-
}
-
hv_Width.Dispose();
-
hv_Height.Dispose();
-
HOperatorSet.GetImageSize(h_ImageJig, out hv_Width, out hv_Height);
-
HOperatorSet.SetPart(h_hWindowHandle, 0, 0, hv_Height, hv_Width);
-
HOperatorSet.DispObj(h_ImageJig, h_hWindowHandle);
-
hv_Rows.Dispose();
-
hv_Cols.Dispose();
-
ho_Rectangle.Dispose();
-
h_ImageJig.Dispose();
-
}
FORM调用显示效果
你要的HALCON源码
-
* 读图
-
ImageFiles := []
-
ImageFiles[0] := 'I:/Data_Nick/素材/4/1.bmp'
-
ImageFiles[1] := 'I:/Data_Nick/素材/4/2.bmp'
-
ImageFiles[2] := 'I:/Data_Nick/素材/4/3.bmp'
-
ImageFiles[3] := 'I:/Data_Nick/素材/4/4.bmp'
-
ImageFiles[4] := 'I:/Data_Nick/素材/4/5.bmp'
-
ImageFiles[5] := 'I:/Data_Nick/素材/4/6.bmp'
-
ImageFiles[6] := 'I:/Data_Nick/素材/4/7.bmp'
-
ImageFiles[7] := 'I:/Data_Nick/素材/4/8.bmp'
-
ImageFiles[8] := 'I:/Data_Nick/素材/4/9.bmp'
-
*根据图片大小创建空白图像
-
read_image (Image, ImageFiles[0])
-
get_image_size (Image, Width, Height)
-
gen_image_const (ImageJig, 'byte', Width*3, Height*3)
-
*获得单个图片的所有像素坐标
-
gen_rectangle1 (Rectangle, 0, 0, Height-1, Width-1)
-
get_region_points (Rectangle, Rows, Columns)
-
*对每张图片操作
-
for Index := 0 to |ImageFiles| - 1 by 1
-
RowsT:=0
-
ColsT:=0
-
read_image (Image, ImageFiles[Index])
-
*转灰
-
rgb1_to_gray (Image, GrayImage)
-
*根据像素点读取全部像素
-
get_grayval (GrayImage, Rows, Columns, Grayval)
-
*加偏移量,将所有像素点存入对应位置
-
switch (Index)
-
case 0:
-
RowsT:=Rows
-
ColsT:=Columns
-
break
-
case 1:
-
RowsT:=Rows
-
ColsT:=Columns+Width
-
break
-
case 2:
-
RowsT:=Rows
-
ColsT:=Columns+Width*2
-
break
-
case 3:
-
RowsT:=Rows+Height
-
ColsT:=Columns+Width*2
-
break
-
case 4:
-
RowsT:=Rows+Height
-
ColsT:=Columns+Width
-
break
-
case 5:
-
RowsT:=Rows+Height
-
ColsT:=Columns
-
break
-
case 6:
-
RowsT:=Rows+Height*2
-
ColsT:=Columns
-
break
-
case 7:
-
RowsT:=Rows+Height*2
-
ColsT:=Columns+Width
-
break
-
case 8:
-
RowsT:=Rows+Height*2
-
ColsT:=Columns+Width*2
-
break
-
endswitch
-
*填入像素
-
set_grayval (ImageJig, RowsT, ColsT, Grayval)
-
endfor
-
*显示合成图像
-
dev_display (ImageJig)