#include "stdafx.h"
|
#include "VisionBufferPro.h"
|
#include "Bspline.h"
|
|
#define DEFAULTT_IMAGE_HEIGHT 100000
|
|
CVisionBufferPro::CVisionBufferPro()
|
{
|
}
|
|
CVisionBufferPro::~CVisionBufferPro(){
|
/* code */
|
}
|
|
bool CVisionBufferPro::bufferToObject(DimensionDir eDir, int height, HalconCpp::HObject &hImage) {
|
/* code */
|
CVisionRecipe *pRecipe = CVisionRecipe::getInstance();
|
int iCam = pRecipe->getCameraIndex(eDir);
|
int iScan = pRecipe->getCameraScan(eDir);
|
CGrabberControl *pGrabCtrl = pRecipe->getGrabberControl(iCam);
|
if (NULL == pGrabCtrl) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
CFrameBufferController *pBuffer = pGrabCtrl->GetFrameBuffer();
|
if (nullptr == pBuffer) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
CMultiBuffer *pMultBuffer = pBuffer->GetMultiBuffer();
|
if (NULL == pMultBuffer) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
int nFrameWidth = pMultBuffer->GetFrameWidth();
|
int nFrameHeight = pMultBuffer->GetFrameHeight();
|
int nFrameCount = pMultBuffer->GetFrameCount();
|
int nTotalLength = nFrameHeight * nFrameCount;
|
if (nTotalLength < 1){
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
try{
|
int imgWidth = nFrameWidth;
|
int imgHeight = nTotalLength;
|
if (imgHeight > DEFAULTT_IMAGE_HEIGHT) {
|
imgHeight = DEFAULTT_IMAGE_HEIGHT;
|
}
|
if (height > 0) {
|
if (imgHeight > height) {
|
imgHeight = height;
|
}
|
}
|
|
//ͼÏñת»»
|
uchar *imgData = new uchar[imgWidth * imgHeight];
|
BYTE *lpImg = pBuffer->GetMultiBuffData(iScan);
|
std::memcpy(imgData, lpImg, imgWidth * imgHeight);
|
HalconCpp::GenImage1(&hImage, "byte", imgWidth, imgHeight, (Hlong)(imgData));
|
delete[] imgData;
|
imgData = NULL;
|
|
return true;
|
}
|
catch (...)
|
{
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::getImageROI(DimensionDir eDir, HalconCpp::HObject &hImage, int x1, int y1, int x2, int y2) {
|
/* code */
|
CVisionRecipe *pRecipe = CVisionRecipe::getInstance();
|
int iCam = pRecipe->getCameraIndex(eDir);
|
int iScan = pRecipe->getCameraScan(eDir);
|
CGrabberControl *pGrabCtrl = pRecipe->getGrabberControl(iCam);
|
if (NULL == pGrabCtrl) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
CFrameBufferController *pBuffer = pGrabCtrl->GetFrameBuffer();
|
if (nullptr == pBuffer) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
CMultiBuffer *pMultBuffer = pBuffer->GetMultiBuffer();
|
if (NULL == pMultBuffer) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
int nFrameWidth = pMultBuffer->GetFrameWidth();
|
int nFrameHeight = pMultBuffer->GetFrameHeight();
|
int nFrameCount = pMultBuffer->GetFrameCount();
|
int nTotalLength = nFrameWidth * nFrameHeight * nFrameCount;
|
if (nTotalLength < 1) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
if (x1 < 0 || y1 < 0 || x2 > nFrameWidth - 1 || y2 > nTotalLength - 1) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
try{
|
int imgWidth = x2 - x1 + 1;
|
int imgHeight = y2 - y1 + 1;
|
if (imgWidth < 1 || imgHeight < 1) {
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
|
//ͼÏñת»»
|
uchar *imgData = new uchar[imgWidth * imgHeight];
|
BYTE *lpImg = pBuffer->GetMultiBuffData(iScan);
|
|
for (int i = y1; i <= y2; i++) {
|
std::memcpy(imgData + (i - y1) * imgWidth, lpImg + (i * nFrameWidth) + x1, imgWidth);
|
}
|
|
|
HalconCpp::GenImage1(&hImage, "byte", imgWidth, imgHeight, (Hlong)(imgData));
|
delete[] imgData;
|
imgData = NULL;
|
|
return true;
|
}
|
catch (...)
|
{
|
ClsVision::GenEmptyObject(hImage);
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::getIndexImage(DimensionDir eDir, HalconCpp::HObject *hImage, int index) {
|
/* code */
|
CVisionRecipe *pRecipe = CVisionRecipe::getInstance();
|
int iCam = pRecipe->getCameraIndex(eDir);
|
int iScan = pRecipe->getCameraScan(eDir);
|
CGrabberControl *pGrabCtrl = pRecipe->getGrabberControl(iCam);
|
if (NULL == pGrabCtrl) {
|
HalconCpp::GenEmptyObj(hImage);
|
return false;
|
}
|
|
CFrameBufferController *pBuffer = pGrabCtrl->GetFrameBuffer();
|
if (nullptr == pBuffer) {
|
HalconCpp::GenEmptyObj(hImage);
|
return false;
|
}
|
CMultiBuffer *pMultBuffer = pBuffer->GetMultiBuffer();
|
if (NULL == pMultBuffer) {
|
HalconCpp::GenEmptyObj(hImage);
|
return false;
|
}
|
int nFrameHeight = pMultBuffer->GetFrameHeight();
|
int nFrameWidth = pMultBuffer->GetFrameWidth();
|
if (nFrameHeight < 1 || nFrameWidth < 1) {
|
HalconCpp::GenEmptyObj(hImage);
|
return false;
|
}
|
|
int x1 = 0;
|
int x2 = nFrameWidth - 1;
|
int y1 = index * nFrameHeight;
|
int y2 = index * nFrameHeight + nFrameHeight - 1;
|
|
return getImageROI(eDir, *hImage, x1, y1, x2, y2);
|
}
|
|
void CVisionBufferPro::getLineFrameSize(DimensionDir eDir, int &nFrameWidth, int &nFrameHeight) {
|
/* code */
|
// ÉèÖÃÖ¡¿íºÍ¸ß¶ÈΪ-1
|
nFrameWidth = -1;
|
nFrameHeight = -1;
|
// »ñÈ¡CVisionRecipeʵÀý
|
CVisionRecipe *pRecipe = CVisionRecipe::getInstance();
|
// »ñȡָ¶¨·½ÏòϵÄÏà»úË÷Òý
|
int iCam = pRecipe->getCameraIndex(eDir);
|
// »ñȡָ¶¨·½ÏòϵÄÏà»úɨÃè·½Ïò
|
int iScan = pRecipe->getCameraScan(eDir);
|
// »ñȡָ¶¨Ïà»úϵÄCGrabberControlʵÀý
|
CGrabberControl *pGrabCtrl = pRecipe->getGrabberControl(iCam);
|
// Èç¹ûpGrabCtrlΪ¿Õ£¬Ôò·µ»Ø
|
if (NULL == pGrabCtrl) return;
|
|
// »ñÈ¡CFrameBufferControllerʵÀý
|
CFrameBufferController *pBuffer = pGrabCtrl->GetFrameBuffer();
|
// Èç¹ûpBufferΪ¿Õ£¬Ôò·µ»Ø
|
if (nullptr == pBuffer) return;
|
// »ñÈ¡CMultiBufferʵÀý
|
CMultiBuffer *pMultBuffer = pBuffer->GetMultiBuffer();
|
// Èç¹ûpMultBufferΪ¿Õ£¬Ôò·µ»Ø
|
if (NULL == pMultBuffer)return;
|
|
// »ñȡ֡¸ß¶È
|
nFrameHeight = pMultBuffer->GetFrameHeight();
|
// »ñȡ֡¿í¶È
|
nFrameWidth = pMultBuffer->GetFrameWidth();
|
}
|
|
bool CVisionBufferPro::GetMeasurePos(HalconCpp::HObject &hObject, Line2D line, int threshold, int modeTrans, int step, Point2D &ptStart, Point2D &ptEnd) {
|
/* code */
|
if (!ClsVision::ObjectExist(hObject)) return false;
|
|
try {
|
HalconCpp::HTuple LineRowStart_Measure_01_0, LineColumnStart_Measure_01_0, LineRowEnd_Measure_01_0, LineColumnEnd_Measure_01_0;
|
LineRowStart_Measure_01_0 = line.pt0.y;
|
LineColumnStart_Measure_01_0 = line.pt0.x;
|
LineRowEnd_Measure_01_0 = line.pt1.y;
|
LineColumnEnd_Measure_01_0 = line.pt1.x;
|
|
HalconCpp::HTuple TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Dr, TmpCtrl_Dc, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2;
|
TmpCtrl_Row = 0.5*(LineRowStart_Measure_01_0 + LineRowEnd_Measure_01_0);
|
TmpCtrl_Column = 0.5*(LineColumnStart_Measure_01_0 + LineColumnEnd_Measure_01_0);
|
TmpCtrl_Dr = LineRowStart_Measure_01_0 - LineRowEnd_Measure_01_0;
|
TmpCtrl_Dc = LineColumnEnd_Measure_01_0 - LineColumnStart_Measure_01_0;
|
|
HalconCpp::TupleAtan2(TmpCtrl_Dr, TmpCtrl_Dc, &TmpCtrl_Phi);
|
|
HalconCpp::TupleSqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc, &TmpCtrl_Len1);
|
TmpCtrl_Len1 = 0.5 * TmpCtrl_Len1;
|
TmpCtrl_Len2 = 0.5 * step;
|
|
HalconCpp::HTuple hvWidth, hvHeight, MsrHandle_Measure_01_0;
|
HalconCpp::GetImageSize(hObject, &hvWidth, &hvHeight);
|
|
HalconCpp::GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, hvWidth, hvHeight, "nearest_neighbor", &MsrHandle_Measure_01_0);
|
|
HalconCpp::HTuple Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0;
|
std::string strMode = "all";
|
if (0 == modeTrans) {
|
strMode = "all";
|
}
|
else if (1 == modeTrans) {
|
strMode = "positive";
|
}
|
else if (2 == modeTrans) {
|
strMode = "negative";
|
}
|
HalconCpp::MeasurePos(hObject, MsrHandle_Measure_01_0, 1, threshold, strMode.c_str(), "all", &Row_Measure_01_0, &Column_Measure_01_0, &Amplitude_Measure_01_0, &Distance_Measure_01_0);
|
HalconCpp::CloseMeasure(MsrHandle_Measure_01_0);
|
|
bool result = false;
|
HalconCpp::HTuple hvLenth;
|
HalconCpp::TupleLength(Row_Measure_01_0, &hvLenth);
|
int iCount = hvLenth[0].I();
|
if (iCount > 0) {
|
double dx = abs(line.pt0.x - line.pt1.x);
|
bool isInit = false;
|
|
Point2D pt;
|
if (dx > 1.0f) {
|
for (int i = 0; i < iCount; i++) {
|
pt.x = Column_Measure_01_0[i].D();
|
pt.y = Row_Measure_01_0[i].D();
|
if (!isInit) {
|
ptStart.x = Column_Measure_01_0[i].D();
|
ptStart.y = Row_Measure_01_0[i].D();
|
ptEnd.x = Column_Measure_01_0[i].D();
|
ptEnd.y = Row_Measure_01_0[i].D();
|
isInit = true;
|
}
|
|
result = true;
|
if (pt.x < ptStart.x) ptStart = pt;
|
if (pt.x > ptEnd.x) ptEnd = pt;
|
}
|
}
|
else {
|
for (int i = 0; i < iCount; i++) {
|
pt.x = Column_Measure_01_0[i].D();
|
pt.y = Row_Measure_01_0[i].D();
|
if (!isInit) {
|
ptStart.x = Column_Measure_01_0[i].D();
|
ptStart.y = Row_Measure_01_0[i].D();
|
ptEnd.x = Column_Measure_01_0[i].D();
|
ptEnd.y = Row_Measure_01_0[i].D();
|
isInit = true;
|
}
|
|
result = true;
|
if (pt.y < ptStart.y) ptStart = pt;
|
if (pt.y > ptEnd.y) ptEnd = pt;
|
}
|
}
|
}
|
return result;
|
}
|
catch (...)
|
{
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::GetMeasureNearPos(HalconCpp::HObject& hObject, Line2D line, int threshold, int modeTrans, int step, Point2D& ptStart, Point2D& ptEnd) {
|
/* code */
|
if (!ClsVision::ObjectExist(hObject)) return false;
|
|
try {
|
HalconCpp::HTuple LineRowStart_Measure_01_0, LineColumnStart_Measure_01_0, LineRowEnd_Measure_01_0, LineColumnEnd_Measure_01_0;
|
LineRowStart_Measure_01_0 = line.pt0.y;
|
LineColumnStart_Measure_01_0 = line.pt0.x;
|
LineRowEnd_Measure_01_0 = line.pt1.y;
|
LineColumnEnd_Measure_01_0 = line.pt1.x;
|
|
HalconCpp::HTuple TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Dr, TmpCtrl_Dc, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2;
|
TmpCtrl_Row = 0.5 * (LineRowStart_Measure_01_0 + LineRowEnd_Measure_01_0);
|
TmpCtrl_Column = 0.5 * (LineColumnStart_Measure_01_0 + LineColumnEnd_Measure_01_0);
|
TmpCtrl_Dr = LineRowStart_Measure_01_0 - LineRowEnd_Measure_01_0;
|
TmpCtrl_Dc = LineColumnEnd_Measure_01_0 - LineColumnStart_Measure_01_0;
|
|
HalconCpp::TupleAtan2(TmpCtrl_Dr, TmpCtrl_Dc, &TmpCtrl_Phi);
|
|
HalconCpp::TupleSqrt(TmpCtrl_Dr * TmpCtrl_Dr + TmpCtrl_Dc * TmpCtrl_Dc, &TmpCtrl_Len1);
|
TmpCtrl_Len1 = 0.5 * TmpCtrl_Len1;
|
TmpCtrl_Len2 = 0.5 * step;
|
|
HalconCpp::HTuple hvWidth, hvHeight, MsrHandle_Measure_01_0;
|
HalconCpp::GetImageSize(hObject, &hvWidth, &hvHeight);
|
|
HalconCpp::GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, hvWidth, hvHeight, "nearest_neighbor", &MsrHandle_Measure_01_0);
|
|
HalconCpp::HTuple Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0;
|
std::string strMode = "all";
|
if (0 == modeTrans) {
|
strMode = "all";
|
}
|
else if (1 == modeTrans) {
|
strMode = "positive";
|
}
|
else if (2 == modeTrans) {
|
strMode = "negative";
|
}
|
HalconCpp::MeasurePos(hObject, MsrHandle_Measure_01_0, 1, threshold, strMode.c_str(), "all", &Row_Measure_01_0, &Column_Measure_01_0, &Amplitude_Measure_01_0, &Distance_Measure_01_0);
|
HalconCpp::CloseMeasure(MsrHandle_Measure_01_0);
|
|
bool result = false;
|
HalconCpp::HTuple hvLenth;
|
HalconCpp::TupleLength(Row_Measure_01_0, &hvLenth);
|
int iCount = hvLenth[0].I();
|
if (iCount > 0) {
|
ptStart.x = Column_Measure_01_0[0].D();
|
ptStart.y = Row_Measure_01_0[0].D();
|
|
if (1 == iCount) {
|
ptEnd = ptStart;
|
return true;
|
}
|
|
ptEnd.x = Column_Measure_01_0[1].D();
|
ptEnd.y = Row_Measure_01_0[1].D();
|
result = true;
|
}
|
|
return result;
|
}
|
catch (...)
|
{
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::GetMeasureRCutPos(HalconCpp::HObject &hObject, Line2D line, int threshold, int modeTrans, int chamberSize, double proDist, Point2D &ptStart, Point2D &ptEnd) {
|
/* code */
|
if (!ClsVision::ObjectExist(hObject)) return false;
|
|
try {
|
HalconCpp::HTuple LineRowStart_Measure_01_0, LineColumnStart_Measure_01_0, LineRowEnd_Measure_01_0, LineColumnEnd_Measure_01_0;
|
LineRowStart_Measure_01_0 = line.pt0.y;
|
LineColumnStart_Measure_01_0 = line.pt0.x;
|
LineRowEnd_Measure_01_0 = line.pt1.y;
|
LineColumnEnd_Measure_01_0 = line.pt1.x;
|
|
HalconCpp::HTuple TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Dr, TmpCtrl_Dc, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2;
|
TmpCtrl_Row = 0.5*(LineRowStart_Measure_01_0 + LineRowEnd_Measure_01_0);
|
TmpCtrl_Column = 0.5*(LineColumnStart_Measure_01_0 + LineColumnEnd_Measure_01_0);
|
TmpCtrl_Dr = LineRowStart_Measure_01_0 - LineRowEnd_Measure_01_0;
|
TmpCtrl_Dc = LineColumnEnd_Measure_01_0 - LineColumnStart_Measure_01_0;
|
|
TmpCtrl_Phi = TmpCtrl_Dr.TupleAtan2(TmpCtrl_Dc);
|
|
HalconCpp::TupleSqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc, &TmpCtrl_Len1);
|
TmpCtrl_Len1 = 0.5 * (((TmpCtrl_Dr * TmpCtrl_Dr) + (TmpCtrl_Dc * TmpCtrl_Dc)).TupleSqrt());
|
TmpCtrl_Len2 = 5.0f;
|
|
HalconCpp::HTuple hvWidth, hvHeight, MsrHandle_Measure_01_0;
|
HalconCpp::GetImageSize(hObject, &hvWidth, &hvHeight);
|
|
HalconCpp::GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, hvWidth, hvHeight, "nearest_neighbor", &MsrHandle_Measure_01_0);
|
|
HalconCpp::HTuple Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0;
|
std::string strMode = "all";
|
if (0 == modeTrans) {
|
strMode = "all";
|
}
|
else if (1 == modeTrans) {
|
strMode = "positive";
|
}
|
else if (2 == modeTrans) {
|
strMode = "negative";
|
}
|
HalconCpp::MeasurePos(hObject, MsrHandle_Measure_01_0, 1, threshold, strMode.c_str(), "all",
|
&Row_Measure_01_0, &Column_Measure_01_0, &Amplitude_Measure_01_0, &Distance_Measure_01_0);
|
HalconCpp::CloseMeasure(MsrHandle_Measure_01_0);
|
|
double fzMinDist = 0.4 * chamberSize;
|
if (proDist > 1.0f) {
|
double temp = 0.8 * proDist;
|
if (temp > fzMinDist) {
|
fzMinDist = temp;
|
}
|
}
|
|
HalconCpp::HTuple hvLenth = Row_Measure_01_0.TupleLength();
|
int sz = hvLenth[0].I();
|
if (sz < 1) return false;
|
if (Amplitude_Measure_01_0[0].D() > 0) return false;
|
ptStart.x = Column_Measure_01_0[0].D();
|
ptStart.y = Row_Measure_01_0[0].D();
|
|
bool isFind = false;
|
double dist = 0.0f;
|
if (sz > 1) {
|
for (int i = 1; i < sz; i++) {
|
if (Amplitude_Measure_01_0[i].D() < 0.0f) {
|
dist += Distance_Measure_01_0[i - 1].D();
|
continue;
|
}
|
ptEnd.x = Column_Measure_01_0[i].D();
|
ptEnd.y = Row_Measure_01_0[i].D();
|
dist += Distance_Measure_01_0[i - 1].D();
|
if (dist > fzMinDist && dist < chamberSize) {
|
isFind = true;
|
break;
|
}
|
|
if (dist > chamberSize) {
|
break;
|
}
|
}
|
}
|
|
if ((!isFind) && proDist > 1.0f) {
|
dist = ClsVision::DistancePP(ptStart, line.pt1);
|
Point2D pose;
|
std::vector<Point2D> vPtStd, vPtReal;
|
pose.x = 0.0f;
|
pose.y = 0.0f;
|
vPtStd.push_back(pose);
|
pose.x = dist;
|
pose.y = 0.0f;
|
vPtStd.push_back(pose);
|
vPtReal.push_back(ptStart);
|
vPtReal.push_back(line.pt1);
|
|
ParaAffine2D affine = ClsVision::VectorToRigid(vPtStd, vPtReal);
|
pose.x = proDist;
|
pose.y = 0.0f;
|
ptEnd = ClsVision::CoordinateTransform(pose, affine);
|
isFind = true;
|
}
|
|
return isFind;
|
}
|
catch (...)
|
{
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::GetMeasurePosKeyLimit(HalconCpp::HObject &hObject, Line2D line, int nTypePoint, int threshold, int modeTrans, int step, int distLimit, Point2D &ptStart, Point2D &ptEnd) {
|
/* code */
|
if (!ClsVision::ObjectExist(hObject)) return false;
|
|
try {
|
HalconCpp::HTuple LineRowStart_Measure_01_0, LineColumnStart_Measure_01_0, LineRowEnd_Measure_01_0, LineColumnEnd_Measure_01_0;
|
LineRowStart_Measure_01_0 = line.pt0.y;
|
LineColumnStart_Measure_01_0 = line.pt0.x;
|
LineRowEnd_Measure_01_0 = line.pt1.y;
|
LineColumnEnd_Measure_01_0 = line.pt1.x;
|
|
HalconCpp::HTuple TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Dr, TmpCtrl_Dc, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2;
|
TmpCtrl_Row = 0.5*(LineRowStart_Measure_01_0 + LineRowEnd_Measure_01_0);
|
TmpCtrl_Column = 0.5*(LineColumnStart_Measure_01_0 + LineColumnEnd_Measure_01_0);
|
TmpCtrl_Dr = LineRowStart_Measure_01_0 - LineRowEnd_Measure_01_0;
|
TmpCtrl_Dc = LineColumnEnd_Measure_01_0 - LineColumnStart_Measure_01_0;
|
HalconCpp::TupleAtan2(TmpCtrl_Dr, TmpCtrl_Dc, &TmpCtrl_Phi);
|
HalconCpp::TupleSqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc, &TmpCtrl_Len1);
|
TmpCtrl_Len1 = 0.5 * TmpCtrl_Len1;
|
TmpCtrl_Len2 = 0.5 * step;
|
|
HalconCpp::HTuple hvWidth, hvHeight, MsrHandle_Measure_01_0;
|
HalconCpp::GetImageSize(hObject, &hvWidth, &hvHeight);
|
|
HalconCpp::GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, hvWidth, hvHeight, "nearest_neighbor", &MsrHandle_Measure_01_0);
|
|
HalconCpp::HTuple Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0;
|
std::string strMode = "all";
|
if (0 == modeTrans) {
|
strMode = "all";
|
}
|
else if (1 == modeTrans) {
|
strMode = "positive";
|
}
|
else if (2 == modeTrans) {
|
strMode = "negative";
|
}
|
HalconCpp::MeasurePos(hObject, MsrHandle_Measure_01_0, 1, threshold, strMode.c_str(), "all", &Row_Measure_01_0, &Column_Measure_01_0, &Amplitude_Measure_01_0, &Distance_Measure_01_0);
|
HalconCpp::CloseMeasure(MsrHandle_Measure_01_0);
|
|
HalconCpp::HTuple hvLenth;
|
HalconCpp::TupleLength(Row_Measure_01_0, &hvLenth);
|
int nCount = hvLenth[0].I();
|
if (nCount < 1) return false;
|
|
ptStart.x = Column_Measure_01_0[0].D();
|
ptStart.y = Row_Measure_01_0[0].D();
|
ptEnd.x = Column_Measure_01_0[nCount - 1].D();
|
ptEnd.y = Row_Measure_01_0[nCount - 1].D();
|
|
Point2D point;
|
if (0 == nTypePoint) {
|
point = ptStart;
|
}
|
else {
|
point = ptEnd;
|
}
|
|
int idx = -1;
|
double maxDist = MININT;
|
Point2D pt;
|
for (int i = 0; i < nCount; i++) {
|
pt.x = Column_Measure_01_0[i].D();
|
pt.y = Row_Measure_01_0[i].D();
|
double dx = pt.x - point.x;
|
double dy = pt.y - point.y;
|
double dist = sqrt(dx * dx + dy * dy);
|
if (dist < distLimit && dist > maxDist) {
|
maxDist = dist;
|
idx = i;
|
}
|
}
|
if (idx < 0) return false;
|
|
point.x = Column_Measure_01_0[idx].D();
|
point.y = Row_Measure_01_0[idx].D();
|
if (0 == nTypePoint) {
|
ptStart = point;
|
}
|
else {
|
ptEnd = point;
|
}
|
|
return true;
|
}
|
catch (...)
|
{
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::getDistResult(DimensionDir eDir, CDistDotProcess *dot, double &result, int &xPosPxl, int &yPosPxl) {
|
/* code */
|
xPosPxl = 0;
|
yPosPxl = 0;
|
result = 0.0f;
|
CCornerDistProcess *dotCorner = CVisionRecipe::getInstance()->getCornerDistProcess(eDir);
|
CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir);
|
if (NULL == pSideData) return true;
|
if (NULL == dotCorner) return true;
|
if (NULL == dot) return true;
|
|
|
Point2D locResult = dot->m_ptMatchResult;
|
Point2D measureResult = dot->m_ptMeasureResult;
|
Point2D nearResult = dot->m_ptNearResult;
|
|
xPosPxl = dot->m_locPointResult.x;
|
yPosPxl = dot->m_locPointResult.y;
|
|
int distMode = dot->m_distMode;
|
if (0 == distMode) {
|
double dx = measureResult.x - nearResult.x;
|
result = fabs(pSideData->m_dPixelSizeX * dx);
|
}
|
else if (1 == distMode) {
|
Point2I topMark = pSideData->getPose(0);
|
Point2I botMark = pSideData->getPose(1);
|
|
Line2D line1, line2;
|
line1.pt0.x = topMark.x;
|
line1.pt0.y = topMark.y;
|
line1.pt1.x = botMark.x;
|
line1.pt1.y = botMark.y;
|
|
line2.pt0.x = measureResult.x;
|
line2.pt0.y = measureResult.y;
|
line2.pt1.x = measureResult.x + 1000.0f;
|
line2.pt1.y = measureResult.y;
|
|
Point2D pose = ClsVision::LineIntersection(line1, line2);
|
double dx = measureResult.x - pose.x;
|
result = fabs(pSideData->m_dPixelSizeX * dx);
|
}
|
else if (2 == distMode) {
|
Point2I pose = pSideData->getPose(0);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
result = sqrt(dx * dx + dy * dy);
|
}
|
else if (3 == distMode) {
|
Point2I pose = pSideData->getPose(1);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
result = sqrt(dx * dx + dy * dy);
|
}
|
else if (4 == distMode) {
|
Point2I pose = pSideData->getPose(2);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
result = sqrt(dx * dx + dy * dy);
|
}
|
else if (5 == distMode) {
|
Point2I pose = pSideData->getPose(3);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
result = sqrt(dx * dx + dy * dy);
|
}
|
else if (6 == distMode) {
|
Point2D pose = nearResult;
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
result = sqrt(dx * dx + dy * dy);
|
}
|
else if (7 == distMode) {
|
Point2D pose = locResult;
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
result = sqrt(dx * dx + dy * dy);
|
}
|
|
if (dot->m_maxValue < 0.1) return true;
|
if (dot->m_minValue > dot->m_maxValue) return true;
|
|
if (result < dot->m_minValue || result > dot->m_maxValue) return false;
|
|
return true;
|
}
|
|
int CVisionBufferPro::getDistResult(DimensionDir eDir, CDistDotProcess *dot, double &distResult) {
|
/* code */
|
//1. ³õʼ»¯½á¹û
|
distResult = 0.0f;
|
int nRet = 0;
|
CCornerDistProcess *dotCorner = CVisionRecipe::getInstance()->getCornerDistProcess(eDir);
|
CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir);
|
if (NULL == pSideData) return nRet;
|
if (NULL == dotCorner) return nRet;
|
if (NULL == dot) return nRet;
|
|
|
Point2D locResult = dot->m_ptMatchResult;
|
Point2D measureResult = dot->m_ptMeasureResult;
|
Point2D nearResult = dot->m_ptNearResult;
|
|
int distMode = dot->m_distMode;
|
if (0 == distMode) {
|
double dx = measureResult.x - nearResult.x;
|
distResult = fabs(pSideData->m_dPixelSizeX * dx);
|
}
|
else if (1 == distMode) {
|
Point2I topMark = pSideData->getPose(0);
|
Point2I botMark = pSideData->getPose(1);
|
|
Line2D line1, line2;
|
line1.pt0.x = topMark.x;
|
line1.pt0.y = topMark.y;
|
line1.pt1.x = botMark.x;
|
line1.pt1.y = botMark.y;
|
|
line2.pt0.x = measureResult.x;
|
line2.pt0.y = measureResult.y;
|
line2.pt1.x = measureResult.x + 1000.0f;
|
line2.pt1.y = measureResult.y;
|
|
Point2D pose = ClsVision::LineIntersection(line1, line2);
|
double dx = measureResult.x - pose.x;
|
distResult = fabs(pSideData->m_dPixelSizeX * dx);
|
}
|
else if (2 == distMode) {
|
Point2I pose = pSideData->getPose(0);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
distResult = sqrt(dx * dx + dy * dy);
|
}
|
else if (3 == distMode) {
|
Point2I pose = pSideData->getPose(1);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
distResult = sqrt(dx * dx + dy * dy);
|
}
|
else if (4 == distMode) {
|
Point2I pose = pSideData->getPose(2);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
distResult = sqrt(dx * dx + dy * dy);
|
}
|
else if (5 == distMode) {
|
Point2I pose = pSideData->getPose(3);
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
distResult = sqrt(dx * dx + dy * dy);
|
}
|
else if (6 == distMode){
|
Point2D pose = nearResult;
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
distResult = sqrt(dx * dx + dy * dy);
|
}
|
else if (7 == distMode) {
|
Point2D pose = locResult;
|
double dx = pSideData->m_dPixelSizeX *(measureResult.x - pose.x);
|
double dy = pSideData->m_dPixelSizeY *(measureResult.y - pose.y);
|
distResult = sqrt(dx * dx + dy * dy);
|
}
|
|
if (dot->m_maxValue < 0.1) return 0;
|
if (dot->m_minValue > dot->m_maxValue) return 0;
|
|
if (distResult < dot->m_minValue || distResult > dot->m_maxValue) return 1;
|
|
return 0;
|
}
|
|
bool CVisionBufferPro::TransformToReal(DimensionDir eDir, Point2I point, int nType, Point2I &result) {
|
/* code */
|
result.x = 0;
|
result.y = 0;
|
CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir);
|
if (NULL == pSideData) return false;
|
if (0 == nType) {
|
if (!pSideData->m_bTopMark_Find) return false;
|
if (!pSideData->m_bBotMark_Find) return false;
|
}
|
else {
|
if (!pSideData->m_bTopPoint_Find) return false;
|
if (!pSideData->m_bBotPoint_Find) return false;
|
}
|
|
double xMmvsp = pSideData->m_dPixelSizeX;
|
double yMmvsp = pSideData->m_dPixelSizeY;
|
|
Point2I ptTop, ptBot;
|
if (0 == nType) { //Origine Mark Line
|
ptTop = pSideData->m_mTopMark;
|
ptBot = pSideData->m_mBotMark;
|
}
|
else if (1 == nType) { //Top MarkPoint
|
ptTop = pSideData->m_mTopPoint;
|
ptBot = pSideData->m_mBotPoint;
|
}
|
else if (2 == nType) { //Bot MarkLine
|
ptTop = pSideData->m_mTopPoint;
|
ptBot = pSideData->m_mBotPoint;
|
}
|
double dx = xMmvsp * (ptBot.x - ptTop.x);
|
double dy = yMmvsp * (ptBot.y - ptTop.y);
|
double dist = sqrt(dx * dx + dy * dy);
|
|
Point2D posTopReal, posBotReal, poseReal;
|
posTopReal.x = 0.0f;
|
posTopReal.y = 0.0f;
|
posBotReal.x = 0.0f;
|
posBotReal.y = dist;
|
if (0 == nType) {
|
poseReal = posTopReal;
|
poseReal.x += point.x;
|
poseReal.y += point.y;
|
}
|
else if (1 == nType) {
|
poseReal = posTopReal;
|
poseReal.x += point.x;
|
poseReal.y += point.y;
|
}
|
else if (2 == nType) {
|
poseReal = posBotReal;
|
poseReal.x += point.x;
|
poseReal.y += point.y;
|
}
|
|
std::vector<Point2D> vPtOrigine, vPtReal;
|
vPtOrigine.push_back(posTopReal);
|
vPtOrigine.push_back(posBotReal);
|
Point2D pose;
|
pose.x = 0.0f;
|
pose.y = 0.0f;
|
vPtReal.push_back(pose);
|
pose.x = xMmvsp * (ptBot.x - ptTop.x);
|
pose.y = yMmvsp * (ptBot.y - ptTop.y);
|
vPtReal.push_back(pose);
|
ParaAffine2D affine = ClsVision::VectorToSimilarity(vPtOrigine, vPtReal);
|
Point2D ptResult = ClsVision::CoordinateTransform(poseReal, affine);
|
result.x = (int)(ptTop.x + ptResult.x / xMmvsp);
|
result.y = (int)(ptTop.y + ptResult.y / yMmvsp);
|
|
|
#if 0 //Modify, jiang, 09-24, 2024
|
Point2D ptImage;
|
ptImage.x = point.x;
|
ptImage.y = point.y;
|
if (1 == nType) {
|
std::vector<Point2D> vPtStd, vPtPoint;
|
Point2D pt;
|
pt.x = pSideData->m_stdTopPoint[0].x;
|
pt.y = pSideData->m_stdTopPoint[0].y;
|
vPtStd.push_back(pt);
|
pt.x = pSideData->m_stdBotPoint[0].x;
|
pt.y = pSideData->m_stdBotPoint[0].y;
|
vPtStd.push_back(pt);
|
|
pt.x = pSideData->m_mTopPoint.x;
|
pt.y = pSideData->m_mTopPoint.y;
|
vPtPoint.push_back(pt);
|
pt.x = pSideData->m_mBotPoint.x;
|
pt.y = pSideData->m_mBotPoint.y;
|
vPtPoint.push_back(pt);
|
|
ParaAffine2D affine = ClsVision::VectorToSimilarity(vPtStd, vPtPoint);
|
pt = ClsVision::CoordinateTransform(ptImage, affine);
|
result.x = (int)(pt.x);
|
result.y = (int)(pt.y);
|
}
|
else if (2 == nType) {
|
std::vector<Point2D> vPtStd, vPtPoint;
|
Point2D pt;
|
pt.x = pSideData->m_stdTopPoint[0].x;
|
pt.y = pSideData->m_stdTopPoint[0].y;
|
vPtStd.push_back(pt);
|
pt.x = pSideData->m_stdTopPoint[1].x;
|
pt.y = pSideData->m_stdTopPoint[1].y;
|
vPtStd.push_back(pt);
|
|
pt.x = pSideData->m_mTopPoint.x;
|
pt.y = pSideData->m_mTopPoint.y;
|
vPtPoint.push_back(pt);
|
pt.x = pSideData->m_mTopPoint2.x;
|
pt.y = pSideData->m_mTopPoint2.y;
|
vPtPoint.push_back(pt);
|
|
ParaAffine2D affine = ClsVision::VectorToSimilarity(vPtStd, vPtPoint);
|
pt = ClsVision::CoordinateTransform(ptImage, affine);
|
result.x = (int)(pt.x);
|
result.y = (int)(pt.y);
|
}
|
else if (3 == nType) {
|
std::vector<Point2D> vPtStd, vPtPoint;
|
Point2D pt;
|
pt.x = pSideData->m_stdBotPoint[0].x;
|
pt.y = pSideData->m_stdBotPoint[0].y;
|
vPtStd.push_back(pt);
|
pt.x = pSideData->m_stdBotPoint[1].x;
|
pt.y = pSideData->m_stdBotPoint[1].y;
|
vPtStd.push_back(pt);
|
|
pt.x = pSideData->m_mBotPoint.x;
|
pt.y = pSideData->m_mBotPoint.y;
|
vPtPoint.push_back(pt);
|
pt.x = pSideData->m_mBotPoint2.x;
|
pt.y = pSideData->m_mBotPoint2.y;
|
vPtPoint.push_back(pt);
|
|
ParaAffine2D affine = ClsVision::VectorToSimilarity(vPtStd, vPtPoint);
|
pt = ClsVision::CoordinateTransform(ptImage, affine);
|
result.x = (int)(pt.x);
|
result.y = (int)(pt.y);
|
}
|
#endif
|
|
return true;
|
}
|
|
void CVisionBufferPro::getRect2Point(Point2D rect2CenterPoint, double rect2Phi, double rect2Length1, double rect2Length2, Point2D *rect2Points) {
|
/* code */
|
double cosValue = cos(rect2Phi);
|
double sinValue = sin(rect2Phi);
|
|
double Row = rect2CenterPoint.y;
|
double Column = rect2CenterPoint.x;
|
double Length1 = rect2Length1;
|
double Length2 = rect2Length2;
|
|
//LeftTop Point
|
double TopLeft_X = -Length1*cosValue - Length2*sinValue;
|
double TopLeft_Y = -Length1*sinValue + Length2*cosValue;
|
rect2Points[0].x = Row - TopLeft_Y;
|
rect2Points[0].y = Column + TopLeft_X;
|
|
//RightTop Point
|
double TopRight_X = Length1*cosValue - Length2*sinValue;
|
double TopRight_Y = Length1*sinValue + Length2*cosValue;
|
rect2Points[1].x = Row - TopRight_Y;
|
rect2Points[1].y = Column + TopRight_X;
|
|
//RightDown Point
|
double LowerRight_X = Length1*cosValue + Length2*sinValue;
|
double LowerRight_Y = Length1*sinValue - Length2*cosValue;
|
rect2Points[2].x = Row - LowerRight_Y;
|
rect2Points[2].y = Column + LowerRight_X;
|
|
//LeftDown Point
|
double LowerLeft_X = -Length1*cosValue + Length2*sinValue;
|
double LowerLeft_Y = -Length1*sinValue - Length2*cosValue;
|
rect2Points[3].x = Row - LowerLeft_Y;
|
rect2Points[3].y = Column + LowerLeft_X;
|
}
|
|
Line2D CVisionBufferPro::getRect2Line(Point2D rect2CenterPoint, double rect2Phi, int rect2Length2) {
|
/* code */
|
Point2D rect2Points[4];
|
double rect2Length1 = 100.0f;
|
double length2 = rect2Length2;
|
getRect2Point(rect2CenterPoint, rect2Phi, rect2Length1, length2, rect2Points);
|
Line2D line;
|
line.pt0.x = 0.5 *(rect2Points[0].x + rect2Points[1].x);
|
line.pt0.y = 0.5 *(rect2Points[0].y + rect2Points[1].y);
|
line.pt1.x = 0.5 *(rect2Points[2].x + rect2Points[3].x);
|
line.pt1.y = 0.5 *(rect2Points[2].y + rect2Points[3].y);
|
return line;
|
}
|
|
/////////////////////////////////////////////////////////////////////////////////
|
void SetMetrologyTransition(int hHandle, int iIndex, int getLineMode){
|
/* code */
|
if (hHandle < 0 || iIndex < 0) return;
|
|
if (0 == getLineMode){
|
HalconCpp::SetMetrologyObjectParam(hHandle, iIndex, "measure_transition", "all");
|
}
|
else if (1 == getLineMode){
|
HalconCpp::SetMetrologyObjectParam(hHandle, iIndex, "measure_transition", "positive");
|
}
|
else if (2 == getLineMode){
|
HalconCpp::SetMetrologyObjectParam(hHandle, iIndex, "measure_transition", "negative");
|
}
|
}
|
|
void SetMetrologySelect(int hHandle, int iIndex, int getLineDirection){
|
/* code */
|
if (hHandle < 0 || iIndex < 0) return;
|
|
if (0 == getLineDirection){
|
HalconCpp::SetMetrologyObjectParam(hHandle, iIndex, "measure_select", "all");
|
}
|
else if (1 == getLineDirection){
|
HalconCpp::SetMetrologyObjectParam(hHandle, iIndex, "measure_select", "first");
|
}
|
else if (2 == getLineDirection){
|
HalconCpp::SetMetrologyObjectParam(hHandle, iIndex, "measure_select", "last");
|
}
|
}
|
|
bool CVisionBufferPro::LineMeasureByRuler(HalconCpp::HObject &hObject, Line2D line,
|
LineMetrologyPara shapeMetrologyPara, Line2D &lineResult)
|
{
|
/* code */
|
lineResult = line;
|
if (!ClsVision::ObjectExist(hObject)){
|
return false;
|
}
|
|
try
|
{
|
//1. ͼÏñ³ß´ç
|
int width = 0;
|
int height = 0;
|
ClsVision::GetImageSize(hObject, width, height);
|
|
//2. ´´½¨²âÁ¿¾ä±ú
|
HalconCpp::HTuple hvMetrologyHandle;
|
HalconCpp::CreateMetrologyModel(&hvMetrologyHandle);
|
HalconCpp::SetMetrologyModelImageSize(hvMetrologyHandle, width, height);
|
|
//3. ´´½¨²âÖ±ÏߵIJÎÊý
|
HalconCpp::HTuple hvLineParams;
|
if (true){
|
Line2D curLine = line;
|
int lineDirection = shapeMetrologyPara.direction;
|
if (0 == lineDirection){
|
if (line.pt0.x < line.pt1.x) {
|
curLine = line;
|
}
|
else {
|
curLine.pt1 = line.pt0;
|
curLine.pt0 = line.pt1;
|
}
|
}
|
else if (1 == lineDirection){
|
if (line.pt0.y < line.pt1.y){
|
curLine = line;
|
}
|
else{
|
curLine.pt0 = line.pt1;
|
curLine.pt1 = line.pt0;
|
}
|
}
|
//Ö±Ïß·½Ïò
|
hvLineParams.Clear();
|
hvLineParams.Append(curLine.pt0.y);
|
hvLineParams.Append(curLine.pt0.x);
|
hvLineParams.Append(curLine.pt1.y);
|
hvLineParams.Append(curLine.pt1.x);
|
}
|
|
HalconCpp::HTuple hvIndex, hvLen;
|
double stepLength = shapeMetrologyPara.stepLength;
|
double stepDist = shapeMetrologyPara.stepDist;
|
int threshold = shapeMetrologyPara.threshold;
|
HalconCpp::AddMetrologyObjectGeneric(hvMetrologyHandle, "line", hvLineParams, stepLength, stepDist, 1, threshold,
|
HalconCpp::HTuple(), HalconCpp::HTuple(), &hvIndex);
|
int iHandle = hvMetrologyHandle[0].I();
|
|
HalconCpp::TupleLength(hvIndex, &hvLen);
|
if (hvLen[0].I() < 1){
|
HalconCpp::ClearMetrologyModel(iHandle);
|
return false;
|
}
|
|
int iIndex = hvIndex[0].I();
|
if (iIndex < 0){
|
HalconCpp::ClearMetrologyModel(iHandle);
|
return false;
|
}
|
|
//Ö±Ïߵı߽çµãÀàÐÍ
|
int getLineMode = shapeMetrologyPara.getMode;
|
SetMetrologyTransition(iHandle, iIndex, getLineMode);
|
|
HalconCpp::SetMetrologyObjectParam(iHandle, iIndex, "num_measures", shapeMetrologyPara.numSample);
|
HalconCpp::SetMetrologyObjectParam(iHandle, iIndex, "min_score", shapeMetrologyPara.minScore);
|
|
//ÉèÖÃÖ±ÏßÌáȡģʽ
|
SetMetrologySelect(iHandle, iIndex, shapeMetrologyPara.getDirection);
|
HalconCpp::ApplyMetrologyModel(hObject, iHandle);
|
HalconCpp::HTuple hvParameter, hvLength;
|
HalconCpp::GetMetrologyObjectResult(iHandle, iIndex, "all", "result_type", "all_param", &hvParameter);
|
HalconCpp::TupleLength(hvParameter, &hvLength);
|
if (hvLength[0].I() < 1){
|
HalconCpp::ClearMetrologyModel(iHandle);
|
return false;
|
}
|
|
lineResult.pt0.y = hvParameter[0].D();
|
lineResult.pt0.x = hvParameter[1].D();
|
lineResult.pt1.y = hvParameter[2].D();
|
lineResult.pt1.x = hvParameter[3].D();
|
HalconCpp::ClearMetrologyModel(iHandle);
|
|
return true;
|
}
|
catch (...)
|
{
|
return false;
|
}
|
}
|
|
bool CVisionBufferPro::findCutLine(DimensionDir eDir, Point2I keyPoint, int keyRange, int keyThres, Point2I &result) {
|
/* code */
|
result.x = 0;
|
result.y = 0;
|
CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir);
|
if (NULL == pSideData) return false;
|
if (!pSideData->m_bTopMark_Find) return false;
|
if (!pSideData->m_bBotMark_Find) return false;
|
|
int x1 = keyPoint.x - keyRange;
|
int y1 = keyPoint.y - 31;
|
int x2 = keyPoint.x + keyRange;
|
int y2 = keyPoint.y + 31;
|
Point2I leftTop;
|
leftTop.x = x1;
|
leftTop.y = y1;
|
HalconCpp::HObject hImage;
|
if (!getImageROI(eDir, hImage, x1, y1, x2, y2)) return false;
|
|
HalconCpp::HObject hMeanImage;
|
HalconCpp::MeanImage(hImage, &hMeanImage, 1, 20);
|
int width = 0;
|
int height = 0;
|
ClsVision::GetImageSize(hImage, width, height);
|
if (width < 10 || height < 25) return false;
|
|
HalconCpp::HObject hLineRegionRoi;
|
HalconCpp::GenRegionLine(&hLineRegionRoi, 0.5 * height - 1, 0, 0.5 * height - 1, width - 1);
|
HalconCpp::HTuple hvY, hvX, hvGray;
|
HalconCpp::GetRegionPoints(hLineRegionRoi, &hvY, &hvX);
|
HalconCpp::GetGrayval(hMeanImage, hvY, hvX, &hvGray);
|
HalconCpp::HTuple hvLength = hvX.TupleLength();
|
int num = hvLength[0].I();
|
if (num < 10) return false;
|
|
HalconCpp::HTuple hvCyValue;
|
for (int i = 0; i < num; i++) {
|
if (hvGray[i].I() < keyThres) {
|
hvCyValue[i] = 255;
|
}
|
else {
|
hvCyValue[i] = 0;
|
}
|
}
|
|
bool isStart = false;
|
bool isFind = false;
|
int nEdgeLine = 0;
|
for (int i = 0; i < num - 1; i++) {
|
if (isFind) break;
|
|
if (hvCyValue[i].I() > 120 || hvCyValue[i + 1].I() > 120) {
|
isStart = true;
|
nEdgeLine = i;
|
}
|
|
if (isStart && hvCyValue[i].I() < 120 && hvCyValue[i + 1].I() < 120) {
|
isFind = true;
|
}
|
}
|
if (!isFind) return false;
|
|
result.x = leftTop.x + nEdgeLine;
|
result.y = leftTop.y + (int)(height / 2);
|
return true;
|
}
|
|
int CVisionBufferPro::findNorchDefect(DimensionDir eDir, std::vector<Point2D> vPoints, CRect roiRect, int nThres, int nOffset, int nBinThres,
|
int szType, int xFzSize, int yFzSize, CRect* aryResult) {
|
/* code */
|
if (roiRect.Width() < 10 || roiRect.Height() < 10) return 0;
|
if (nThres < 5 || nBinThres < 5) return 0;
|
if (0 == szType) return 0;
|
|
int sz = (int)(vPoints.size());
|
if (sz < 3) return 0;
|
|
std::vector<Point2D> vEdgePoints;
|
for (auto item : vPoints) {
|
Point2D point = item;
|
vEdgePoints.push_back(point);
|
}
|
//1. ½øÐÐ×ÔÉ϶øÏµÄÅÅÐò
|
for (int i = 0; i < sz - 1; i++) {
|
for (int j = i + 1; j < sz; j++) {
|
if (vEdgePoints[i].y > vEdgePoints[j].y) {
|
Point2D temp = vEdgePoints[i];
|
vEdgePoints[i] = vEdgePoints[j];
|
vEdgePoints[j] = temp;
|
}
|
}
|
}
|
|
//2. ½ØÈ¡ROIͼÏñ
|
int x1 = roiRect.left;
|
int y1 = roiRect.top;
|
int x2 = roiRect.right;
|
int y2 = roiRect.bottom;
|
HalconCpp::HObject hImage;
|
if (!getImageROI(eDir, hImage, x1, y1, x2, y2)) return 0;
|
|
int width = 0;
|
int height = 0;
|
ClsVision::GetImageSize(hImage, width, height);
|
if (width < 10 || height < 10) return 0;
|
|
|
//3. µãÎ»Æ«ÒÆ
|
for (int i = 0; i < sz; i++) {
|
vEdgePoints[i].x -= roiRect.left;
|
vEdgePoints[i].y -= roiRect.top;
|
}
|
|
int edgeThres = nThres;
|
if (nThres > 10) {
|
edgeThres = 10;
|
}
|
|
std::vector<vec> vEdgeCtrlPoint;
|
for (int i = 0; i < sz; i++) {
|
if (0 != i) {
|
double dy = vEdgePoints[i].y - vEdgePoints[i - 1].y;
|
double dx = vEdgePoints[i].x - vEdgePoints[i - 1].x;
|
if (dy > 450) {
|
for (int j = 0; j < 3; j++) {
|
Line2D line;
|
line.pt0.x = vEdgePoints[i - 1].x + (j + 1) * dx / 4 - 75;
|
line.pt0.y = vEdgePoints[i - 1].y + (j + 1) * dy / 4;
|
line.pt1.x = vEdgePoints[i - 1].x + (j + 1) * dx / 4 + 75;
|
line.pt1.y = vEdgePoints[i - 1].y + (j + 1) * dy / 4;
|
if (line.pt0.x < 0) line.pt0.x = 1;
|
if (line.pt0.x > width - 1) line.pt0.x = width - 2;
|
if (line.pt0.y < 0) line.pt0.y = 1;
|
if (line.pt0.y > height - 1) line.pt0.y = height - 2;
|
if (line.pt1.x < 0) line.pt1.x = 1;
|
if (line.pt1.x > width - 1) line.pt1.x = width - 2;
|
if (line.pt1.y < 0) line.pt1.y = 1;
|
if (line.pt1.y > height - 1) line.pt1.y = height - 2;
|
if (line.pt1.x > line.pt0.x) {
|
Point2D ptStart, ptEnd;
|
if (CVisionBufferPro::GetMeasureNearPos(hImage, line, edgeThres, 0, 10, ptStart, ptEnd)) {
|
float xPos = (float)(ptEnd.x);
|
float yPos = (float)(ptEnd.y);
|
vec p1(xPos, yPos, 0);
|
vEdgeCtrlPoint.push_back(p1);
|
}
|
}
|
}
|
}
|
else if (dy > 300) {
|
for (int j = 0; j < 2; j++) {
|
Line2D line;
|
line.pt0.x = vEdgePoints[i - 1].x + (j + 1) * dx / 3 - 75;
|
line.pt0.y = vEdgePoints[i - 1].y + (j + 1) * dy / 3;
|
line.pt1.x = vEdgePoints[i - 1].x + (j + 1) * dx / 3 + 75;
|
line.pt1.y = vEdgePoints[i - 1].y + (j + 1) * dy / 3;
|
if (line.pt0.x < 0) line.pt0.x = 1;
|
if (line.pt0.x > width - 1) line.pt0.x = width - 2;
|
if (line.pt0.y < 0) line.pt0.y = 1;
|
if (line.pt0.y > height - 1) line.pt0.y = height - 2;
|
if (line.pt1.x < 0) line.pt1.x = 1;
|
if (line.pt1.x > width - 1) line.pt1.x = width - 2;
|
if (line.pt1.y < 0) line.pt1.y = 1;
|
if (line.pt1.y > height - 1) line.pt1.y = height - 2;
|
if (line.pt1.x > line.pt0.x) {
|
Point2D ptStart, ptEnd;
|
if (CVisionBufferPro::GetMeasureNearPos(hImage, line, edgeThres, 0, 10, ptStart, ptEnd)) {
|
float xPos = (float)(ptEnd.x);
|
float yPos = (float)(ptEnd.y);
|
vec p1(xPos, yPos, 0);
|
vEdgeCtrlPoint.push_back(p1);
|
}
|
}
|
}
|
}
|
else {
|
Line2D line;
|
line.pt0.x = 0.5 * (vEdgePoints[i].x + vEdgePoints[i - 1].x) - 75;
|
line.pt0.y = 0.5 * (vEdgePoints[i].y + vEdgePoints[i - 1].y);
|
line.pt1.x = 0.5 * (vEdgePoints[i].x + vEdgePoints[i - 1].x) + 75;
|
line.pt1.y = 0.5 * (vEdgePoints[i].y + vEdgePoints[i - 1].y);
|
if (line.pt0.x < 0) line.pt0.x = 1;
|
if (line.pt0.x > width - 1) line.pt0.x = width - 2;
|
if (line.pt0.y < 0) line.pt0.y = 1;
|
if (line.pt0.y > height - 1) line.pt0.y = height - 2;
|
if (line.pt1.x < 0) line.pt1.x = 1;
|
if (line.pt1.x > width - 1) line.pt1.x = width - 2;
|
if (line.pt1.y < 0) line.pt1.y = 1;
|
if (line.pt1.y > height - 1) line.pt1.y = height - 2;
|
if (line.pt1.x > line.pt0.x) {
|
Point2D ptStart, ptEnd;
|
if (CVisionBufferPro::GetMeasureNearPos(hImage, line, edgeThres, 0, 10, ptStart, ptEnd)) {
|
float xPos = (float)(ptEnd.x);
|
float yPos = (float)(ptEnd.y);
|
vec p1(xPos, yPos, 0);
|
vEdgeCtrlPoint.push_back(p1);
|
}
|
}
|
}
|
}
|
Point2D point = vEdgePoints[i];
|
vec p0((float)point.x, (float)point.y, 0);
|
vEdgeCtrlPoint.push_back(p0);
|
}
|
|
CBspline bspline(vEdgeCtrlPoint);
|
bspline.execute();
|
|
int number = (int)(bspline.m_vPtResults.size());
|
if (number < 10) return 0;
|
|
std::vector<Point2D> vPointResults;
|
for (int i = 0; i < number; i++) {
|
Point2D point;
|
point.x = bspline.m_vPtResults[i][0] + nOffset;
|
point.y = bspline.m_vPtResults[i][1];
|
vPointResults.push_back(point);
|
}
|
|
HalconCpp::HTuple hvY, hvX;
|
int idx = 0;
|
for (auto item : vPointResults) {
|
hvX[idx] = item.x;
|
hvY[idx] = item.y;
|
idx += 1;
|
}
|
|
hvX[idx] = width - 5;
|
hvY[idx] = height - 5;
|
idx += 1;
|
|
hvX[idx] = width - 5;
|
hvY[idx] = 5;
|
idx += 1;
|
|
HalconCpp::HObject hRoiRegion;
|
HalconCpp::GenRegionPolygonFilled(&hRoiRegion, hvY, hvX);
|
|
#if 0
|
HalconCpp::HWindow hDispWin(0, 0, 0.4 * width, 0.4 * height);
|
hDispWin.SetPart(0, 0, height-1, width-1);
|
hDispWin.SetColor("green");
|
hDispWin.SetDraw("margin");
|
hDispWin.DispObj(hImage);
|
hDispWin.DispObj(hRoiRegion);
|
hDispWin.Click();
|
#endif
|
|
HalconCpp::HObject hBinRegion, hRegion, hConnectRegion, hSelectRegion;
|
HalconCpp::ReduceDomain(hImage, hRoiRegion, &hBinRegion);
|
HalconCpp::Threshold(hBinRegion, &hRegion, 0, nBinThres);
|
HalconCpp::Connection(hRegion, &hConnectRegion);
|
if (1 == szType) {
|
HalconCpp::HObject hSelectRegionX, hSelectRegionY, hConcatRegion, hUinonRegion, hClosngRegion;
|
HalconCpp::SelectShape(hConnectRegion, &hSelectRegionX, "width", "and", xFzSize, 99999999);
|
HalconCpp::SelectShape(hConnectRegion, &hSelectRegionY, "height", "and", yFzSize, 99999999);
|
HalconCpp::ConcatObj(hSelectRegionX, hSelectRegionY, &hConcatRegion);
|
HalconCpp::Union1(hConcatRegion, &hUinonRegion);
|
HalconCpp::ClosingCircle(hUinonRegion, &hClosngRegion, 7.5);
|
HalconCpp::Connection(hClosngRegion, &hSelectRegion);
|
}
|
else {
|
HalconCpp::HObject hSelectRegionX, hSelectRegionY, hConcatRegion, hUinonRegion, hClosngRegion;
|
HalconCpp::SelectShape(hConnectRegion, &hSelectRegionX, "width", "and", xFzSize, 99999999);
|
HalconCpp::SelectShape(hSelectRegionX, &hSelectRegion, "height", "and", yFzSize, 99999999);
|
HalconCpp::Union1(hSelectRegion, &hUinonRegion);
|
HalconCpp::ClosingCircle(hUinonRegion, &hClosngRegion, 7.5);
|
hSelectRegion.Clear();
|
HalconCpp::Connection(hClosngRegion, &hSelectRegion);
|
}
|
#if 0
|
hDispWin.SetColored(6);
|
hDispWin.SetDraw("fill");
|
hDispWin.DispObj(hSelectRegion);
|
hDispWin.Click();
|
#endif
|
int nRet = ClsVision::ObjectNumber(hSelectRegion);
|
if (nRet < 1) return 0;
|
|
HalconCpp::HTuple hvY1, hvX1, hvY2, hvX2;
|
HalconCpp::SmallestRectangle1(hSelectRegion, &hvY1, &hvX1, &hvY2, &hvX2);
|
int nNumber = 0;
|
for (int i = 0; i < nRet; i++) {
|
aryResult[i].left = hvX1[i].I() + roiRect.left;
|
aryResult[i].top = hvY1[i].I() + roiRect.top;
|
aryResult[i].right = hvX2[i].I() + roiRect.left;
|
aryResult[i].bottom = hvY2[i].I() + roiRect.top;
|
nNumber += 1;
|
if (i > 99) break;
|
}
|
|
return nNumber;
|
}
|