我尝试计算照片中的对象,但无法获得正确的结果。
你可以在l中看到左边的图像有9个对象,在我运行代码之后,我得到了11个对象的正确图片。 我用ncomp引入了MessageBoxW,你可以看到ncomp得到11而不是9。
我搜索了很多,但找不到另一个代码作品给我带来了9的结果。
我希望有人修复此代码我将使用它,或者链接到我可以使用它的代码并获得许多对象的正确结果。
这是我使用的代码:
cv::Mat src = cv::imread("c:\\pic\\test.jpg");
if (!src.data)
return -1;
cv::imshow("src", src);
// Create binary image from source image
cv::Mat bw;
cv::cvtColor(src, bw, CV_BGR2GRAY);
cv::threshold(bw, bw, 40, 255, CV_THRESH_BINARY);
cv::imshow("bw", bw);
// Perform the distance transform algorithm
cv::Mat dist;
cv::distanceTransform(bw, dist, CV_DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
cv::normalize(dist, dist, 0, 1., cv::NORM_MINMAX);
cv::imshow("dist", dist);
// Threshold to obtain the peaks
// This will be the markers for the foreground objects
cv::threshold(dist, dist, .5, 1., CV_THRESH_BINARY);
cv::imshow("dist2", dist);
// Create the CV_8U version of the distance image
// It is needed for cv::findContours()
cv::Mat dist_8u;
dist.convertTo(dist_8u, CV_8U);
// Find total markers
std::vector<std::vector<cv::Point> > contours;
cv::findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
int ncomp = contours.size();
// Create the marker image for the watershed algorithm
cv::Mat markers = cv::Mat::zeros(dist.size(), CV_32SC1);
// Draw the foreground markers
for (int i = 0; i < ncomp; i++)
cv::drawContours(markers, contours, i, cv::Scalar::all(i+1), -1);
// Draw the background marker
cv::circle(markers, cv::Point(5,5), 3, CV_RGB(255,255,255), -1);
cv::imshow("markers", markers*10000);
// Perform the watershed algorithm
cv::watershed(src, markers);
// Generate random colors
std::vector<cv::Vec3b> colors;
for (int i = 0; i < ncomp; i++)
{
int b = cv::theRNG().uniform(0, 255);
int g = cv::theRNG().uniform(0, 255);
int r = cv::theRNG().uniform(0, 255);
colors.push_back(cv::Vec3b((uchar)b, (uchar)g, (uchar)r));
}
// Create the result image
cv::Mat dst = cv::Mat::zeros(markers.size(), CV_8UC3);
// Fill labeled objects with random colors
for (int i = 0; i < markers.rows; i++)
{
for (int j = 0; j < markers.cols; j++)
{
int index = markers.at<int>(i,j);
if (index > 0 && index <= ncomp)
dst.at<cv::Vec3b>(i,j) = colors[index-1];
else
dst.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);
}
}
cv::imshow("dst", dst);
wchar_t buffer[256];
wsprintfW(buffer, L"%d", ncomp);
MessageBoxW(nullptr, buffer, buffer, MB_OK);
cv::waitKey(0);
return 0;
提前致谢
答案 0 :(得分:1)
你的算法计算用分水岭算法标记的对象。你给出的例子是5个对象和5个分水岭标签,所以它工作正常。但是你看到你的图像有9个objesct但是11个分水岭标签。您应该在应用分水岭之前对对象进行计数。实际上,您不需要应用分水岭,因为您的图像非常清晰,可以分割和计算对象。您只能将图像转换为二进制从彩色图像。之后找到并计算轮廓。 代码在下面。如果您不想应用扩张,您可以使用contours_eroded.size()获取对象的数量。这将在侵蚀后为您提供数字。如果您不想应用任何形态您可以将findcontour函数应用于二进制图像,然后您可以获得其轮廓的大小。
int main () {
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Rect bounding_rect;
Mat dst,bin;
Mat src = imread("example.jpg", CV_LOAD_IMAGE_COLOR); // reads image from file
cvtColor(src,dst,CV_BGR2GRAY); // converts image from rgb(src) to gray level (dst)
threshold(dst,bin,40,255,THRESH_BINARY); // Tresholds image with level = 40 from gray level(dst) to binary (bin)
findContours(bin,contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE ); // finds contours on bin image
Scalar color( 255,255,255 );
for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
{
if((contourArea(contours[i],false))>100){ // if counter area >100 pixel draw it on ero which is new image variable
drawContours( bin, contours, i , color, CV_FILLED, 8, hierarchy ); //Draw contours on itself as filled
}
}
findContours( bin, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
{
bounding_rect=boundingRect(contours[i]); //Bound and Draw rectangle each object which detected at the end on src(original image)
rectangle(src, bounding_rect, Scalar(0,255,0),3, 8,0);
}
namedWindow("Binary",CV_WINDOW_NORMAL);
imshow("Binary",src);
cout<<contours.size();
waitKey(0);
return 0;
}