***************
*Tif转PNG
***************
*图片的编码信息
Z_MAX:=[]
Z_MIN:=[]
Z_SCALE:=[]
X_OFFSET:=0
X_SCALE:=1
Y_OFFSET:=0
Y_SCALE:=1
Z_SCALE:=[]
*************
*开始tif转PNG
read_image (InputImage, 'D:/00-halcon程序代码/Tif2PNG/12-03-15-43-20-652.tif')
count_seconds(t1)
get_image_size (InputImage, Width, Height)
**************************************************
*判断的三个标志位;标志位分别为于最后一行28、29、30处
flagRows:=[Height,Height,Height]
flagColumns:=[28,29,30]
flagValue:=[88,28888,58888]
***********************
*获取图像上最低点和最高点
threshold (InputImage, Region, -200, 200)
min_max_gray (Region, InputImage, 0, Z_MIN, Z_MAX, Range)
***********
*Z向放缩系数
SCALE:=65535/Range
*****************
*将图像转为16位图像
gen_image_proto (InputImage, ImageCleared, Z_MIN)
sub_image (InputImage, ImageCleared, ImageSub, SCALE, 0)
convert_image_type (ImageSub, Image2, 'uint2')
**************
*将两张图象融合
gen_image_const (OutImage, 'uint2', Width, Height+1)
gen_rectangle1(Rectangle, 0, 0, Height-1, Width-1)
get_region_points (Rectangle, Rows, Columns)
get_grayval_interpolated (Image2, Rows, Columns, 'bilinear', Grayval)
set_grayval (OutImage, Rows, Columns, Grayval)
************
*存储编码信息
if (Z_MAX<65)
Z_SCALE:=1000
Z_MAX:=Z_MAX*Z_SCALE
Z_MIN:=Z_MIN*Z_SCALE
else
Z_SCALE:=100
Z_MAX:=Z_MAX*Z_SCALE
Z_MIN:=Z_MIN*Z_SCALE
endif
codeRows:=[Height,Height,Height,Height,Height,Height,Height]
codeColumns:=[0,3,7,11,15,19,23]
codeValue:=[X_OFFSET,X_SCALE,Y_OFFSET,Y_SCALE,Z_MIN,Z_MAX,Z_SCALE]
set_grayval (OutImage, codeRows, codeColumns, codeValue)
******************
*三个标志位存储信息
set_grayval (OutImage, flagRows, flagColumns, flagValue)
count_seconds(t2)
time:=t2-t1
*******************************************************
*随机找两个点,对比一下实数型图片转为16位灰度图像素值的误差
Rows1:=[1456,1519]
Columns1:=[1878,1926]
get_grayval (Image2, Rows1, Columns1, Grayval1)
get_grayval (ImageSub, Rows1, Columns1, Grayval2)
write_image (OutImage, 'png', 0, 'D:/00-halcon程序代码/Tif2PNG/test.png')
***************
*PNG转Tif
***************
read_image (InputImage1,'D:/00-halcon程序代码/Tif2PNG/test.png')
**************************************
*根据标志位判断图片是否符合定义的编码格式
get_grayval (InputImage1, flagRows, flagColumns, Grayval3)
if (Grayval3==flagValue)
************
*读取编码信息
get_grayval (InputImage1, codeRows, codeColumns, codeGrayval)
********************************************************
*将整数数据转为float;转为C#代码可以忽略,因为C#转类型更方便
tuple_real (codeGrayval, Real)
********
*分项读取
X_OFFSET1:=Real[0]
X_SCALE1:=Real[1]
Y_OFFSET1:=Real[2]
Y_SCALE1:=Real[3]
Z_MIN1:=Real[4]
Z_MAX1:=Real[5]
Z_SCALE1:=Real[6]
Z_MAX1:=Z_MAX1/Z_SCALE1
Z_MIN1:=Z_MIN1/Z_SCALE1
else
return()
endif
******************************
*将图片最后一行存有编码信息的裁掉
crop_part (InputImage1, ImagePart, 0, 0, Width, Height)
gen_rectangle1 (Rectangle1, 0, 0, Height-1, Width-1)
get_region_points (Rectangle1, Rows2, Columns2)
get_grayval (ImagePart, Rows2, Columns2, Grayval4)
zoomFactor:=65535/(Z_MAX1-Z_MIN1)
Grayval5:=Grayval4/zoomFactor+Z_MIN1
gen_image_const (Image, 'real', Width, Height)
set_grayval (Image, Rows, Columns, Grayval5)
****************************************
*将转后的tif图和原始tif图读一些位置进行对比
*对比看看转换前后的精度丢失情况
testRows:=[1584,1599,192,10]
testColumns:=[1781,1993,1156,145]
get_grayval (InputImage, testRows, testColumns, Grayval6)
get_grayval (Image, testRows, testColumns, Grayval7)
这次在之前博客写的基础上优化而来,提升了转换后对比度,增加了防呆标志位。
将tif转为PNG后,再将PNG转为tif,随机取点进行前后对比,精度丢失为1um左右