请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 13580|回复: 2

C#+Halcon实现图像平移和缩放

[复制链接]

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
发表于 2019-9-2 22:55:58 | 显示全部楼层 |阅读模式
C#+Halcon实现图像标注:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using HalconDotNet;

  11. namespace image_to_label
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         HTuple WindowID, ImageWidth, ImageHeight;
  16.         private double RowDown;//鼠标按下时的行坐标
  17.         private double ColDown;//鼠标按下时的列坐标
  18.         HObject ho_image;      //图像变量
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.             WindowID = hWindowControl1.HalconWindow;
  23.         }

  24.         private void bt_openImage_Click(object sender, EventArgs e)
  25.         {
  26.             OpenFileDialog openFileDialog = new OpenFileDialog();
  27.             //openFileDialog.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*";
  28.             openFileDialog.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
  29.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  30.             {
  31.                 HTuple ImagePath = openFileDialog.FileName;
  32.                 HOperatorSet.ReadImage(out ho_image, ImagePath);
  33.             }
  34.             HOperatorSet.GetImageSize(ho_image, out ImageWidth, out ImageHeight);
  35.             HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight, ImageWidth);
  36.             HOperatorSet.DispObj(ho_image, WindowID);
  37.         }

  38.         private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
  39.         {
  40.             HTuple Row, Column, Button;
  41.             HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);
  42.             RowDown = Row;    // 鼠标按下时的行坐标
  43.             ColDown = Column;
  44.         }

  45.         private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
  46.         {
  47.             HTuple Row, Column, Button, pointGray;
  48.             HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);              //获取当前鼠标的坐标值
  49.             if (ImageHeight != null && (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth))//设置3个条件项,防止程序崩溃。
  50.             {
  51.                 HOperatorSet.GetGrayval(ho_image, Row, Column, out pointGray);                 //获取当前点的灰度值
  52.             }
  53.             else
  54.             {
  55.                 pointGray = "_";
  56.             }
  57.             String str = String.Format("Row:{0}  Column:{1}  Gray:{2}", Row, Column, pointGray); //格式化字符串
  58.             label_xyRGB.Text = str;
  59.         }

  60.         private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
  61.         {
  62.             HTuple row1, col1, row2, col2, Row, Column, Button;
  63.             HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);
  64.             double RowMove = Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
  65.             double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
  66.             HOperatorSet.GetPart(WindowID, out row1, out col1, out row2, out col2);//得到当前的窗口坐标
  67.             HOperatorSet.SetPart(WindowID, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);//这里可能有些不好理解。以左上角原点为参考点
  68.             HOperatorSet.ClearWindow(WindowID);
  69.             if (ImageHeight != null)
  70.             {
  71.                 HOperatorSet.DispObj(ho_image, WindowID);
  72.             }
  73.             else
  74.             {
  75.                 MessageBox.Show("请加载一张图片");
  76.             }
  77.         }

  78.         private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
  79.         {
  80.             HTuple Zoom, Row, Col, Button;
  81.             HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
  82.             if (e.Delta > 0)
  83.             {
  84.                 Zoom = 1.5;
  85.             }
  86.             else
  87.             {
  88.                 Zoom = 0.5;
  89.             }
  90.             HOperatorSet.GetMposition(WindowID, out Row, out Col, out Button);
  91.             HOperatorSet.GetPart(WindowID, out Row0, out Column0, out Row00, out Column00);
  92.             Ht = Row00 - Row0;
  93.             Wt = Column00 - Column0;
  94.             if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
  95.             {
  96.                 r1 = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0)));
  97.                 c1 = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0)));
  98.                 r2 = r1 + (Ht / Zoom);
  99.                 c2 = c1 + (Wt / Zoom);
  100.                 HOperatorSet.SetPart(WindowID, r1, c1, r2, c2);
  101.                 HOperatorSet.ClearWindow(WindowID);
  102.                 HOperatorSet.DispObj(ho_image, WindowID);
  103.             }
  104.         }

  105.         private void bt_ResetImage_Click(object sender, EventArgs e)
  106.         {
  107.             HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight - 1, ImageWidth - 1);
  108.             HOperatorSet.ClearWindow(WindowID);
  109.             HOperatorSet.DispObj(ho_image, WindowID);
  110.         }


  111.     }
  112. }
复制代码

参考:
【1】C# 简易实现图片的缩放以及图片的平移
【2】Halcon+Winform实现图像缩放等功能
【3】C#与Halcon联合编程实现鼠标拖动图片
【4】获取鼠标事件


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
 楼主| 发表于 2019-11-5 22:50:05 | 显示全部楼层
  1. dev_update_off()
  2. dev_set_draw('margin')
  3. dev_close_window()
  4. dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
  5. read_image(Image, 'printer_chip/printer_chip_01')
  6. get_mbutton(WindowHandle, Row, Column, Button)

  7. * 画圆
  8. draw_circle(WindowHandle, Row1, Column1, Radius)
  9. gen_circle(Circle, Row1, Column1, Radius)

  10. * 交互式画圆
  11. read_image(Image,'monkey')
  12. draw_circle_mod(WindowHandle,20,20,15,Row,Column,Radius)
  13. gen_circle(Circle,Row,Column,Radius)
  14. reduce_domain(Image,Circle,ImageReduced)
  15. invert_image (ImageReduced, ImageInvert)
  16. dev_display (ImageInvert)
复制代码
回复 支持 反对

使用道具 举报

0

主题

16

帖子

1

金钱

新手上路

Rank: 1

积分
8
发表于 2020-8-21 22:47:24 | 显示全部楼层
谢谢楼主的分享,好好学习一下:lol
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-28 22:36 , Processed in 0.230730 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表