综合处理代码,找到凹陷处

libtiff::TIFF *image;
    uint32_t width = 0, height = 0;
    uint16_t ncn = 0;
    uint16_t bitsPer = 0;
    uint16_t *pData;
   if((image = libtiff::TIFFOpen("/Users//Tiff/brake1_height.tif",  "r")) == NULL)
    {
        cout << "not a tiff" << endl;
        exit(1);
    } else {
        cout << "tiff loaded" << endl;
    }
    int nTotalFrames = libtiff::TIFFNumberOfDirectories(image);
    libtiff::TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
    libtiff::TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
    libtiff::TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &ncn);
    libtiff::TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitsPer);
    pData = (uint16_t *)libtiff::_TIFFmalloc(width * height * bitsPer);
    if(pData != NULL)
    {
        
    }
    cout << "tiff width:" << width << endl;
    cout << "tiff length:" << height << endl;
    cout << ncn << endl;
    cout << "bitsPer:" << bitsPer << endl;
    cout << "scanlinesize:" << libtiff::TIFFScanlineSize(image) << endl;
    for(int i = 0; i < height; i++)
    {
        libtiff::TIFFReadScanline(image, pData + i * width, i);
    }
    Mat M(height, width, CV_16UC1, Scalar(0));
    uint16_t minValue = 0, maxValue = 0;
    bool flag = false;
    const uint16_t bottom = 20000;
    
    // 找最大,最小值
    // 赋值原始图像
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            if(pData[j+i*width] > bottom && flag == false)
            {
                flag = true;
                minValue = pData[j + i * width];
            }
            if(pData[j+i*width] < minValue && pData[j+i*width] > bottom)
            {
                minValue = pData[j+i*width];
            }
            if(pData[j+i*width] > maxValue)
            {
                maxValue = pData[j+i*width];
            }
//            if(pData[j + i * width] < 35500 && pData[j + i * width] > 35000)
            M.at(i,j) = pData[j + i * width];
        }
    }
    
    // 按比例筛选
    uint16_t broad = maxValue - minValue;
    uint16_t delta = broad * 60/ 100;
    
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            if(pData[j+i*width] > minValue + delta && pData[j+i*width] < maxValue)
            {
                M.at(i,j) = pData[j + i * width];
            } else {
                M.at(i,j) = 0;
            }
        }
    }
    
    // 从最高值开始去掉一定比例
    uint16_t meltDown = 200;
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            if(M.at(i,j) > maxValue - meltDown && M.at(i,j) < maxValue)
            {
                M.at(i,j) = 0;
            }
        }
    }
    
    // 二值化
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            if(M.at(i,j) > minValue)
            {
                M.at(i,j) = 65535;
            }
        }
    }
    
    // 开操作
    cv::Mat element3(19,19,CV_8U,cv::Scalar(1));
    cv::Mat openedImage;
    cv::Mat opened;
    cv::morphologyEx(M, openedImage, cv::MORPH_OPEN, element3);
    
    // 找连通域
    Mat M8(height, width, CV_8UC1, Scalar(0));
    // 转到8位
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            if(openedImage.at(i,j) == 65535)
            {
                M8.at(i,j) = 255;
            }
        }
    }
    imshow("8bit", M8);

    // 移除小连通域
    std::vector> contours;
    cv::findContours(M8, contours, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
    contours.erase(std::remove_if(contours.begin(), contours.end(),
                                  [](const std::vector& c){return cv::contourArea(c) < 7000; }), contours.end());
    M8.setTo(0);
    cv::drawContours(M8, contours, -1, cv::Scalar(255), cv::FILLED);
    cout << "minv:" << minValue << endl;
    cout << "maxv:" << maxValue << endl;
  
    imshow("removed", M8);
    libtiff::_TIFFfree(pData);
    libtiff::TIFFClose(image);

你可能感兴趣的:(综合处理代码,找到凹陷处)