低对比度图像分割

krz*_*ych 14 c++ opencv image-processing computer-vision

我有低对比度图像分割的问题.任务是找到表面缺陷.它们是可见的(缺陷总是暗区)但图像的对比度非常低.以下两个样本.

1 2

我尝试过增强对比度然后进行阈值处理:

Mat tmp1 = imread("C:\\framesRoi\\311.bmp",0);
stretchContrast(tmp1);
threshold(tmp1,tmp1,75,255,THRESH_BINARY);
Run Code Online (Sandbox Code Playgroud)

拉伸对比度的地方:

int minValue = 255, maxValue = 0;
const int l = sourceImg.cols * sourceImg.rows * sourceImg.channels();
if(sourceImg.isContinuous())
{
    uchar* ptr = sourceImg.ptr<uchar>(0);
    for(int i = 0; i < l; ++i)
    {
        if(ptr[i] < minValue)
        {
            minValue = ptr[i];
        }
        if(ptr[i] > maxValue)
        {
            maxValue = ptr[i];
        }
    }
}
cout<<"min: "<<minValue<<";"<<"max value: "<<maxValue<<endl;

const int  magicThreshold = 10;
if(sourceImg.isContinuous())
{
    uchar* ptr = sourceImg.ptr<uchar>(0);
    for(int i = 0; i < l; ++i)
    {
        ptr[i] = 255 * (ptr[i]-minValue)/(maxValue - minValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

但这种方法失败了.有许多错误检测,并未检测到所有缺陷: 3

这是带测试框的zip:https://dl.dropboxusercontent.com/u/47015140/testFrames.rar

dha*_*hka 8

尝试使用聚类方法(如kmeans)按灰度级对图像进行聚类.下面我直接在图像上使用了kmeans而没有任何灰度级变换(使用3个簇给了我更好的结果).您应该能够通过使用注释中概述的方法对预处理的图像进行聚类来改善结果.

在此输入图像描述 在此输入图像描述

由于kmeans的随机性,簇的形状可能略有不同.

现在,如果您拍摄聚类图像的连通分量并计算这些区域的平均灰度级,则缺陷的平均值应低于其他区域.

我在Matlab中进行了聚类.

im = imread('r2SOV.png');%Uy1Fq r2SOV
gr = im;
size = size(gr);

% perform closing using a 5x5 circular structuring element
sel = strel('disk', 2, 4);
mcl = imclose(gr, sel);
% cluster gray levels using kmeans: using 3 clusters
x = double(mcl(:));
idx = kmeans(x, 3);
cl = reshape(idx, size);

figure, imshow(label2rgb(cl))
Run Code Online (Sandbox Code Playgroud)