Halcon控制台应用程序(可遍历多图)

 弹出的窗体与OpenCV不同,没有最大、最小、关闭。我们可以设置键盘事件退出,尤其在遍历多个图查看每个的效果时很方便。

注意opencv如果使用system("pause");不展示图,必须用waitKey();

VS新建控制台应用程序

#include<halconcpp\HalconCpp.h>
using namespace HalconCpp;

void main()
{
    HObject ho_Image;
    HTuple hv_WindowID;
    ReadImage(&ho_Image, "D:/sunflower.png");
    OpenWindow(0, 0, -1, -1, 0, "", "", &hv_WindowID);//-1,-1窗口大小适应图片,若设为200,200时,窗体大小固定
    DispObj(ho_Image, hv_WindowID);
    system("pause");
}

多图遍历展示,鼠标先点击下cmd窗口,再按Esc键

#include<conio.h> //键盘事件
#include<halconcpp\HalconCpp.h>
using namespace HalconCpp;

void main()
{
    HObject ho_Image, ho_GrayImage, ho_Regions;
    HTuple hv_WindowID;

    HTuple  hv_ImageFiles, hv_Index;
    //从路径读图
    ListFiles("D:/myPic", ((HTuple("files").Append("follow_links")).Append("recursive")), &hv_ImageFiles);
    TupleRegexpSelect(hv_ImageFiles, (HTuple("\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$").Append("ignore_case")), &hv_ImageFiles);
    {
        HTuple end_val2 = (hv_ImageFiles.TupleLength()) - 1;
        HTuple step_val2 = 1;
        for (hv_Index = 0; hv_Index.Continue(end_val2, step_val2); hv_Index += step_val2)
        {
            //图像处理程序
            ReadImage(&ho_Image, HTuple(hv_ImageFiles[hv_Index]));
            Rgb1ToGray(ho_Image, &ho_GrayImage);
            Threshold(ho_GrayImage, &ho_Regions, 45, 55);
            SetWindowAttr("background_color", "black");//窗体背景色为黑
            OpenWindow(0, 0, -1, -1, 0, "", "", &hv_WindowID);//-1,-1窗口大小适应图片
            DispObj(ho_Image, hv_WindowID);
            SetColor(hv_WindowID, "red");//设置二值图目标的颜色(在显示之前)
            DispObj(ho_Regions, hv_WindowID);
            //Esc键切换下一张图
            while (true)
            {
                if (_getch() == 27){
                    CloseWindow(hv_WindowID);
                    break;//跳出while循环体
                }
            }
        }
    }
}

 对应的halcon程序(无键盘事件)

list_files ('D:/myPic', ['files','follow_links','recursive'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    * Image Acquisition 01: Do something
    rgb1_to_gray (Image, GrayImage)
    threshold (GrayImage, Regions, 45, 55)
    dev_open_window (0, 0, -1, -1, 'black', WindowHandle1)
    dev_display (GrayImage)
    dev_set_color ('red')
    dev_display (Regions)
    dev_close_window ()
endfor

 

posted @ 2020-04-20 17:53  夕西行  阅读(583)  评论(0编辑  收藏  举报