1
我開發了一個應用程序,使用OpenCV
,它檢測圓形度並從二進制圖像中獲取質心。我現在要刪除從我的原始圖像中未檢測到的對象。所以我想知道是否有解決方案通過質心從圖像中提取對象。通過質心提取物體
這是我到目前爲止有:
的結果進行分割:
// Read image
cv::Mat im = cv::imread("11002847.bmp", cv::IMREAD_GRAYSCALE);
bitwise_not(im, im);
cv::Mat im2;
im.copyTo(im2);
//Detec the contour to get all the obeject centroid
std::vector<std::vector<cv::Point> > contours;
vector<cv::Vec4i> hierarchy;
findContours(im2, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
vector<cv::Moments> mu(contours.size());
for(int i = 0; i < contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}
ofstream file;
file.open("log.txt");
/// Get the mass centers:
vector<cv::Point2f> mc(contours.size());
for(int i = 0; i < contours.size(); i++)
{
mc[i] = cv::Point2f(mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00);
file << mc[i].x << " "<< mc[i].y << endl;
}
file.close();
//Dtect the circular objects and get there centroid
// Setup SimpleBlobDetector parameters.
cv::SimpleBlobDetector::Params params;
// Change thresholds
//params.minThreshold = 10;
//params.maxThreshold = 200;
// Filter by Area.
params.filterByArea = true;
params.minArea = 1;
// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.5;
// Filter by Convexity
params.filterByConvexity = true;
params.minConvexity = 0.1;
// Filter by Inertia
params.filterByInertia = true;
params.minInertiaRatio = 0.0;
// Storage for blobs
vector<cv::KeyPoint> keypoints;
// Set up detector with params
cv::SimpleBlobDetector detector(params);
// Detect blobs
detector.detect(im, keypoints);
ofstream file2;
file2.open("log2.txt");
for(vector<cv::KeyPoint>::iterator it = keypoints.begin(); it != keypoints.end(); ++it)
{
cv::KeyPoint k = *it;
file2 << k.pt.x << " "<< k.pt.y << endl;
}
file2.close();
// Draw detected blobs as red circles.
// DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
// the size of the circle corresponds to the size of blob
cv::Mat im_with_keypoints;
drawKeypoints(im, keypoints, im_with_keypoints, cv::Scalar(0,0,255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
// Show blobs
cv::imwrite("keypoints.bmp", im_with_keypoints);
任何幫助,請。 –