我正在尝试从图像中计算对象。我使用对数照片,并采用一些步骤来获取二进制图像。
这是我的代码:
如您所见,我使用腐蚀和膨胀来得到更好的原木圆形对象。我的问题是,我卡在了计数对象上。我尝试了SimpleBlobDetector,但是什么也没有得到,因为当我尝试将"dilation"步骤的结果转换为CV_8U时,白色对象消失了。当我使用findContours()时,也出现了错误。它说图像通道的一些内容。我不能在这里展示错误,因为那太多步骤了,我已经从我的代码中删除了它。
顺便说一句,在最后,我得到了一个图像的通道。 我可以直接用它来计数吗?还是我必须将其转换,并且最好的方法是什么?
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <features2d.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
//load image
Mat img = imread("kayu.jpg", CV_LOAD_IMAGE_COLOR);
if(img.empty())
return -1;
//namedWindow( "kayu", CV_WINDOW_AUTOSIZE );
imshow("kayu", img);
//convert to b/w
Mat bw;
cvtColor(img, bw, CV_BGR2GRAY);
imshow("bw1", bw);
threshold(bw, bw, 40, 255, CV_THRESH_BINARY);
imshow("bw", bw);
//distance transform & normalisasi
Mat dist;
distanceTransform(bw, dist, CV_DIST_L2, 3);
normalize(dist, dist, 0, 2., NORM_MINMAX);
imshow("dist", dist);
//threshold to draw line
threshold(dist, dist, .5, 1., CV_THRESH_BINARY);
imshow("dist2", dist);
//dist = bw;
//dilasi
Mat dilation, erotion, element;
int dilation_type = MORPH_ELLIPSE;
int dilation_size = 17;
element = getStructuringElement(dilation_type, Size(2*dilation_size + 1, 2*dilation_size+1), Point(dilation_size, dilation_size ));
erode(dist, erotion, element);
int erotionCount = 0;
for(int i=0; i<erotionCount; i++){
erode(erotion, erotion, element);
}
imshow("erotion", erotion);
dilate(erotion, dilation, element);
imshow("dilation", dilation);
waitKey(0);
return 0;
}
如您所见,我使用腐蚀和膨胀来得到更好的原木圆形对象。我的问题是,我卡在了计数对象上。我尝试了SimpleBlobDetector,但是什么也没有得到,因为当我尝试将"dilation"步骤的结果转换为CV_8U时,白色对象消失了。当我使用findContours()时,也出现了错误。它说图像通道的一些内容。我不能在这里展示错误,因为那太多步骤了,我已经从我的代码中删除了它。
顺便说一句,在最后,我得到了一个图像的通道。 我可以直接用它来计数吗?还是我必须将其转换,并且最好的方法是什么?