#include "AlgorithmTpi.h" #include #include CAlgorithmTpi::CAlgorithmTpi() //: m_root(NULL) { } CAlgorithmTpi::~CAlgorithmTpi() { //if (NULL != m_root) //{ // delete m_root; //} } struct tagOthers{ int i; double erea; double arcLen; double radius; double angle; double distance; }; bool othersLessThan(const tagOthers &s1, const tagOthers &s2) { return (s1.angle < s2.angle);// ? (s1.angle < s2.angle) : (s1.radius < s2.radius); } inline double diffAngle(double a1, double a2) { double diff = a1 - a2; if (diff < -180) { diff += 360; } else if (diff > 180) { diff = 360 - diff; } if (diff < 0.0) { diff = -diff; } return diff; } int CAlgorithmTpi::IImageAnalysis(class IImageObject* pImgObj, TP_ALGORITHM_OPTION* pOpt) { emTpColorFormat colorFormat = pImgObj->IColorFormat(); if (TP_COLOR_Y800 != colorFormat) { return 0; } int nImageW; int nImageH; int nBitsPerPixel; int nBytesPerLine; BYTE* pImageData = pImgObj->IImageData(nImageW, nImageH, nBitsPerPixel, nBytesPerLine); cv::Mat matSrc(nImageH, nImageW, CV_8UC1, pImageData); //cv::Mat matDes(nImageW, nImageH, CV_8UC1); cv::threshold(matSrc, matSrc, 200, 255, /*CV_THRESH_OTSU*/CV_THRESH_BINARY_INV);//must use INV simple_draw_image_8(pImgObj, NULL, 0); cv::vector> contours; //CvSeq hierarchy; cv::vector hierarchy; cv::findContours(matSrc, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); QList childs; QList tops; QList others; for (int i = 0; i < contours.size(); ++i) { if (contours[i].size() < 4) { continue; } // double fArea = cv::contourArea(contours[i]); if (fArea < 50.0) { continue; } cv::Rect rect = cv::boundingRect(contours[i]); double fRect = rect.width*rect.height; double fRate = fArea / fRect; if (abs(rect.width - rect.height) > 5 || fRate < 0.70 || fRate > 0.90) { tagOthers other = { 0 }; other.i = i; other.erea = fArea; others.append(other); continue; } else { if (-1 == hierarchy[i][3]) { tops.append(i); } else { childs.append(i); } } POINT *pData = new POINT[contours[i].size()]; for (int j = 0; j < contours[i].size(); ++j) { pData[j].x = contours[i][j].x; pData[j].y = contours[i][j].y; } pImgObj->IDrawDefectOrg(IImageObject::TP_DEF_POLYLINE_I, (BYTE*)pData, contours[i].size() * sizeof(POINT)); } //找最外层的圆 cv::Point2f topCenter; float topRadius = 0.0f; int topIndex = -1; for (QList::iterator it = tops.begin(); it != tops.end(); ++it) { cv::Point2f tmpCt; float tmpR; cv::minEnclosingCircle(contours[*it], tmpCt, tmpR); if (tmpR > topRadius) { topRadius = tmpR; topCenter = tmpCt; topIndex = *it; } } if (-1 == topIndex) { return 0; } //找内层的园的个数,只能等于1 int nChilds = 0; int centerIdx = -1; for (QList::iterator it = childs.begin(); it != childs.end(); ++it) { if (hierarchy[*it][3] == topIndex) { ++nChilds; centerIdx = *it; } } if (1 != nChilds) { return 0; } cv::Point2f childCenter; float childRadius; cv::minEnclosingCircle(contours[centerIdx], childCenter, childRadius); //去除不在圆内的形状 for (int i = others.size() - 1; i >= 0; --i) { if (hierarchy[others[i].i][3] != topIndex ) { others.removeAt(i); } } // cv::Point2f ptCenter = childCenter; //ptCenter.x = (topCenter.x + childCenter.x) / 2; //ptCenter.y = (topCenter.y + childCenter.y) / 2; for (QList::iterator it = others.begin(); it != others.end(); ++it) { it->arcLen = cv::arcLength(contours[it->i], false); //if (it->erea / arclen < 3.0) //{ // continue; //} cv::RotatedRect rRect = cv::minAreaRect(contours[it->i]); double fdx = rRect.center.x - ptCenter.x; double fdy = rRect.center.y - ptCenter.y; it->radius = cv::sqrt(fdx*fdx + fdy*fdy); if (fdx > -0.01&&fdx < 0.01) { it->angle = 90.0; } else { it->angle = atan(fdy / fdx) * 180 / PI; } if (fdx < 0.0) { if (fdy > 0.0) { it->angle += 180; } else { it->angle -= 180; } } POINT pData[5]; cv::Point2f pointfs[4]; rRect.points(pointfs); for (int j = 0; j < 4; ++j) { pData[j].x = pointfs[j].x; pData[j].y = pointfs[j].y; } pData[4] = pData[0]; pImgObj->IDrawDefectOrg(IImageObject::TP_DEF_POLYLINE_I, (BYTE*)pData, 5 * sizeof(POINT)); } qSort(others.begin(), others.end(), othersLessThan); tpDebugOut("parameters:"); double fOldAngle = INT_MAX; int nTimes = 0; QList angles; for (QList::iterator it = others.begin(); it != others.end(); ++it) { tpDebugOut("i = %d, arcLen = %.1f, area = %.1f, radius = %.1f, angle = %.1f", it->i, it->arcLen, it->erea, it->radius, it->angle); double fDiff = it->angle - fOldAngle; if (fDiff < 1.0 && fDiff > -1.0) { ++nTimes; continue; } if (nTimes > 0) { angles.append(fOldAngle); } fOldAngle = it->angle; nTimes = 0; } if (nTimes > 0) { angles.append(fOldAngle); } if (angles.size() < 3) { return 0; } double avgAngle = 360. / angles.size(); fOldAngle = angles[0]; double diffTotal = 0; for (int i = 1; i < angles.size(); ++i) { double diff = diffAngle(angles[i], fOldAngle); diff -=avgAngle; diffTotal += diff*diff; fOldAngle = angles[i]; } double diff = diffAngle(angles[0],fOldAngle); diff -= avgAngle; diffTotal += diff*diff; if (diffTotal > 10.0) { return 0; } // //cv::Mat matMask(nImageH, nImageW, CV_8UC1); //memset(matMask.ptr(), 0, matMask.total()); //cv::circle(matMask, topCenter, topRadius, cv::Scalar(255), -1); //cv::circle(matMask, topCenter, childRadius, cv::Scalar(0), -1); // return 1; } //bool CAlgorithmTpi::addContour(int self, int parent, tagTreeNode& node) //{ // if (-1 == parent) // { // m_root->childs.insert(self, tagTreeNode{}); // return true; // } // // // if (node.childs.contains(parent)) // { // node.childs[parent].childs.insert(self, tagTreeNode{}); // return true; // } // for (QMap::iterator it = node.childs.begin(); it != node.childs.end(); ++it) // { // if (addContour(self, parent, it.value())) // { // return true; // } // } // return false; //}