实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using HalconDotNet;
using ViewROI;
using System.Collections;
using HalconDotNet;
using ViewROI;
using System.Collections;
namespace ViewROI
{
{
public delegate void FuncROIDelegate();
/// <summary>
/// This class creates and manages ROI objects. It responds
/// to mouse device inputs using the methods mouseDownAction and
/// mouseMoveAction. You don't have to know this class in detail when you
/// build your own C# project. But you must consider a few things if
/// you want to use interactive ROIs in your application: There is a
/// quite close connection between the ROIController and the HWndCtrl
/// class, which means that you must 'register' the ROIController
/// with the HWndCtrl, so the HWndCtrl knows it has to forward user input
/// (like mouse events) to the ROIController class.
/// The visualization and manipulation of the ROI objects is done
/// by the ROIController.
/// This class provides special support for the matching
/// applications by calculating a model region from the list of ROIs. For
/// this, ROIs are added and subtracted according to their sign.
/// </summary>
public class ROIController
{
public delegate void UpdateEventHandler(int handle);
public event UpdateEventHandler ROI_Update;
/// This class creates and manages ROI objects. It responds
/// to mouse device inputs using the methods mouseDownAction and
/// mouseMoveAction. You don't have to know this class in detail when you
/// build your own C# project. But you must consider a few things if
/// you want to use interactive ROIs in your application: There is a
/// quite close connection between the ROIController and the HWndCtrl
/// class, which means that you must 'register' the ROIController
/// with the HWndCtrl, so the HWndCtrl knows it has to forward user input
/// (like mouse events) to the ROIController class.
/// The visualization and manipulation of the ROI objects is done
/// by the ROIController.
/// This class provides special support for the matching
/// applications by calculating a model region from the list of ROIs. For
/// this, ROIs are added and subtracted according to their sign.
/// </summary>
public class ROIController
{
public delegate void UpdateEventHandler(int handle);
public event UpdateEventHandler ROI_Update;
/// <summary>
/// Constant for setting the ROI mode: positive ROI sign.
/// </summary>
public const int MODE_ROI_POS = 21;
/// <summary>
/// Constant for setting the ROI mode: negative ROI sign.
/// </summary>
public const int MODE_ROI_NEG = 22;
/// Constant for setting the ROI mode: negative ROI sign.
/// </summary>
public const int MODE_ROI_NEG = 22;
/// <summary>
/// Constant for setting the ROI mode: no model region is computed as
/// the sum of all ROI objects.
/// </summary>
public const int MODE_ROI_NONE = 23;
/// Constant for setting the ROI mode: no model region is computed as
/// the sum of all ROI objects.
/// </summary>
public const int MODE_ROI_NONE = 23;
/// <summary>Constant describing an update of the model region</summary>
public const int EVENT_UPDATE_ROI = 50;
public const int EVENT_UPDATE_ROI = 50;
public const int EVENT_CHANGED_ROI_SIGN = 51;
/// <summary>Constant describing an update of the model region</summary>
public const int EVENT_MOVING_ROI = 52;
public const int EVENT_MOVING_ROI = 52;
public const int EVENT_DELETED_ACTROI = 53;
public const int EVENT_DELETED_ALL_ROIS = 54;
public const int EVENT_ACTIVATED_ROI = 55;
public const int EVENT_CREATED_ROI = 56;
private ROI roiMode;
private int stateROI;
private double currX, currY;
/// <summary>Index of the active ROI object</summary>
public int activeROIidx;
public int deletedIdx;
/// <summary>List containing all created ROI objects so far</summary>
public ArrayList ROIList;
public ArrayList ROIList;
/// <summary>
/// Region obtained by summing up all negative
/// and positive ROI objects from the ROIList
/// </summary>
public HRegion ModelROI;
/// Region obtained by summing up all negative
/// and positive ROI objects from the ROIList
/// </summary>
public HRegion ModelROI;
private string activeCol = "green";
private string activeHdlCol = "red";
private string inactiveCol = "yellow";
private string activeHdlCol = "red";
private string inactiveCol = "yellow";
/// <summary>
/// Reference to the HWndCtrl, the ROI Controller is registered to
/// </summary>
public HWndCtrl viewController;
/// Reference to the HWndCtrl, the ROI Controller is registered to
/// </summary>
public HWndCtrl viewController;
/// <summary>
/// Delegate that notifies about changes made in the model region
/// </summary>
public IconicDelegate NotifyRCObserver;
/// Delegate that notifies about changes made in the model region
/// </summary>
public IconicDelegate NotifyRCObserver;
/// <summary>Constructor</summary>
public ROIController()
{
stateROI = MODE_ROI_NONE;
ROIList = new ArrayList();
activeROIidx = -1;
ModelROI = new HRegion();
NotifyRCObserver = new IconicDelegate(dummyI);
deletedIdx = -1;
currX = currY = -1;
}
public ROIController()
{
stateROI = MODE_ROI_NONE;
ROIList = new ArrayList();
activeROIidx = -1;
ModelROI = new HRegion();
NotifyRCObserver = new IconicDelegate(dummyI);
deletedIdx = -1;
currX = currY = -1;
}
/// <summary>Registers the HWndCtrl to this ROIController instance</summary>
public void setViewController(HWndCtrl view)
{
viewController = view;
}
public void setViewController(HWndCtrl view)
{
viewController = view;
}
/// <summary>Gets the ModelROI object</summary>
public HRegion getModelRegion()
{
return ModelROI;
}
public HRegion getModelRegion()
{
return ModelROI;
}
/// <summary>Gets the List of ROIs created so far</summary>
public ArrayList getROIList()
{
return ROIList;
}
public ArrayList getROIList()
{
return ROIList;
}
/// <summary>Get the active ROI</summary>
public ROI getActiveROI()
{
if (activeROIidx != -1)
return ((ROI)ROIList[activeROIidx]);
public ROI getActiveROI()
{
if (activeROIidx != -1)
return ((ROI)ROIList[activeROIidx]);
return null;
}
}
public int getActiveROIIdx()
{
return activeROIidx;
}
{
return activeROIidx;
}
public void setActiveROIIdx(int active)
{
activeROIidx = active;
}
{
activeROIidx = active;
}
public int getDelROIIdx()
{
return deletedIdx;
}
{
return deletedIdx;
}
/// <summary>
/// To create a new ROI object the application class initializes a
/// 'seed' ROI instance and passes it to the ROIController.
/// The ROIController now responds by manipulating this new ROI
/// instance.
/// </summary>
/// <param name="r">
/// 'Seed' ROI object forwarded by the application forms class.
/// </param>
public void setROIShape(ROI r)
{
roiMode = r;
roiMode.setOperatorFlag(stateROI);
}
/// To create a new ROI object the application class initializes a
/// 'seed' ROI instance and passes it to the ROIController.
/// The ROIController now responds by manipulating this new ROI
/// instance.
/// </summary>
/// <param name="r">
/// 'Seed' ROI object forwarded by the application forms class.
/// </param>
public void setROIShape(ROI r)
{
roiMode = r;
roiMode.setOperatorFlag(stateROI);
}
/// <summary>
/// Sets the sign of a ROI object to the value 'mode' (MODE_ROI_NONE,
/// MODE_ROI_POS,MODE_ROI_NEG)
/// </summary>
public void setROISign(int mode)
{
stateROI = mode;
if (activeROIidx != -1)
{
((ROI)ROIList[activeROIidx]).setOperatorFlag(stateROI);
viewController.repaint();
NotifyRCObserver(ROIController.EVENT_CHANGED_ROI_SIGN);
}
}
{
((ROI)ROIList[activeROIidx]).setOperatorFlag(stateROI);
viewController.repaint();
NotifyRCObserver(ROIController.EVENT_CHANGED_ROI_SIGN);
}
}
/// <summary>
/// Removes the ROI object that is marked as active.
/// If no ROI object is active, then nothing happens.
/// </summary>
public void removeActive()
{
if (activeROIidx != -1)
{
ROIList.RemoveAt(activeROIidx);
deletedIdx = activeROIidx;
activeROIidx = -1;
viewController.repaint();
NotifyRCObserver(EVENT_DELETED_ACTROI);
}
}
/// Removes the ROI object that is marked as active.
/// If no ROI object is active, then nothing happens.
/// </summary>
public void removeActive()
{
if (activeROIidx != -1)
{
ROIList.RemoveAt(activeROIidx);
deletedIdx = activeROIidx;
activeROIidx = -1;
viewController.repaint();
NotifyRCObserver(EVENT_DELETED_ACTROI);
}
}
/// <summary>
/// Calculates the ModelROI region for all objects contained
/// in ROIList, by adding and subtracting the positive and
/// negative ROI objects.
/// </summary>
public bool defineModelROI()
{
HRegion tmpAdd, tmpDiff, tmp;
double row, col;
if (stateROI == MODE_ROI_NONE)
return true;
return true;
tmpAdd = new HRegion();
tmpDiff = new HRegion();
tmpAdd.GenEmptyRegion();
tmpDiff.GenEmptyRegion();
tmpDiff = new HRegion();
tmpAdd.GenEmptyRegion();
tmpDiff.GenEmptyRegion();
for (int i=0; i < ROIList.Count; i )
{
switch (((ROI)ROIList[i]).getOperatorFlag())
{
case ROI.POSITIVE_FLAG:
tmp = ((ROI)ROIList[i]).getRegion();
tmpAdd = tmp.Union2(tmpAdd);
break;
case ROI.NEGATIVE_FLAG:
tmp = ((ROI)ROIList[i]).getRegion();
tmpDiff = tmp.Union2(tmpDiff);
break;
default:
break;
}//end of switch
}//end of for
{
switch (((ROI)ROIList[i]).getOperatorFlag())
{
case ROI.POSITIVE_FLAG:
tmp = ((ROI)ROIList[i]).getRegion();
tmpAdd = tmp.Union2(tmpAdd);
break;
case ROI.NEGATIVE_FLAG:
tmp = ((ROI)ROIList[i]).getRegion();
tmpDiff = tmp.Union2(tmpDiff);
break;
default:
break;
}//end of switch
}//end of for
ModelROI = null;
if (tmpAdd.AreaCenter(out row, out col) > 0)
{
tmp = tmpAdd.Difference(tmpDiff);
if (tmp.AreaCenter(out row, out col) > 0)
ModelROI = tmp;
}
{
tmp = tmpAdd.Difference(tmpDiff);
if (tmp.AreaCenter(out row, out col) > 0)
ModelROI = tmp;
}
//in case the set of positiv and negative ROIs dissolve
if (ModelROI == null || ROIList.Count == 0)
return false;
if (ModelROI == null || ROIList.Count == 0)
return false;
return true;
}
}
/// <summary>
/// Clears all variables managing ROI objects
/// </summary>
public void reset()
{
ROIList.Clear();
activeROIidx = -1;
ModelROI = null;
roiMode = null;
NotifyRCObserver(EVENT_DELETED_ALL_ROIS);
}
/// <summary>
/// Deletes this ROI instance if a 'seed' ROI object has been passed
/// to the ROIController by the application class.
///
/// </summary>
public void resetROI()
{
activeROIidx = -1;
roiMode = null;
}
/// <summary>Defines the colors for the ROI objects</summary>
/// <param name="aColor">Color for the active ROI object</param>
/// <param name="inaColor">Color for the inactive ROI objects</param>
/// <param name="aHdlColor">
/// Color for the active handle of the active ROI object
/// </param>
public void setDrawColor(string aColor,
string aHdlColor,
string inaColor)
{
if (aColor != "")
activeCol = aColor;
if (aHdlColor != "")
activeHdlCol = aHdlColor;
if (inaColor != "")
inactiveCol = inaColor;
}
/// <param name="aColor">Color for the active ROI object</param>
/// <param name="inaColor">Color for the inactive ROI objects</param>
/// <param name="aHdlColor">
/// Color for the active handle of the active ROI object
/// </param>
public void setDrawColor(string aColor,
string aHdlColor,
string inaColor)
{
if (aColor != "")
activeCol = aColor;
if (aHdlColor != "")
activeHdlCol = aHdlColor;
if (inaColor != "")
inactiveCol = inaColor;
}
/// <summary>
/// Paints all objects from the ROIList into the HALCON window
/// </summary>
/// <param name="window">HALCON window</param>
public void paintData(HalconDotNet.HWindow window)
{
window.SetDraw("margin");
window.SetLineWidth(1);
if (ROIList.Count > 0)
{
window.SetColor(inactiveCol);
window.SetDraw("margin");
{
window.SetColor(inactiveCol);
window.SetDraw("margin");
for (int i=0; i < ROIList.Count; i )
{
window.SetLineStyle(((ROI)ROIList[i]).flagLineStyle);
((ROI)ROIList[i]).draw(window);
{
window.SetLineStyle(((ROI)ROIList[i]).flagLineStyle);
((ROI)ROIList[i]).draw(window);
//BOBBY
//window.SetFont("-灿砰-32-16-0-0-0-0-ANSI_CHARSET-");
window.SetTposition((int)((ROIRectangle1)ROIList[i]).row1-15, (int)((ROIRectangle1)ROIList[i]).col1);
window.WriteString(((ROIRectangle1)ROIList[i]).Name);
//window.SetFont("-灿砰-32-16-0-0-0-0-ANSI_CHARSET-");
window.SetTposition((int)((ROIRectangle1)ROIList[i]).row1-15, (int)((ROIRectangle1)ROIList[i]).col1);
window.WriteString(((ROIRectangle1)ROIList[i]).Name);
}
if (activeROIidx != -1)
{
window.SetColor(activeCol);
window.SetLineStyle(((ROI)ROIList[activeROIidx]).flagLineStyle);
((ROI)ROIList[activeROIidx]).draw(window);
{
window.SetColor(activeCol);
window.SetLineStyle(((ROI)ROIList[activeROIidx]).flagLineStyle);
((ROI)ROIList[activeROIidx]).draw(window);
window.SetColor(activeHdlCol);
((ROI)ROIList[activeROIidx]).displayActive(window);
}
}
}
((ROI)ROIList[activeROIidx]).displayActive(window);
}
}
}
/// <summary>
/// Reaction of ROI objects to the 'mouse button down' event: changing
/// the shape of the ROI and adding it to the ROIList if it is a 'seed'
/// ROI.
/// </summary>
/// <param name="imgX">x coordinate of mouse event</param>
/// <param name="imgY">y coordinate of mouse event</param>
/// <returns></returns>
public int mouseDownAction(double imgX, double imgY)
{
int idxROI= -1;
double max = 10000, dist = 0;
double epsilon = 35.0; //maximal shortest distance to one of
//the handles
/// Reaction of ROI objects to the 'mouse button down' event: changing
/// the shape of the ROI and adding it to the ROIList if it is a 'seed'
/// ROI.
/// </summary>
/// <param name="imgX">x coordinate of mouse event</param>
/// <param name="imgY">y coordinate of mouse event</param>
/// <returns></returns>
public int mouseDownAction(double imgX, double imgY)
{
int idxROI= -1;
double max = 10000, dist = 0;
double epsilon = 35.0; //maximal shortest distance to one of
//the handles
if (roiMode != null) //either a new ROI object is created
{
roiMode.createROI(imgX, imgY);
ROIList.Add(roiMode);
roiMode = null;
activeROIidx = ROIList.Count - 1;
viewController.repaint();
{
roiMode.createROI(imgX, imgY);
ROIList.Add(roiMode);
roiMode = null;
activeROIidx = ROIList.Count - 1;
viewController.repaint();
NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
}
else if (ROIList.Count > 0) // ... or an existing one is manipulated
{
activeROIidx = -1;
}
else if (ROIList.Count > 0) // ... or an existing one is manipulated
{
activeROIidx = -1;
for (int i =0; i < ROIList.Count; i )
{
dist = ((ROI)ROIList[i]).distToClosestHandle(imgX, imgY);
if ((dist < max) && (dist < epsilon))
{
max = dist;
idxROI = i;
}
}//end of for
{
dist = ((ROI)ROIList[i]).distToClosestHandle(imgX, imgY);
if ((dist < max) && (dist < epsilon))
{
max = dist;
idxROI = i;
}
}//end of for
if (idxROI >= 0)
{
activeROIidx = idxROI;
NotifyRCObserver(ROIController.EVENT_ACTIVATED_ROI);
}
{
activeROIidx = idxROI;
NotifyRCObserver(ROIController.EVENT_ACTIVATED_ROI);
}
viewController.repaint();
}
return activeROIidx;
}
}
return activeROIidx;
}
/// <summary>
/// Reaction of ROI objects to the 'mouse button move' event: moving
/// the active ROI.
/// </summary>
/// <param name="newX">x coordinate of mouse event</param>
/// <param name="newY">y coordinate of mouse event</param>
public void mouseMoveAction(double newX, double newY)
{
if ((newX == currX) && (newY == currY))
return;
/// Reaction of ROI objects to the 'mouse button move' event: moving
/// the active ROI.
/// </summary>
/// <param name="newX">x coordinate of mouse event</param>
/// <param name="newY">y coordinate of mouse event</param>
public void mouseMoveAction(double newX, double newY)
{
if ((newX == currX) && (newY == currY))
return;
((ROI)ROIList[activeROIidx]).moveByHandle(newX, newY);
viewController.repaint();
currX = newX;
currY = newY;
NotifyRCObserver(ROIController.EVENT_MOVING_ROI);
}
viewController.repaint();
currX = newX;
currY = newY;
NotifyRCObserver(ROIController.EVENT_MOVING_ROI);
}
/***********************************************************/
public void dummyI(int v)
{
if (v == 50)
{
if (ROI_Update != null)
{
//ROI_Update(((ROI)ROIList[activeROIidx]).getNumHandles());
ROI_Update(activeROIidx);
}
}
}
public int CreatROI(double imgX,double imgY)
{
roiMode.createROI(imgX, imgY);
ROIList.Add(roiMode);
roiMode = null;
activeROIidx = ROIList.Count - 1;
viewController.repaint();
{
roiMode.createROI(imgX, imgY);
ROIList.Add(roiMode);
roiMode = null;
activeROIidx = ROIList.Count - 1;
viewController.repaint();
NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
return activeROIidx;
}
}
public int CreatROI(double imgX, double imgY,int SizeX,int SizeY)
{
roiMode.createROI(imgX, imgY,SizeX,SizeY);
ROIList.Add(roiMode);
roiMode = null;
activeROIidx = ROIList.Count - 1;
viewController.repaint();
{
roiMode.createROI(imgX, imgY,SizeX,SizeY);
ROIList.Add(roiMode);
roiMode = null;
activeROIidx = ROIList.Count - 1;
viewController.repaint();
NotifyRCObserver(ROIController.EVENT_CREATED_ROI);
return activeROIidx;
}
}
}//end of class
}//end of namespace
}//end of namespace
好例子网口号:伸出你的我的手 — 分享!
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)