3

现在我正在尝试在 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;
}
4

1 回答 1

2

您正在提供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);
于 2013-01-28T16:45:10.147 回答