halcon部分:
*定义三维图的x、y的分辨率精度
xResolution:=0.04
yResolution:=0.04
*将深度图转为real类型三通道代表X、Y、Z的深度图
read_image (Image, 'E:/SMT电容虚焊测高/12-3测试图/12-03-15-34-58-155.tif')
get_image_size (Image, Width, Height)
*生成一副存real类型x坐标的单通道图
gen_image_const (Image1, 'real', Width, Height)
*生成一副存real类型y坐标的单通道图
gen_image_const (Image2, 'real', Width, Height)
*halcon 遍历图像的方式,针对单一像素进行操作
*不再是opencv专属
gen_rectangle1 (Rectangle, 0, 0, Height-1, Width-1)
get_region_points (Rectangle, Rows, Columns)
for Index := 0 to |Rows|-1 by 1
set_grayval (Image1, Rows[Index], Columns[Index], Rows[Index]*xResolution)
set_grayval (Image1, Rows[Index], Columns[Index], Rows[Index]*xResolution)
endfor
本来通过halcon来做,奈何halcon对像素操作太慢。改成opencv处理。
opencv部分:
处理根据.at方法进行处理,处理速度很快
通过
vector<Mat> ReadImg(cv::String path)
{
vector<cv::String> fn;
glob(path, fn, false);
vector<Mat> images;
size_t count = fn.size(); //number of png files in images folder
for (size_t i = 0; i < count; i++)
{
images.emplace_back(cv::imread(fn[i],2));
}
return images;
}
读取文件夹的图像。
通过
void ImageConvert()
{
float xResolution = 0.04;
float yResolution = 0.04;
cv::String path = "E:\\Halcon程度代码\\图像处理DEMO\\深度图转real类型xyz三通道\\x64\\Debug\\image\\*.tif";
vector<Mat> Img_Vec = ReadImage(path);
for (int i = 0; i < Img_Vec.size(); i++)
{
Mat src = Img_Vec[i];
Size sizeImg = src.size();
Mat tempImg(sizeImg, CV_32FC3);
for (int j = 0; j < src.rows; j++)
{
for (int k = 0; k < src.cols; k++)
{
tempImg.at<Vec3f>(j, k)[0] = xResolution*j;
tempImg.at<Vec3f>(j, k)[1] = yResolution*i;
tempImg.at<Vec3f>(j, k)[2] = src.at<float>(j, k);
}
}
imwrite(".\\image\\Img_" + to_string(i) + ".bmp", tempImg);
}
}
开始图像转换。
总结:针对算法的开发,halcon优势较为明显,但遇到像素遍历赋值的问题,opencv效率优势十分明显。
图像类型是CV_32FC3类型时,配合Vec3f使用。
图像类型是CV_64FC3类型时,配合Vec3d使用。