DistanceTransform not working in Swift/Xcode

asked 2018-11-08 19:31:55 -0500

kenneth gravatar image

updated 2018-11-08 19:34:18 -0500

I am trying to use opencv watershed algorithm with DistanceTransform but when I put the tried to run the code and display the mat image from DistanceTransform it will have me an [Unknown process name] CGImageCreate: invalid image bits/component: 32 bits/pixel 32 alpha info = kCGImageAlphaNone error do anyone know what's missing from my code or am I doing anything wrongly?

The code I am using I have gotten it from docs.opencv.org I did make some changes to it so that it will work with swift

This is the code I tried to use in my .mm file

+(UIImage *) watershed:(UIImage *)image { cv::Mat img; UIImageToMat(image, img, cv::IMREAD_UNCHANGED);

for ( int i = 0; i < img.rows; i++ ) { for ( int j = 0; j < img.cols; j++ ) { if ( img.at<cv::vec3b>(i, j) == cv::Vec3b(255,255,255) ) { img.at<cv::vec3b>(i, j)[0] = 0; img.at<cv::vec3b>(i, j)[1] = 0; img.at<cv::vec3b>(i, j)[2] = 0; } } }

cv::Mat kernel = (cv::Mat_<float>(3,3) << 1, 1, 1, 1, -8, 1, 1, 1, 1);

cv::Mat imgLaplacian; filter2D(img, imgLaplacian, CV_32F, kernel); cv::Mat sharp; img.convertTo(sharp, CV_32F); cv::Mat imgResult = sharp - imgLaplacian;

imgResult.convertTo(imgResult, CV_8UC3); imgLaplacian.convertTo(imgLaplacian, CV_8UC3);

cv::Mat bw; cvtColor(imgResult, bw, cv::COLOR_BGR2GRAY); threshold(bw, bw, 40, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);

cv::Mat dist; distanceTransform(bw, dist, cv::DIST_L2, 3);

normalize(dist, dist, 0, 1.0, cv::NORM_MINMAX);

UIImage *outputImg = MatToUIImage(dist);

return outputImg;

}

edit retag flag offensive close merge delete