void CropImgScreen::onIconPressed(int i) { if(i==0)//ok { imgIcon->saveCropImg(fileNameForSave); emit imgCropped(fileNameForSave); } this->hide(); }
long QSProcessThreadFunc(CTCSys *QS) { int i; int pass = -1; while (QS->EventEndProcess == FALSE) { #ifdef PTGREY if (QS->IR.Acquisition == TRUE) { for (i = 0; i < QS->IR.NumCameras; i++) { if (QS->IR.pgrCamera[i]->RetrieveBuffer(&QS->IR.PtGBuf[i]) == PGRERROR_OK) { QS->QSSysConvertToOpenCV(&QS->IR.AcqBuf[i], QS->IR.PtGBuf[i]); } } for (i = 0; i < QS->IR.NumCameras; i++) { #ifdef PTG_COLOR mixChannels(&QS->IR.AcqBuf[i], 1, &QS->IR.ProcBuf[i], 1, QS->IR.from_to, 3); // Swap B and R channels anc=d copy out the image at the same time. #else QS->IR.AcqBuf[i].copyTo(QS->IR.ProcBuf[i][BufID]); // Has to copy out of acquisition buffer before processing #endif } } #else Sleep(200); #endif // Process Image ProcBuf if (QS->IR.Inspection) { // Images are acquired into ProcBuf{0] // May need to create child image for processing to exclude background and speed up processing for (i = 0; i < QS->IR.NumCameras; i++) { //Obtain image Mat imgFull = QS->IR.ProcBuf[i]; //Crop image static Rect roi; if (roi.width == 0) calcAndOrDraw(imgFull, roi, true); Mat imgCropped(imgFull, roi); Mat img = imgCropped; // Threshold Mat imgHsv, d0, d2; cvtColor(img, imgHsv, CV_BGR2HSV); channelDist(imgHsv, d0, 16, 0); bitwise_not(d0, d0); getChannel(imgHsv, d2, 2); addWeighted(d0, .5, d2, .5, 0, img); GaussianBlur(img, img, Size(5, 5), 0); static int th = 0; if (th == 0) th = cv::threshold(img, img, 60, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); else cv::threshold(img, img, th, 255, CV_THRESH_BINARY); //Find contours vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(img, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); img = Mat::zeros(img.size(), CV_8UC3); for (int i = 0; i < contours.size(); i++) drawContours(img, contours, i, Scalar(255, 255, 255)); // Find parent contour int pretzelContourIndex = -1; int minPretzelSize = 1000; int largestContourSize = minPretzelSize; for (int i = 0; i < hierarchy.size(); i++) { if (hierarchy[i][3] == -1) { Moments mm = moments((Mat)contours[i]); if (mm.m00 > largestContourSize) { if (pretzelContourIndex != -1) // if multiple pretzels { pretzelContourIndex = -2; break; } pretzelContourIndex = i; largestContourSize = mm.m00; printf("Size: %d\n", (int)mm.m00); } } } int pretzelSize = largestContourSize; // Evaluate pretzel based on contour children int minHoleSize = 10; if (pretzelContourIndex != -1 && pretzelContourIndex != -2) { // Find center of mass Moments mm = moments((Mat)contours[pretzelContourIndex]); double centerX = (mm.m10 / mm.m00); double centerY = (mm.m01 / mm.m00); circle(img, Point(centerX, centerY), 4, Scalar(0, 255, 0)); int borderSize = 100; if (centerY > borderSize && centerY < img.size().height - borderSize) { int numberOfHoles = 0; int child = hierarchy[pretzelContourIndex][2]; while (child != -1) { if (contours[child].size() > minHoleSize) numberOfHoles++; child = hierarchy[child][0]; } if (numberOfHoles <= 1) pass = 2; else if (numberOfHoles == 2) pass = 1; else if (numberOfHoles == 3) pass = 0; } else pass = 3; //no pretzel on belt } else if (pretzelContourIndex == -1) pass = 3; //no pretzel on belt else //if (pretzelContourIndex == -2) pass = -1; //error //Output Image if (img.channels() != 3) cvtColor(img, img, CV_GRAY2BGR); img.copyTo(imgCropped); if (imgFull.channels() == 3) cvtColor(imgFull, imgFull, CV_BGR2GRAY); imgFull.copyTo(QS->IR.OutBuf1[i]); } } // Display Image if (QS->IR.UpdateImage) { for (i = 0; i<QS->IR.NumCameras; i++) { if (!QS->IR.Inspection) { // Example of displaying color buffer ProcBuf QS->IR.ProcBuf[i].copyTo(QS->IR.DispBuf[i]); } else { // Example of displaying B/W buffer OutBuf1 QS->IR.OutBuf[0] = QS->IR.OutBuf[1] = QS->IR.OutBuf[2] = QS->IR.OutROI1[i]; merge(QS->IR.OutBuf, 3, QS->IR.DispROI[i]); // Example to show inspection result, print result after the image is copied QS->QSSysPrintResult(pass); } } QS->QSSysDisplayImage(); } } QS->EventEndProcess = FALSE; return 0; }