c++ - 检测卡片上的水滴并计算其数量和大小 OpenCV C++

标签 c++ image opencv image-processing

我正在尝试获取诸如水敏卡上水滴占据的面积之类的信息,其中我必须提取面积、水滴数量,并确定最大水滴和最小水滴。

示例图片:

Example image

到目前为止,我所做的是检测潮湿区域,但我很难检测水滴并测量它们的大小和数量。

按照下面的代码操作,

如果有人可以提供帮助,我将不胜感激!

src = cv::imread("/Users/gustavovisentini/Documents/Developer/Desktop/OpenCV-Teste3.3.1/binary_image.png");

cout << "Loading Image...\n\n";
cvtColor( src, src_gray, COLOR_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
Mat canny_output;
Canny( src_gray, canny_output, thresh, thresh*2 );

vector<vector<Point>> contours;
findContours( src_gray, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );

vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>centers( contours.size() );
vector<float>radius( contours.size() );

for( size_t i = 0; i < contours.size(); i++ )
{
    approxPolyDP( contours[i], contours_poly[i], 3, true );
    boundRect[i] = boundingRect( contours_poly[i] );
    minEnclosingCircle( contours_poly[i], centers[i], radius[i] );
}

Mat drawing = src.clone();

for( size_t i = 0; i< contours.size(); i++ )
{
    Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
    drawContours( drawing, contours_poly, (int)i, color );
    rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2 );
    circle( drawing, centers[i], (int)radius[i], color, 2 );

}
stringstream temp;
temp << "Total: " << contours.size() << " - " << thresh << " - " << contours[1][1];
cv::putText(drawing, temp.str(), cv::Point(10,40), FONT_HERSHEY_PLAIN, 0.7, CV_RGB(255, 0, 0));

imshow( "Contours", drawing );

最佳答案

这是一种使用阈值+轮廓过滤的方法。使用此屏幕截图输入图像:

我们首先将图像转换为灰度,然后使用大津阈值得到二值图像

接下来,我们在二值图像上查找轮廓,迭代每个轮廓,并使用轮廓区域进行过滤。为了确定水滴的总面积,我们保留一个total_area变量并对每个轮廓的面积求和。液滴的数量就是掩模上轮廓线数量的长度。为了确定最小或最大的下降,我们只需根据升序轮廓面积对轮廓进行排序。第一个轮廓将是最小的水滴,最后一个轮廓将是最大的水滴。

这是检测到的液滴、液滴数量和总面积

Drops: 257
Total area: 31448.0

我在 Python 中实现了这种方法,但您可以轻松地将其转换为 C++

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours and filter using contour area
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

total_area = 0
drops = len(cnts)
smallest = sorted(cnts, key=cv2.contourArea)[0]
largest = sorted(cnts, key=cv2.contourArea)[-1]
for c in cnts:
    area = cv2.contourArea(c)
    total_area += area

# Draw largest and smallest drop onto a mask
cv2.drawContours(mask, [largest], -1, (255,255,255), -1)
cv2.drawContours(mask, [smallest], -1, (255,255,255), -1)

# Visualize result better
result = cv2.bitwise_and(image, image, mask=thresh)
result[thresh==0] = (255,255,255)

print('Drops: {}'.format(drops))
print('Total area: {}'.format(total_area))
cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey()

关于c++ - 检测卡片上的水滴并计算其数量和大小 OpenCV C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58941355/

相关文章:

python - 事件形状模型 : matching model points to target points

python - 为什么三角点不能投影回 OpenCV 中的相同图像点?

c++ - 将 char[] 转换为 Qstring 添加额外的 3 个字符

c++ - 尝试使用 uint*& 作为 const 单元 *& 失败 : invalid initialization of reference of type ‘const uint8_t*&’ from expression of type ‘uint8_t*

c# - 获得正确的图像旋转

android - flutter 中的 Glide 等效

html - 图像的 Flexbox 动态大小

c++ 和 openCV 与 windows8 : VideoWriter class not opening

c++ - 重载运算符 * 和 ->

c++ - 非常量元素 initializer_list 类