Opencv distanceTransform in a method for a non-zero distance from each of the nearest distance of zero, the second parameter distanceTransform Mat matrix dst holds the closest distance of each point of information, the brighter the image points on the image is calculated with zero, It represents the farther the distance from the zero point.
The nature of this distance can be transformed, through a simple operation, a character and a contour refinement to find the center of mass (center).
First, the refinement profile
#include "Core / core.hpp"
#include "imgproc / imgproc.hpp"
#include "HighGUI / highgui.hpp"
the using namespace CV;
int main (int argc, char * the argv [])
{
a float maxValue = 0; / / define the maximum distance transform matrix
Mat Image imread = (the argv [. 1]);
Mat imageGray;
cvtColor (Image, imageGray, CV_RGB2GRAY);
imageGray = ~ imageGray; // negated
GaussianBlur (imageGray, imageGray, Size ( 5 , 5), 2); // filtering
threshold (imageGray, imageGray, 20,200, CV_THRESH_BINARY); // threshold
imshow ( "S", imageGray);
Mat imageThin (imageGray.size (), CV_32FC1); // save the defined distance Mat conversion result matrix
distanceTransform (imageGray, imageThin, CV_DIST_L2,3) ; // distance transform
Mat distShow;
distShow=Mat::zeros(imageGray.size(),CV_8UC1); //定义细化后的字符轮廓
for(int i=0;i<imageThin.rows;i++)
{
for(int j=0;j<imageThin.cols;j++)
{
if(imageThin.at<float>(i,j)>maxValue)
{
maxValue=imageThin.at<float>(i,j); //获取距离变换的极大值
}
}
}
for(int i=0;i<imageThin.rows;i++)
{
for(int j=0;j<imageThin.cols;j++)
{
if(imageThin.at<float>(i,j)>maxValue/1.9)
{
distShow.at<uchar>(i,j)=255;// meet certain percentage point greater than the conditions of the maximum distance is set to 255 } }
}
imshow("Source Image",image);
imshow("Thin Image",distShow);
waitKey();
return 0;
}
Letters to refine the original image:
Refining Effect:
Digital refinement, the original image:
Refining Effect:
Second, find the center of mass
#include "Core / core.hpp"
#include "imgproc / imgproc.hpp"
#include "HighGUI / highgui.hpp"
the using namespace CV;
int main (int argc, char * the argv [])
{
a float maxValue = 0; / / transformation matrix is defined from the maximum value of
Point of Pt (0,0);
Mat Image imread = (the argv [. 1]);
Mat imageGray;
cvtColor (Image, imageGray, CV_RGB2GRAY);
imageGray = ~ imageGray; // negated
GaussianBlur (imageGray, imageGray, Size (5,5 ), 2); // filtering
threshold (imageGray, imageGray, 20,200, CV_THRESH_BINARY); // thresholding
Mat imageThin (imageGray.size (), CV_32FC1 ); // save the defined distance Mat conversion result matrix
distanceTransform (imageGray, imageThin, CV_DIST_L2,3) ; // distance transform
Mat distShow;
distShow = Mat :: zeros (imageGray.size ( ), CV_8UC1); // define the outline of the character thinning
for (int I = 0; I <imageThin.rows; I ++)
{
for (int J = 0; J < imageThin.cols; J ++)
{
distShow.at <UCHAR> (I, J) = imageThin.at <a float> (I, J);
IF (imageThin.at <a float> (I, J)> maxValue)
{
maxValue = imageThin.at <float> (i, j ); // Get maximum value of the distance transform
Pt = Point (j, i) ; // coordinate
}
}
}
the normalize (distShow, distShow, 0,255, CV_MINMAX); // for clear display, made 0-255 normalized
Circle (Image, of Pt, maxValue, the Scalar (0,0,255),. 3);
Circle (Image, of Pt,. 3, the Scalar (0,255,0),. 3);
imshow ( " Source Image ", image);
imshow("Thin Image",distShow);
waitKey();
return 0;
}
The original image:
After the distance transform matrix from Mat dst:
For clarity the figure, made of 0 to 255 normalized. It can be seen at the center of the brightest been described from a center point farthest from zero, it can be used as the most distant object centroid.
Mark centroid (green points):