一种naive的方法:在图像的边缘上做间隔采样。
int main(int argc, char** argv)
{
if (argc < 2)
{
cout << "usage: ./demo input_image" << endl;
return -1;
}
Mat img = imread(argv[1]);
if (img.empty())
{
cout << "error: image is empty" << endl;
}
Mat imgGray, imgBlur, imgEdge;
cvtColor(img, imgGray, CV_BGR2GRAY);
blur(imgGray, imgBlur, Size(3, 3));
double threshold = 10.0;
Canny(imgBlur, imgEdge, threshold, threshold * 3, 3);
vector<Point2f> supportPoints;
uint32_t imgHeight = img.rows;
uint32_t imgWidth = img.cols;
uint32_t sampleInterval = 3;
for (int row = 0; row < imgHeight; row += sampleInterval)
{
for (int col = 0; col < imgWidth; col += sampleInterval)
{
if (imgEdge.at<uchar>(row, col) == 255)
{
supportPoints.push_back(Point2f(col, row));
}
}
}
Mat imgSupportPoints = img.clone();
for (int i = 0; i < supportPoints.size(); i++)
{
circle( imgSupportPoints, supportPoints[i], 1, Scalar(0, 0, 255), FILLED, LINE_8, 0 );
}
imshow("img with support points", imgSupportPoints);
waitKey(0);
}
网友评论