OpenCV:iOS中的k-means聚类

时间:2013-01-28 14:20:50

标签: c++ objective-c opencv uiimage k-means

现在我正在尝试在iOS中进行k-means聚类。为了做k-means,我从UIImage转换为cv :: Mat并将函数转换为集群cv :: Mat。该功能不能很好地工作。 结果看起来几乎是好的,但右侧的cols变黑了。我读了openCV参考,我不知道出了什么问题。 代码如下。如果有人帮助我,那将会非常受欢迎。 我的英语不好的借口......

- (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorSpace;

if (cvMat.elemSize() == 1) {
    colorSpace = CGColorSpaceCreateDeviceGray();
} else {
    colorSpace = CGColorSpaceCreateDeviceRGB();
}

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(
    cvMat.cols,                                 //width
    cvMat.rows,                                 //height
    8,                                          //bits per component
    8 * cvMat.elemSize(),                       //bits per pixel
    cvMat.step[0],                              //bytesPerRow
    colorSpace,                                 //colorspace
    kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
    provider,                                   //CGDataProviderRef
    NULL,                                       //decode
    false,                                      //should interpolate
    kCGRenderingIntentDefault                   //intent
);


// Getting UIImage from CGImage
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);

return finalImage;
}

- (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;

cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels

CGContextRef contextRef = CGBitmapContextCreate(
cvMat.data,                 // Pointer to  data
cols,                       // Width of bitmap
rows,                       // Height of bitmap
8,                          // Bits per component
cvMat.step[0],              // Bytes per row
colorSpace,                 // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags

CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

return cvMat;
}

- (cv::Mat)kMeansClustering:(cv::Mat)input
{
cv::Mat samples(input.rows * input.cols, 3, CV_32F);

for( int y = 0; y < input.rows; y++ ){
    for( int x = 0; x < input.cols; x++ ){
        for( int z = 0; z < 3; z++){
            samples.at<float>(y + x*input.rows, z) = input.at<cv::Vec3b>(y,x)[z];
        }
    }
}

int clusterCount = 20;
cv::Mat labels;
int attempts = 5;
cv::Mat centers;
kmeans(samples, clusterCount, labels, cv::TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 100, 0.01), attempts, cv::KMEANS_PP_CENTERS, centers );


cv::Mat new_image( input.rows, input.cols, input.type());


for( int y = 0; y < input.rows; y++ ){
    for( int x = 0; x < input.cols; x++ )
    {
        int cluster_idx = labels.at<int>(y + x*input.rows,0);
        new_image.at<cv::Vec3b>(y,x)[0] = centers.at<float>(cluster_idx, 0);
        new_image.at<cv::Vec3b>(y,x)[1] = centers.at<float>(cluster_idx, 1);
        new_image.at<cv::Vec3b>(y,x)[2] = centers.at<float>(cluster_idx, 2);
    }
}

return new_image;
}

1 个答案:

答案 0 :(得分:1)

您正在为kMeansClustering提供四通道图像,它需要3个通道。尝试丢失Alpha通道。

在函数顶部添加:

cv::cvtColor(input , input , CV_RGBA2RGB);

所以它看起来像这样:

- (cv::Mat)kMeansClustering:(cv::Mat)input
{
    cv::cvtColor(input , input , CV_RGBA2RGB);
    cv::Mat samples(input.rows * input.cols, 3, CV_32F);