Ask Your Question
0

DistanceTransform not working in Swift/Xcode

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

kenneth gravatar image

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

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

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-09-08 19:42:12 -0600

I'm not entirely sure what is happening with this issue, but if you convert dist using gray to BGR, it will show something.

After normalize(dist, dist, 0, 1.0, cv::NORM_MINMAX); add cvtColor(dist, dist, COLOR_GRAY2BGR);

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-11-08 19:31:55 -0600

Seen: 561 times

Last updated: Sep 08 '20