Ask Your Question

Revision history [back]

Opencv multiple bubble detection in a image

Hey there, I writing a code for bubble detection, This is the code i'm using as of now.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/features2d.hpp>

void main()
{
    cv::Mat src = cv::imread("e:/template/bubble.jpg");
    cv::Mat matX; cv::Mat matY; cv::Mat gray;
    cv::blur(src, src, Size(5, 5));
    cv::cvtColor(src, gray, COLOR_BGR2GRAY);
    cv::Sobel(gray, matX, CV_16SC1, 1, 0);
    cv::Sobel(gray, matY, CV_16SC1, 0, 1);
    cv::Mat  matXY = abs(matX) + abs(matY);
    matXY.convertTo(matXY, CV_8UC1);
    cv::Mat bin;
    cv::threshold(matXY, bin, 100, 255, cv::THRESH_BINARY);
    cv::waitKey();
}

source image

image description

The Output of bin is as follows: image description

By using the sobel method, it can help to find some bubbles, but also lost a lot of especially small bubbles. I have tried contour recognition and other methods, but I can’t get good results from this picture. I hope I can get some pointers from everyone.

Opencv multiple bubble detection in a image

Hey there, I writing a code for bubble detection, This is the code i'm using as of now.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/features2d.hpp>

void main()
{
 cv::Mat src = cv::imread("e:/template/bubble.jpg");
    cv::imread("e:/template/气泡.jpg");
cv::Mat matX; cv::Mat matY; cv::Mat gray;
    gray; cv::Mat houghImage;
cv::blur(src, src, Size(5, 5));
 cv::cvtColor(src, gray, COLOR_BGR2GRAY);
 cv::Sobel(gray, matX, CV_16SC1, 1, 0);
 cv::Sobel(gray, matY, CV_16SC1, 0, 1);
 cv::Mat  matXY = abs(matX) + abs(matY);
 matXY.convertTo(matXY, CV_8UC1);
 cv::Mat bin;
 cv::threshold(matXY, bin, 100, 255, cv::THRESH_BINARY);
 
GaussianBlur(gray, gray, Size(9, 9), 2, 2);
houghImage = src.clone();
vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 2, gray.rows / 4, 200, 100);
for (size_t i = 0; i < circles.size(); i++)
{
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    circle(houghImage, center, 3, Scalar(0, 255, 0), -1, 8, 0);
    circle(houghImage, center, radius, Scalar(0, 0, 255), 3, 8, 0);
}
cv::waitKey();
}

source image

image description

The Output of bin is as follows: image description

By using the sobel method, it can help to find some bubbles, but also bubbles. By Using HoughCircles I cannot find any cirlce. lost a lot of especially small bubbles. I have tried contour recognition and other methods, but methods,but I can’t get good results from this picture. I hope I can get some pointers from everyone.