gpt4 book ai didi

opencv - 如何使用 OpenCV SimpleBlobDetector

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:43 26 4
gpt4 key购买 nike

我如何使用 cv::SimpleBlobDetector 类及其函数 detectblobs() 而不是任何额外的 blob 检测库?

最佳答案

Python:读取图像 blob.jpg 并使用不同的参数执行 Blob 检测。

#!/usr/bin/python

# Standard imports
import cv2
import numpy as np;

# Read image
im = cv2.imread("blob.jpg")

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200


# Filter by Area.
params.filterByArea = True
params.minArea = 1500

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01

# Create a detector with the parameters
# OLD: detector = cv2.SimpleBlobDetector(params)
detector = cv2.SimpleBlobDetector_create(params)


# Detect blobs.
keypoints = detector.detect(im)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob

im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)

C++:读取图像 blob.jpg 并使用不同的参数执行 Blob 检测。

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
// Read image
#if CV_MAJOR_VERSION < 3 // If you are using OpenCV 2
Mat im = imread("blob.jpg", CV_LOAD_IMAGE_GRAYSCALE);
#else
Mat im = imread("blob.jpg", IMREAD_GRAYSCALE);
#endif

// Setup SimpleBlobDetector parameters.
SimpleBlobDetector::Params params;

// Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;

// Filter by Area.
params.filterByArea = true;
params.minArea = 1500;

// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.1;

// Filter by Convexity
params.filterByConvexity = true;
params.minConvexity = 0.87;

// Filter by Inertia
params.filterByInertia = true;
params.minInertiaRatio = 0.01;

// Storage for blobs
std::vector<KeyPoint> keypoints;

#if CV_MAJOR_VERSION < 3 // If you are using OpenCV 2

// Set up detector with params
SimpleBlobDetector detector(params);

// Detect blobs
detector.detect(im, keypoints);
#else

// Set up detector with params
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);

// Detect blobs
detector->detect(im, keypoints);
#endif

// Draw detected blobs as red circles.
// DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
// the size of the circle corresponds to the size of blob

Mat im_with_keypoints;
drawKeypoints(im, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

// Show blobs
imshow("keypoints", im_with_keypoints);
waitKey(0);
}

答案已从tutorial复制而来我写在 LearnOpenCV.com解释 SimpleBlobDetector 的各种参数。您可以在教程中找到有关参数的更多详细信息。

关于opencv - 如何使用 OpenCV SimpleBlobDetector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8076889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com