Blog.csdn.net/dcrmg/artic…

Blog.csdn.net/zfjBIT/arti…

The distanceTransform method in Opencv is used to calculate the distance between each non-zero point and the nearest zero point in the image. The second Mat matrix parameter DST of the distanceTransform saves the distance information between each point and the nearest zero point. The brighter the point on the image, It represents the further away from zero.

According to the property of distance transformation, after a simple operation, used to refine the outline of the character and find the center of mass of the object (center).

First, refine the outline

#include “core/core.hpp” #include “imgproc/imgproc.hpp” #include “highgui/highgui.hpp” using namespace cv; int main(int argc,char *argv[]) { float maxValue=0; Mat image=imread(argv[1]); Mat imageGray; cvtColor(image,imageGray,CV_RGB2GRAY); imageGray=~imageGray; / / get the GaussianBlur (imageGray imageGray, Size (5, 5), 2); / / filter threshold (imageGray imageGray, 20200, CV_THRESH_BINARY); / / threshold imshow (” s “, imageGray); Mat imageThin(imageGray.size(),CV_32FC1); // Define the distanceTransform(imageGray,imageThin,CV_DIST_L2,3); Mat distShow; distShow=Mat::zeros(imageGray.size(),CV_8UC1); For (int I =0; i

(i,j)>maxValue) { maxValue=imageThin.at

(i,j); }}} for(int I =0; i

> (I, j) maxValue / 1.9) {distShow. At < uchar > (I, j) = 255; }} imshow(“Source Image”, Image); imshow(“Thin Image”,distShow); waitKey(); return 0; }
;>

;>

To refine the letters, the original image:

Refinement effect:

To refine the numbers, the original image:

Refinement effect:

Second, find the center of mass of the object

#include “core/core.hpp” #include “imgproc/imgproc.hpp” #include “highgui/highgui.hpp” using namespace cv; int main(int argc,char *argv[]) { float maxValue=0; // define the maximum Point Pt(0,0); Mat image=imread(argv[1]); Mat imageGray; cvtColor(image,imageGray,CV_RGB2GRAY); imageGray=~imageGray; / / get the GaussianBlur (imageGray imageGray, Size (5, 5), 2); / / filter threshold (imageGray imageGray, 20200, CV_THRESH_BINARY); // Threshold Mat imageThin(imagegry.size (),CV_32FC1); // Define the distanceTransform(imageGray,imageThin,CV_DIST_L2,3); Mat distShow; distShow=Mat::zeros(imageGray.size(),CV_8UC1); For (int I =0; i

(i,j)=imageThin.at

(i,j); if(imageThin.at

(i,j)>maxValue) { maxValue=imageThin.at

(i,j); Pt=Point(j, I); / / coordinates}}} the normalize (distShow distShow, 0255, CV_MINMAX); // For clarity, circle(image,Pt,maxValue,Scalar(0,0,255),3) is normalized; Circle (image, Pt, and 3, Scalar (0255, 0), 3); imshow(“Source Image”,image); imshow(“Thin Image”,distShow); waitKey(); return 0; }



;>

Original image:

After distance transformation, distance Mat matrix DST:

The figure above is normalized from 0 to 255 for clarity. As you can see, the center is brightest, which means that the center is farthest from the zero point, and the farthest can be the center of mass of the object.

Mark center of mass (green dot) :

 

The OpenCV boundingRect and minAreaRect functions are described

cv::Rect exRect = boundingRect(InputArray points)

RotatedRect boundingBox = minAreaRect(InputArray points)