cv::Mat FSVision::subLaser(cv::Mat &laserOff, cv::Mat &laserOn, double threshold) { unsigned int cols = laserOff.cols; unsigned int rows = laserOff.rows; cv::Mat bwLaserOff( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat bwLaserOn( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat diffImage( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat treshImage( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat result( cols,rows,CV_8UC3,cv::Scalar(100) ); cv::cvtColor(laserOff, bwLaserOff, CV_RGB2GRAY); //convert to grayscale cv::cvtColor(laserOn, bwLaserOn, CV_RGB2GRAY); //convert to grayscale cv::subtract(bwLaserOn,bwLaserOff,diffImage); //subtract both grayscales cv::GaussianBlur(diffImage,diffImage,cv::Size(5,5),3); //gaussian filter cv::threshold(diffImage,treshImage,threshold,255,cv::THRESH_BINARY); //apply threshold cv::Mat element5(3,3,CV_8U,cv::Scalar(1)); cv::morphologyEx(treshImage,treshImage,cv::MORPH_OPEN,element5); cv::cvtColor(treshImage, result, CV_GRAY2RGB); //convert back ro rgb /*cv::namedWindow("laserLine"); cv::imshow("laserLine", result); cv::waitKey(0); cv::destroyWindow("laserLine");*/ return result; }
cv::Mat FSVision::diffImage(cv::Mat &laserOff, cv::Mat &laserOn) { unsigned int cols = laserOff.cols; unsigned int rows = laserOff.rows; cv::Mat bwLaserOff( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat bwLaserOn( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat diffImage( cols,rows,CV_8U,cv::Scalar(100) ); cv::Mat result( cols,rows,CV_8UC3,cv::Scalar(100) ); cv::cvtColor(laserOff, bwLaserOff, CV_RGB2GRAY); //convert to grayscale cv::cvtColor(laserOn, bwLaserOn, CV_RGB2GRAY); //convert to grayscale cv::subtract(bwLaserOn,bwLaserOff,diffImage); //subtract both grayscales cv::cvtColor(diffImage, result, CV_GRAY2RGB); //convert back ro rgb return result; }
void WinImage::slotDiffer() { // Differ specific QImage imageExpected; QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) imageExpected = QImage( fileName); _image = diffImage( _image, imageExpected, _a_number->value()); update(); //printf("File %s differs ", fileName); QString message = "Differs... "; statusBar()->showMessage( message); }
QPixmap MismatchWindow::computeDiffPixmap(const QImage& expectedImage, const QImage& resultImage) { // Create an empty difference image if the images differ in size if (expectedImage.height() != resultImage.height() || expectedImage.width() != resultImage.width()) { return QPixmap(); } // This is an optimization, as QImage.setPixel() is embarrassingly slow unsigned char* buffer = new unsigned char[expectedImage.height() * expectedImage.width() * 3]; // loop over each pixel for (int y = 0; y < expectedImage.height(); ++y) { for (int x = 0; x < expectedImage.width(); ++x) { QRgb pixelP = expectedImage.pixel(QPoint(x, y)); QRgb pixelQ = resultImage.pixel(QPoint(x, y)); // Convert to luminance double p = R_Y * qRed(pixelP) + G_Y * qGreen(pixelP) + B_Y * qBlue(pixelP); double q = R_Y * qRed(pixelQ) + G_Y * qGreen(pixelQ) + B_Y * qBlue(pixelQ); // The intensity value is modified to increase the brightness of the displayed image double absoluteDifference = fabs(p - q) / 255.0; double modifiedDifference = sqrt(absoluteDifference); int difference = (int)(modifiedDifference * 255.0); buffer[3 * (x + y * expectedImage.width()) + 0] = difference; buffer[3 * (x + y * expectedImage.width()) + 1] = difference; buffer[3 * (x + y * expectedImage.width()) + 2] = difference; } } QImage diffImage(buffer, expectedImage.width(), expectedImage.height(), QImage::Format_RGB888); QPixmap resultPixmap = QPixmap::fromImage(diffImage); delete[] buffer; return resultPixmap; }
Mat ScanProc::DetectLaser2(Mat &laserOn, Mat &laserOff) { //some parameter need to be tuned int laserMagnitudeThreshold = 20; //default: 10 int maxLaserWidth = 40, minLaserWidth = 3; //default: 40, 3 int rangeDistanceThreshold = 5; //default: 5 int& rows = laserOff.rows; int& cols = laserOff.cols; Mat grayLaserOn(rows, cols, CV_8U, Scalar(0)); Mat grayLaserOff(rows, cols, CV_8U, Scalar(0)); Mat diffImage(rows, cols, CV_8U, Scalar(0)); Mat thresholdImage(rows, cols, CV_8U, Scalar(0)); Mat rangeImage(rows, cols, CV_8U, Scalar(0)); Mat result(rows, cols, CV_8U, Scalar(0)); // convert to grayscale cvtColor(laserOff, grayLaserOff, CV_BGR2GRAY); cvtColor(laserOn, grayLaserOn, CV_BGR2GRAY); subtract(grayLaserOn, grayLaserOff, diffImage); //const double MAX_MAGNITUDE_SQ = 255 * 255 * 3; // The maximum pixel magnitude sq we can see //const double INV_MAX_MAGNITUDE_SQ = 1.0f / MAX_MAGNITUDE_SQ; const int width = grayLaserOff.cols; const int height = grayLaserOff.rows; //unsigned components = before.getNumComponents(); //unsigned rowStep = width * components; int numLocations = 0; int numMerged = 0; int numPixelsOverThreshold = 0; // The location that we last detected a laser line int maxNumLocations = height; int firstRowLaserCol = width / 2; int prevLaserCol = firstRowLaserCol; LaserRange* laserRanges = new LaserRange[width + 1]; //unsigned char * ar = a; //unsigned char * br = b; //unsigned char * dr = d; for (unsigned iRow = 0; iRow < height && numLocations < maxNumLocations; iRow++) { // The column that the laser started and ended on int numLaserRanges = 0; laserRanges[numLaserRanges].startCol = -1; laserRanges[numLaserRanges].endCol = -1; int numRowOut = 0; int imageColumn = 0; for (unsigned iCol = 0; iCol < width; iCol += 1) { int mag = diffImage.at<uchar>(iRow, iCol); // Compare it against the threshold if (mag > laserMagnitudeThreshold) { thresholdImage.at<uchar>(iRow, iCol) = 255; // Flag that this pixel was over the threshold value numPixelsOverThreshold++; // The start of pixels with laser in them if (laserRanges[numLaserRanges].startCol == -1) { laserRanges[numLaserRanges].startCol = imageColumn; } } // The end of pixels with laser in them else if (laserRanges[numLaserRanges].startCol != -1) { int laserWidth = imageColumn - laserRanges[numLaserRanges].startCol; if (laserWidth <= maxLaserWidth && laserWidth >= minLaserWidth) { // If this range was real close to the previous one, merge them instead of creating a new one bool wasMerged = false; if (numLaserRanges > 0) { unsigned rangeDistance = laserRanges[numLaserRanges].startCol - laserRanges[numLaserRanges - 1].endCol; if (rangeDistance < rangeDistanceThreshold) { laserRanges[numLaserRanges - 1].endCol = imageColumn; laserRanges[numLaserRanges - 1].centerCol = round((laserRanges[numLaserRanges - 1].startCol + laserRanges[numLaserRanges - 1].endCol) / 2); wasMerged = true; numMerged++; } } // Proceed to the next laser range if (!wasMerged) { // Add this range as a candidate laserRanges[numLaserRanges].endCol = imageColumn; laserRanges[numLaserRanges].centerCol = round((laserRanges[numLaserRanges].startCol + laserRanges[numLaserRanges].endCol) / 2); numLaserRanges++; } // Reinitialize the range laserRanges[numLaserRanges].startCol = -1; laserRanges[numLaserRanges].endCol = -1; } // There was a false positive else { laserRanges[numLaserRanges].startCol = -1; } } // Go from image components back to image pixels imageColumn++; } // foreach column // If we have a valid laser region if (numLaserRanges > 0) { for (int i = 0; i < numLaserRanges; i++) { for (int j = laserRanges[i].startCol; j < laserRanges[i].endCol; j++) { rangeImage.at<uchar>(iRow, j) = 255; } } int rangeChoice = detectBestLaserRange(laserRanges, numLaserRanges, prevLaserCol); prevLaserCol = laserRanges[rangeChoice].centerCol; int centerCol = detectLaserRangeCenter(iRow, laserRanges[rangeChoice], diffImage); result.at<uchar>(iRow, centerCol) = 255; //laserLocations[numLocations].x = centerCol; //laserLocations[numLocations].y = iRow; // If this is the first row that a laser is detected in, set the firstRowLaserCol member if (numLocations == 0) { firstRowLaserCol = laserRanges[rangeChoice].startCol; } numLocations++; } } // foreach row pPreviewWnd->updateFrame(grayLaserOff, "laser off"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); pPreviewWnd->updateFrame(grayLaserOn, "laser on"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); pPreviewWnd->updateFrame(diffImage, "diff"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); pPreviewWnd->updateFrame(thresholdImage, "threshold"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); pPreviewWnd->updateFrame(rangeImage, "laser range"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); pPreviewWnd->updateFrame(result, "result"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); return result; }
Mat ScanProc::DetectLaser(Mat &laserOn, Mat &laserOff) { Q_ASSERT(pPreviewWnd); unsigned int cols = laserOn.cols; unsigned int rows = laserOn.rows; Mat grayLaserOn(rows,cols,CV_8U,Scalar(0)); Mat grayLaserOff(rows,cols,CV_8U,Scalar(0)); Mat diffImage(rows,cols,CV_8U,Scalar(0)); Mat gaussImage(rows,cols,CV_8U,Scalar(0)); Mat laserImage(rows,cols,CV_8U,Scalar(0)); Mat result(rows,cols,CV_8UC3,Scalar(0)); //QImage imglaserOn((uchar*)laserOn.data, laserOn.cols, laserOn.rows, QImage::Format_RGB888); //imglaserOn = imglaserOn.rgbSwapped(); //imglaserOn.save(QString("laserOn.jpg"),"JPEG"); pPreviewWnd->updateFrame(laserOn, "laserOn"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); //QImage imglaserOff((uchar*)laserOff.data, laserOff.cols, laserOff.rows, QImage::Format_RGB888); //imglaserOff = imglaserOff.rgbSwapped(); //imglaserOff.save(QString("laserOff.jpg"),"JPEG"); pPreviewWnd->updateFrame(laserOff, "laserOff"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); // convert to grayscale cvtColor(laserOn, grayLaserOn, CV_BGR2GRAY); cvtColor(laserOff, grayLaserOff, CV_BGR2GRAY); pPreviewWnd->updateFrame(grayLaserOn, "grayLaserOn"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); pPreviewWnd->updateFrame(grayLaserOff, "grayLaserOff"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); // diff image subtract(grayLaserOn,grayLaserOff,diffImage); pPreviewWnd->updateFrame(diffImage, "diffImage"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); /* // apply gaussian GaussianBlur(diffImage,gaussImage,Size(15,15),12,12); diffImage = diffImage-gaussImage; pPreviewWnd->updateFrame(diffImage, "gaussian"); msleep(LASER_LINE_DETECTION_DELAY); */ // apply threshold double threshold = 10; cv::threshold(diffImage, diffImage, threshold, 255, THRESH_TOZERO); pPreviewWnd->updateFrame(diffImage, "threshold"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); // apply erode erode(diffImage,diffImage,Mat(3,3,CV_8U,Scalar(1)) ); pPreviewWnd->updateFrame(diffImage, "erode"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); // apply canny Canny(diffImage,diffImage,20,50); pPreviewWnd->updateFrame(diffImage, "canny"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); int* edges = new int[cols]; //contains the cols index of the detected edges per row for(unsigned int y = 0; y < rows; y++) { //reset the detected edges for(unsigned int j=0; j < cols; j++) { edges[j] = -1; } int j=0; for(unsigned int x = 0; x < cols; x++) { if(diffImage.at<uchar>(y,x) > 250) { edges[j] = x; j++; } } //iterate over detected edges, take middle of two edges for(unsigned int j = 0; j < cols - 1; j += 2) { if(edges[j] >= 0 && edges[j+1] >= 0 && edges[j+1] - edges[j] < 40) { int middle = (int)(edges[j] + edges[j+1]) / 2; laserImage.at<uchar>(y,middle) = 255; } } } delete [] edges; pPreviewWnd->updateFrame(laserImage, "laserLine"); msleep(LASER_LINE_DETECTION_PREVIEW_DELAY); //cvtColor(laserImage, result, CV_GRAY2RGB); //convert back ro rgb //QImage imgResult((uchar*)result.data, result.cols, result.rows, QImage::Format_RGB888); //imgResult.save(QString("result.jpg"),"JPEG"); return laserImage; }
int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); qreal tolerance = 0; QStringList args = app.arguments(); for (int i = 0; i < argc; ++i) if (args[i] == "-t" || args[i] == "--tolerance") tolerance = args[i + 1].toDouble(); char buffer[2048]; QImage actualImage; QImage baselineImage; while (fgets(buffer, sizeof(buffer), stdin)) { // remove the CR char* newLineCharacter = strchr(buffer, '\n'); if (newLineCharacter) *newLineCharacter = '\0'; if (!strncmp("Content-Length: ", buffer, 16)) { strtok(buffer, " "); int imageSize = strtol(strtok(0, " "), 0, 10); if (imageSize <= 0) { fputs("error, image size must be specified.\n", stdout); } else { unsigned char buffer[2048]; QBuffer data; // Read all the incoming chunks data.open(QBuffer::WriteOnly); while (imageSize > 0) { size_t bytesToRead = qMin(imageSize, 2048); size_t bytesRead = fread(buffer, 1, bytesToRead, stdin); data.write(reinterpret_cast<const char*>(buffer), bytesRead); imageSize -= static_cast<int>(bytesRead); } // Convert into QImage QImage decodedImage; decodedImage.loadFromData(data.data(), "PNG"); decodedImage.convertToFormat(QImage::Format_ARGB32); // Place it in the right place if (actualImage.isNull()) actualImage = decodedImage; else baselineImage = decodedImage; } } if (!actualImage.isNull() && !baselineImage.isNull()) { if (actualImage.size() != baselineImage.size()) { fprintf(stdout, "diff: 100%% failed\n"); fprintf(stderr, "error, test and reference image have different properties.\n"); fflush(stderr); fflush(stdout); } else { int w = actualImage.width(); int h = actualImage.height(); QImage diffImage(w, h, QImage::Format_ARGB32); int count = 0; qreal sum = 0; qreal maxDistance = 0; for (int x = 0; x < w; ++x) for (int y = 0; y < h; ++y) { QRgb pixel = actualImage.pixel(x, y); QRgb basePixel = baselineImage.pixel(x, y); qreal red = (qRed(pixel) - qRed(basePixel)) / static_cast<float>(qMax(255 - qRed(basePixel), qRed(basePixel))); qreal green = (qGreen(pixel) - qGreen(basePixel)) / static_cast<float>(qMax(255 - qGreen(basePixel), qGreen(basePixel))); qreal blue = (qBlue(pixel) - qBlue(basePixel)) / static_cast<float>(qMax(255 - qBlue(basePixel), qBlue(basePixel))); qreal alpha = (qAlpha(pixel) - qAlpha(basePixel)) / static_cast<float>(qMax(255 - qAlpha(basePixel), qAlpha(basePixel))); qreal distance = qSqrt(red * red + green * green + blue * blue + alpha * alpha) / 2.0f; int gray = distance * qreal(255); diffImage.setPixel(x, y, qRgb(gray, gray, gray)); if (distance >= 1 / qreal(255)) { count++; sum += distance; maxDistance = qMax(maxDistance, distance); } } qreal difference = 0; if (count) difference = 100 * sum / static_cast<qreal>(w * h); if (difference <= tolerance) { difference = 0; } else { difference = qRound(difference * 100) / 100.0f; difference = qMax(difference, qreal(0.01)); } if (!difference) fprintf(stdout, "diff: %01.2f%% passed\n", difference); else { QBuffer buffer; buffer.open(QBuffer::WriteOnly); diffImage.save(&buffer, "PNG"); buffer.close(); const QByteArray &data = buffer.data(); printf("Content-Length: %lu\n", static_cast<unsigned long>(data.length())); // We have to use the return value of fwrite to avoid "ignoring return value" gcc warning // See https://bugs.webkit.org/show_bug.cgi?id=45384 for details. if (fwrite(data.constData(), 1, data.length(), stdout)) {} fprintf(stdout, "diff: %01.2f%% failed\n", difference); } fflush(stdout); } actualImage = QImage(); baselineImage = QImage(); } } return 0; }
TestCase::IterateResult ReadPixelsCase::iterate (void) { const tcu::RenderTarget& renderTarget = m_context.getRenderTarget(); tcu::PixelFormat pixelFormat = renderTarget.getPixelFormat(); int targetWidth = renderTarget.getWidth(); int targetHeight = renderTarget.getHeight(); int x = 0; int y = 0; int imageWidth = 0; int imageHeight = 0; deRandom rnd; deRandom_init(&rnd, deInt32Hash(m_curIter)); switch (m_curIter) { case 0: // Fullscreen x = 0; y = 0; imageWidth = targetWidth; imageHeight = targetHeight; break; case 1: // Upper left corner x = 0; y = 0; imageWidth = targetWidth / 2; imageHeight = targetHeight / 2; break; case 2: // Lower right corner x = targetWidth / 2; y = targetHeight / 2; imageWidth = targetWidth - x; imageHeight = targetHeight - y; break; default: x = deRandom_getUint32(&rnd) % (targetWidth - 1); y = deRandom_getUint32(&rnd) % (targetHeight - 1); imageWidth = 1 + (deRandom_getUint32(&rnd) % (targetWidth - x - 1)); imageHeight = 1 + (deRandom_getUint32(&rnd) % (targetHeight - y - 1)); break; } Surface resImage(imageWidth, imageHeight); Surface refImage(imageWidth, imageHeight); Surface diffImage(imageWidth, imageHeight); int r = (int)(deRandom_getUint32(&rnd) & 0xFF); int g = (int)(deRandom_getUint32(&rnd) & 0xFF); int b = (int)(deRandom_getUint32(&rnd) & 0xFF); tcu::clear(refImage.getAccess(), tcu::IVec4(r, g, b, 255)); glClearColor(r/255.0f, g/255.0f, b/255.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glu::readPixels(m_context.getRenderContext(), x, y, resImage.getAccess()); GLU_CHECK_MSG("glReadPixels() failed."); RGBA colorThreshold = pixelFormat.getColorThreshold(); RGBA matchColor(0, 255, 0, 255); RGBA diffColor(255, 0, 0, 255); bool isImageOk = true; for (int j = 0; j < imageHeight; j++) { for (int i = 0; i < imageWidth; i++) { RGBA resRGBA = resImage.getPixel(i, j); RGBA refRGBA = refImage.getPixel(i, j); bool isPixelOk = compareThreshold(refRGBA, resRGBA, colorThreshold); diffImage.setPixel(i, j, isPixelOk ? matchColor : diffColor); isImageOk = isImageOk && isPixelOk; } } if (isImageOk) m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass"); else { m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail"); TestLog& log = m_testCtx.getLog(); log << TestLog::ImageSet("Result", "Resulting framebuffer") << TestLog::Image("Result", "Resulting framebuffer", resImage) << TestLog::Image("Reference", "Reference image", refImage) << TestLog::Image("DiffMask", "Failing pixels", diffImage) << TestLog::EndImageSet; } return (++m_curIter < m_numIters) ? CONTINUE : STOP; }
cv::Mat FSVision::subLaser2(cv::Mat &laserOff, cv::Mat &laserOn,int JD) { unsigned int cols = laserOff.cols; unsigned int rows = laserOff.rows; cv::Mat bwLaserOff( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat bwLaserOn( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat tresh2Image( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat diffImage( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat gaussImage( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat laserImage( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat result( rows,cols,CV_8UC3,cv::Scalar(0) ); std::vector<cv::Mat> rgbChannelsOff(3); cv::split(laserOff, rgbChannelsOff); // cv::cvtColor(laserOff, bwLaserOff, CV_RGB2GRAY);//convert to grayscale bwLaserOff=rgbChannelsOff[0]; //cv::imshow("laserLineOff", bwLaserOff); cv::waitKey(10); std::vector<cv::Mat> rgbChannelsOn(3); cv::split(laserOn, rgbChannelsOn); // cv::cvtColor(laserOn, bwLaserOn, CV_RGB2GRAY); //convert to grayscale bwLaserOn = rgbChannelsOn[0]; // cv::subtract(bwLaserOn,bwLaserOff,diffImage); //subtract both grayscales // cv::cvtColor(laserOn, diffImage, CV_RGB2GRAY); tresh2Image = diffImage.clone(); //cv::imshow("tresh2Image1", tresh2Image); cv::waitKey(10); //细化测试 cv::Mat img=tresh2Image.clone(); //cv::equalizeHist(img,img); cv::blur(img,img, cv::Size(5,5)); //cv::imshow("tresh2Image1_2", img); cv::waitKey(10); cv::threshold(img, img,40, 255, cv::THRESH_BINARY); cv::blur(img,img, cv::Size(3,3)); char buffer [256]; if(JD%4==0) { JD=JD/4; sprintf (buffer, "D:\\img\\img1\\sd_spline-%d.jpg",JD); cv::imwrite(buffer,img); sprintf (buffer, "D:\\img\\img2\\tex_image-%d.jpg",JD); cv::imwrite(buffer,laserOff); } normalizeLetter(img,img); //很好的细化 // cv::imshow("tresh2Image2", img); cv::waitKey(10); // img.convertTo(img,CV_32FC1); // skeletonize(img); //很好的细化 //cv::blur(img,img, cv::Size(3,3)); // cv::imshow("tresh2Image3", img); cv::waitKey(0); /* cv::blur(img,img, cv::Size(3,3)); cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3)); cv::erode(img, img, element); // cv::imshow("tresh2Image3", img); cv::waitKey(1000); cv::dilate(img, img, element); cv::dilate(img, img, element); // cv::dilate(img, img, element); cv::blur(img,img, cv::Size(5,5)); cv::imshow("tresh2Image2_2", img); cv::waitKey(10); cv::imshow("tresh2Image3", img); cv::waitKey(10); // cv::blur(img,img, cv::Size(3,3)); cv::Mat element2 = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3)); cv::dilate(img, img, element2);// cv::dilate(img, img, element); cv::blur(img,img, cv::Size(3,3)); // cv::erode(img, img, element2); cv::imshow("tresh2Image4", img); cv::waitKey(0); // atfx atf ;// = new atfx(); // atf.setInputSketch(1-img); // cv::Mat thinned_image = atf.getATFImage(); // cv::imshow("tresh2Image4", img); cv::waitKey(10); // skeletonize(img); //很好的细化 //cv::imshow("tresh2Image5", img); cv::waitKey(10); // cv::dilate(img, img, element2); // cv::imshow("tresh2Image6", img); cv::waitKey(0); //// //cv::namedWindow("tresh2Image2",0); //cv::imshow("tresh2Image2", img); //cv::waitKey(10); //diffImage =img.clone(); // cv::cvtColor(laserImage, result, CV_GRAY2RGB); //convert back ro rgb //return result; //细化 //void cvThin (IplImage* src, IplImage* dst, int iterations) //cvThreshold // cv::blur(tresh2Image,tresh2Image, cv::Size(3,3)); // Apply the specified morphology operation //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); cv::GaussianBlur(diffImage,gaussImage,cv::Size(15,15),12,12); diffImage = diffImage-gaussImage; //cv::imshow("laserLine", diffImage); //cv::waitKey(0); double threshold = 10; cv::threshold(diffImage,diffImage,threshold,255,cv::THRESH_TOZERO); //apply threshold //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); //cv::equalizeHist(diffImage,diffImage); //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); cv::erode(diffImage,diffImage,cv::Mat(3,3,CV_8U,cv::Scalar(1)) ); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); //cv::Mat element5(3,3,CV_8U,cv::Scalar(1)); //cv::morphologyEx(diffImage,diffImage,cv::MORPH_OPEN,element5); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); cv::Canny(diffImage,diffImage,20,50); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); //cv::imshow("laserLine", treshImage+diffImage); //cv::waitKey(0); //cv::destroyWindow("laserLine"); int *edges; edges=new int[cols]; //contains the cols index of the detected edges per row for(unsigned int y = 0; y <rows; y++){ //reset the detected edges for(unsigned int j=0; j<cols; j++){ edges[j]=-1; } int j=0; for(unsigned int x = 0; x<cols; x++){ if(diffImage.at<uchar>(y,x)>250){ edges[j]=x; j++; } } //iterate over detected edges, take middle of two edges for(unsigned int j=0; j<cols-1; j+=2){ if(edges[j]>=0 && edges[j+1]>=0 && edges[j+1]-edges[j]<40){ int middle = (int)(edges[j]+edges[j+1])/2; //qDebug() << cols << rows << y << middle; laserImage.at<uchar>(y,middle) = 255; } } } */ for(unsigned int y = 0; y <img.rows ; y++) { for(unsigned int x = 0; x<img.cols; x++) { if(img.at<int>(y,x)>99) { laserImage.at<uchar>(y,x) = 255; } else { laserImage.at<uchar>(y,x) = 0; } } } // cv::namedWindow("laserLine",0); //cv::imshow("laserLine", diffImage); // cv::imshow("laserLine", laserImage); //cv::waitKey(0); // cv::imshow("laserLine", laser+treshImage); // cv::waitKey(0); //cv::destroyAllWindows(); cv::cvtColor(laserImage, result, CV_GRAY2RGB); //convert back ro rgb return result; }
cv::Mat CSVision::subLaser2(cv::Mat &laserOff, cv::Mat &laserOn) { unsigned int cols = laserOff.cols; unsigned int rows = laserOff.rows; cv::Mat bwLaserOff( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat bwLaserOn( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat tresh2Image( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat diffImage( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat gaussImage( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat laserImage( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat result( rows,cols,CV_8UC3,cv::Scalar(0) ); cv::Mat mresult( rows,cols,CV_8UC3,cv::Scalar(0) ); cv::absdiff(laserOn,laserOff,mresult); //subtract both grayscales cv::threshold(mresult,mresult,12,255,cv::THRESH_BINARY); //CEB temp remove // cv::imshow("color absdiff", mresult); // cv::cvtColor(laserOff, bwLaserOff, CV_RGB2GRAY);//convert to grayscale // cv::cvtColor(laserOn, bwLaserOn, CV_RGB2GRAY); //convert to grayscale // cv::subtract(bwLaserOn,bwLaserOff,diffImage); //subtract both grayscales // CEB good // cv::absdiff(bwLaserOn,bwLaserOff,diffImage); //subtract both grayscales // tresh2Image = diffImage.clone(); // cv::namedWindow("Before Image"); // cv::imshow("subtract", diffImage); cv::cvtColor(mresult,diffImage, CV_RGB2GRAY);//convert to grayscale // cv::imshow("subtract", diffImage); // cv::waitKey(0); // Apply the specified morphology operation //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); /*int morph_elem = 0; int morph_size = 1; // cv::Canny(diffImage,diffImage,20,50); cv::Mat element = cv::getStructuringElement( morph_elem, cv::Size( 2*morph_size + 1, 2*morph_size+1 ), cv::Point( morph_size, morph_size ) ); cv::morphologyEx(diffImage, diffImage, cv::MORPH_OPEN, element);*/ //cv::imshow("laserLine", diffImage); //cv::waitKey(0); // cv::imshow("Before Blur ", diffImage); CEB // cv::GaussianBlur(diffImage,gaussImage,cv::Size(15,15),12,12); // remove CEB // cv::imshow("Gauss image ", gaussImage); // diffImage = diffImage-gaussImage; // CEB removed // cv::imshow("Blur", diffImage); // cv::waitKey(0); // FSFloat threshold = 10; // cv::imshow("Before threshold", diffImage); // cv::threshold(diffImage,diffImage,threshold,255,cv::THRESH_TOZERO); //apply threshold cv::threshold(diffImage,diffImage,11,255,cv::THRESH_BINARY); //CEB good // cv::threshold(diffImage,diffImage,11,255,cv::THRESH_BINARY); //CEB temp remove // cv::threshold(diffImage,diffImage,threshold,255,cv::THRESH_OTSU); //CEB worked // cv::threshold(diffImage,diffImage,threshold,255,cv::THRESH_TOZERO); // cv::imshow("After threshold", diffImage); // cv::imshow("threshold", CSVision::histogram(diffImage)); // cv::waitKey(0); // cv::equalizeHist(diffImage,diffImage); // ceb worked // cv::imshow("threshhold", diffImage); // cv::waitKey(0); //cv::erode(diffImage,diffImage,cv::Mat(3,3,CV_8U,cv::Scalar(1)) ); CEB removing to test cv::erode(diffImage,diffImage,cv::Mat(3,3,CV_8U,cv::Scalar(1)) ); //cv::imshow("Erode", diffImage); cv::dilate(diffImage,diffImage,cv::Mat(3,3,CV_8U,cv::Scalar(1)) ); // cv::imshow("After", diffImage); // cv::imshow("laserLine", diffImage); // cv::waitKey(0); // cv::Mat element5(3,3,CV_8U,cv::Scalar(1)); // ceb worked // cv::morphologyEx(diffImage,diffImage,cv::MORPH_OPEN,element5); // ceb worked // cv::imshow("laserLine", diffImage); // cv::waitKey(0); // cv::Canny(diffImage,diffImage,20,50); CEB // cv::imshow("laserLine", diffImage); // cv::waitKey(0); // cv::imshow("laserLine", treshImage+diffImage); // cv::waitKey(0); // cv::destroyWindow("laserLine"); /* int edges[cols]; //contains the cols index of the detected edges per row for(unsigned int y = 0; y <rows; y++){ //reset the detected edges for(unsigned int j=0; j<cols; j++){ edges[j]=-1; } int j=0; for(unsigned int x = 0; x<cols; x++){ if(diffImage.at<uchar>(y,x)>250){ edges[j]=x; j++; } } //iterate over detected edges, take middle of two edges for(unsigned int j=0; j<cols-1; j+=2) // cv::Canny(diffImage,diffImage,20,50); { //cv::imshow("laserBasic", laserBasic); // cv::waitKey(0); if(edges[j]>=0 && edges[j+1]>=0 && edges[j+1]-edges[j]<40){ int middle = (int)(edges[j]+edges[j+1])/2; //qDebug() << cols << rows << y << middle; laserImage.at<uchar>(y,middle) = 255; } }/ } int edges[cols]; //contains the cols index of the detected edges per row for(unsigned int y = 0; y <rows; y++){ //reset the detected edges for(unsig/ned int j=0; j<cols; j++){ edges[j]=-1; } int j=0;cv::imshow("laserBasic", laserBasic); cv::waitKey(0); for(unsigned int x = 0; x<cols; x++){ if(diffImage.at<uchar>(y,x)>250){ edges[j]=x; j++; } } //iterate over detected edges, take middle of two edges for(unsigned int j=0; j<cols-1; j+=2){ if(edges[j]>=0 && edges[j+1]>=0 && edges[j+1]-edges[j]<40){ int middle = (int)(edges[j]+edges[j+1])/2; //qDebug() << cols << rows << y << middle; laserImage.at<uchar>(y,middle) = 255; } } } } } */ /* cv::namedWindow("laserLine"); cv::imshow("laserLine", diffImage); cv::waitKey(0); cv::imshow("laserLine", laserImage); cv::waitKey(0); cv::imshow("laserLine", laser+treshImage); cv::waitKey(0); cv::destroyAllWindows();*/ /* CEB cv::absdiff(laserOff, laserOn, result); cv::Mat channel[3]; cv::split(result,channel); cv::Mat laserBasic( rows,cols,CV_8U,cv::Scalar(0) ); laserBasic = channel[2]; cv::threshold(laserBasic,laserBasic,20,255,cv::THRESH_OTSU); cv::Mat laserFocus( rows,cols,CV_8U,cv::Scalar(0) ); */ int edges[cols]; //contains the cols index of the detected edges per row for(unsigned int y = 0; y <rows; y++){ //reset the detected edges for(unsigned int j=0; j<cols; j++){ edges[j]=-1; } int j=0; for(unsigned int x = 0; x<cols; x++){ // if(laserBasic.at<uchar>(y,x)>250){ ceb if(diffImage.at<uchar>(y,x)>250){ edges[j]=x; j++; } } //iterate over detected edges, take middle of two edges for(unsigned int j=0; j<cols-1; j+=2){ if(edges[j]>=0 && edges[j+1]>=0 && edges[j+1]-edges[j]<40){ int middle = (int)(edges[j]+edges[j+1])/2; //qDebug() << cols << rows << y << middle; // laserFocus.at<uchar>(y,middle) = 255; CEB laserImage.at<uchar>(y,middle) = 255; } } } // cv::cvtColor(laserFocus, result, CV_GRAY2RGB); //convert back ro rgb //CEB cv::cvtColor(laserImage, result, CV_GRAY2RGB); //convert back ro rgb //CEB return result; }
cv::Mat FSVision::subLaser2(cv::Mat &laserOff, cv::Mat &laserOn) { unsigned int cols = laserOff.cols; unsigned int rows = laserOff.rows; cv::Mat bwLaserOff( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat bwLaserOn( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat tresh2Image( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat diffImage( rows,cols,CV_8U,cv::Scalar(100) ); cv::Mat gaussImage( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat laserImage( rows,cols,CV_8U,cv::Scalar(0) ); cv::Mat result( rows,cols,CV_8UC3,cv::Scalar(0) ); cv::cvtColor(laserOff, bwLaserOff, CV_RGB2GRAY);//convert to grayscale cv::cvtColor(laserOn, bwLaserOn, CV_RGB2GRAY); //convert to grayscale cv::subtract(bwLaserOn,bwLaserOff,diffImage); //subtract both grayscales tresh2Image = diffImage.clone(); /*cv::namedWindow("laserLine"); cv::imshow("laserLine", diffImage); cv::waitKey(0);*/ // Apply the specified morphology operation //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); /*int morph_elem = 0; int morph_size = 1; cv::Mat element = cv::getStructuringElement( morph_elem, cv::Size( 2*morph_size + 1, 2*morph_size+1 ), cv::Point( morph_size, morph_size ) ); cv::morphologyEx(diffImage, diffImage, cv::MORPH_OPEN, element);*/ //cv::imshow("laserLine", diffImage); //cv::waitKey(0); cv::GaussianBlur(diffImage,gaussImage,cv::Size(15,15),12,12); diffImage = diffImage-gaussImage; //cv::imshow("laserLine", diffImage); //cv::waitKey(0); FSFloat threshold = 10; cv::threshold(diffImage,diffImage,threshold,255,cv::THRESH_TOZERO); //apply threshold //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); //cv::equalizeHist(diffImage,diffImage); //cv::imshow("laserLine", FSVision::histogram(diffImage)); //cv::waitKey(0); cv::erode(diffImage,diffImage,cv::Mat(3,3,CV_8U,cv::Scalar(1)) ); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); //cv::Mat element5(3,3,CV_8U,cv::Scalar(1)); //cv::morphologyEx(diffImage,diffImage,cv::MORPH_OPEN,element5); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); cv::Canny(diffImage,diffImage,20,50); //cv::imshow("laserLine", diffImage); //cv::waitKey(0); //cv::imshow("laserLine", treshImage+diffImage); //cv::waitKey(0); //cv::destroyWindow("laserLine"); int edges[cols]; //contains the cols index of the detected edges per row for(unsigned int y = 0; y <rows; y++){ //reset the detected edges for(unsigned int j=0; j<cols; j++){ edges[j]=-1; } int j=0; for(unsigned int x = 0; x<cols; x++){ if(diffImage.at<uchar>(y,x)>250){ edges[j]=x; j++; } } //iterate over detected edges, take middle of two edges for(unsigned int j=0; j<cols-1; j+=2){ if(edges[j]>=0 && edges[j+1]>=0 && edges[j+1]-edges[j]<40){ int middle = (int)(edges[j]+edges[j+1])/2; //qDebug() << cols << rows << y << middle; laserImage.at<uchar>(y,middle) = 255; } } } /*cv::namedWindow("laserLine"); cv::imshow("laserLine", diffImage); cv::waitKey(0); cv::imshow("laserLine", laserImage); cv::waitKey(0); cv::imshow("laserLine", laser+treshImage); cv::waitKey(0); cv::destroyAllWindows();*/ cv::cvtColor(laserImage, result, CV_GRAY2RGB); //convert back ro rgb return result; }