Version of distanceTransform() that returns labels ignores zeros on image boundary (Bug #1512)


Added by Ose Pedro over 13 years ago. Updated about 13 years ago.


Status:Done Start date:
Priority:High Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:imgproc, video
Target version:2.4.0
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

I've been trying to use distanceTransform() with distanceType=CV_DIST_L2 and maskSize=CV_DIST_MASK_PRECISE in OpenCV 2.2, but I have found that when I use the version that returns labels, it ignores zeros on the boundary of the input image. I have not tried using other distanceTypes or maskSizes, so I don't know if it suffers from the same problem in those cases.

The following code snippet reproduces the problem. It generates a binary image with value 0 on the boundary and value 255 in the interior, calls the two versions of distanceTransform(), and writes the results to image files.

 1const int sz=100;
 2const int szm=sz-1;
 3cv::Mat binary=cv::Mat::ones(sz,sz,CV_8UC1)*255;
 4
 5// Set boundary pixels to zero
 6cv::Mat top(binary,cv::Range(0,1),cv::Range(0,sz));
 7top=cv::Scalar(0);
 8cv::Mat bot(binary,cv::Range(szm,sz),cv::Range(0,sz));
 9bot=cv::Scalar(0);
10cv::Mat left(binary,cv::Range(1,szm),cv::Range(0,1));
11left=cv::Scalar(0);
12cv::Mat right(binary,cv::Range(1,szm),cv::Range(szm,sz));
13right=cv::Scalar(0);
14
15// Calculate distance transform
16cv::Mat distsWithLabels, distsWithoutLabels, labels;
17cv::distanceTransform(binary,distsWithoutLabels,CV_DIST_L2,CV_DIST_MASK_PRECISE);
18cv::distanceTransform(binary,distsWithLabels,labels,CV_DIST_L2,CV_DIST_MASK_PRECISE);
19
20// Write original image
21cv::imwrite("binary.tiff",binary);
22
23// Write distance images
24double minV, maxV, rng;
25cv::minMaxLoc(distsWithLabels,&minV,&maxV);
26rng=maxV-minV;
27cv::Mat dists8u;
28distsWithLabels.convertTo(dists8u,CV_8U,255.0/rng,-255.0*minV/rng);
29cv::imwrite("distsWithLabels.tiff",dists8u);
30
31cv::minMaxLoc(distsWithoutLabels,&minV,&maxV);
32rng=maxV-minV;
33dists8u;
34distsWithoutLabels.convertTo(dists8u,CV_8U,255.0/rng,-255.0*minV/rng);
35cv::imwrite("distsWithoutLabels.tiff",dists8u);

Associated revisions

Revision 3780685f
Added by Alexander Smorkalov almost 13 years ago

CodeReview #1512 notes fixed.

Revision e0f5a2e5
Added by Roman Donchenko over 11 years ago

Merge pull request #1512 from Exocoder:pre-gtk3

History

Updated by Vadim Pisarevsky about 13 years ago

fixed in r7206

  • Status changed from Open to Done
  • (deleted custom field) set to fixed

Updated by Andrey Kamaev about 13 years ago

  • Target version set to 2.4.0
  • Description changed from I've been trying to use distanceTransform() with distanceType=CV_DIST_L2 ... to I've been trying to use @distanceTransform()@ with @distanceType=CV_DIST_... More
  • Category set to imgproc, video

Also available in: Atom PDF