ham*_*474 6 python opencv python-3.x
我刚刚开始使用 OpenCV,我发现了 contourArea 函数的一些非常奇怪的行为。
看到这张图片。
它有三个不相连的区域,左侧是一组长笔划,顶部中心有一个点,最后是右侧的一个大方块。
当我运行我的函数时,我得到了这个结果
Contour[0] Area: 221, Length: 70, Colour: Red
Contour[1] Area: 13772, Length: 480, Colour: Green
Contour[2] Area: 150, Length: 2370, Colour: Blue
Run Code Online (Sandbox Code Playgroud)
虽然我还没有实际计算左侧部分的面积,但它似乎包含超过 150 个像素并且肯定比顶部中心的点具有更高的值,我会说该点应该能够适应左侧部分至少 10 次。广场的面积确实有效。
Square Area
width = 118
height = 116
118 * 116 = 13,688
Run Code Online (Sandbox Code Playgroud)
13,688 与 opencv 给出的面积(13,772)非常接近,差异可能代表我的测量误差。我手动计算了点的面积
Dot Area
width = 27
height = 6
27*6 = 162
Run Code Online (Sandbox Code Playgroud)
与 opencv 所说的相差不远 (221)
从contourArea 上的OpenCV 文档页面读取它说它会为具有自相交的轮廓提供错误的结果。不太明白什么是自相交,我做了一个测试图像。
如您所见,左侧有一个矩形,中间有一个十字,另一个旋转了 45 度的十字。由于中心重叠,我希望十字架的面积略小于矩形面积的两倍。
Contour[0] Area: 1805, Length: 423, Colour: Red
Contour[1] Area: 947, Length: 227, Colour: Green
Contour[2] Area: 1825, Length: 415, Colour: Blue
Run Code Online (Sandbox Code Playgroud)
如您所见,两个十字架的面积略小于矩形面积的两倍。正如预期的那样。
我对捕获正方形的内部或在左侧的形状和点周围绘制一个框不感兴趣(尽管它会切向有趣)这不是我在这个问题中特别要问的。
所以我的问题是:为什么我的不规则形状的面积被严重低估了?
我从本教程中复制了大部分代码
我已经将我的代码简化为下面这个自包含示例。
def contour_test(name):
import cv2 as cv
colours = [{'name': 'Red ', 'bgr': (0, 0, 255)},
{'name': 'Green ', 'bgr': (0, 255, 0)},
{'name': 'Blue ', 'bgr': (255, 0, 0)}]
src = cv.imread(cv.samples.findFile(name))
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
src_gray = cv.blur(src_gray, (3,3))
threshold = 100
canny_output = cv.Canny(src_gray, threshold, threshold * 2)
contours, _ = cv.findContours(canny_output, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Get the moments
mu = [None for i in contours]
for i in range(len(contours)):
mu[i] = cv.moments(contours[i])
# Get the mass centers
mc = [None for i in contours]
for i in range(len(contours)):
mc[i] = (mu[i]['m10'] / (mu[i]['m00'] + 1e-5), mu[i]['m01'] / (mu[i]['m00'] + 1e-5))
# Draw contours
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
for i, j in enumerate(contours):
colour = colours[i]['bgr']
cv.drawContours(drawing, contours, i, colour, 2)
area = int(cv.contourArea(contours[i]))
length = int(cv.arcLength(contours[i], True))
print('Contour[{0}] Area: {1}, Length: {2}, Colour: {3}'.format(i, area, length, colours[i]['name']))
Run Code Online (Sandbox Code Playgroud)
发现的轮廓的内部findContours
应该填充有白色。
cv.Canny
之前不要使用findContours
(cv.blur
也不是必需的)。 cv.threshold
。
建议添加自动阈值选项。 cv.THRESH_BINARY_INV
cv.THRESH_OTSU
您可以将cv.blur
andcv.Canny
和替换cv.findContours(canny_output...
为:
_, src_thresh = cv.threshold(src_gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
contours, _ = cv.findContours(src_thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
结果(上图):
Contour[0] Area: 13531, Length: 476, Colour: Red
Contour[1] Area: 184, Length: 71, Colour: Green
Contour[2] Area: 4321, Length: 1202, Colour: Blue
Run Code Online (Sandbox Code Playgroud)
这是完整的(更新的)代码:
import numpy as np
def contour_test(name):
import cv2 as cv
colours = [{'name': 'Red ', 'bgr': (0, 0, 255)},
{'name': 'Green ', 'bgr': (0, 255, 0)},
{'name': 'Blue ', 'bgr': (255, 0, 0)}]
src = cv.imread(cv.samples.findFile(name))
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
#src_gray = cv.blur(src_gray, (3,3))
#threshold = 100
#canny_output = cv.Canny(src_gray, threshold, threshold * 2)
#contours, _ = cv.findContours(canny_output, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
_, src_thresh = cv.threshold(src_gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
cv.imshow('src_thresh', src_thresh);cv.waitKey(0);cv.destroyAllWindows() # Show src_thresh for testing
contours, _ = cv.findContours(src_thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Get the moments
mu = [None for i in contours]
for i in range(len(contours)):
mu[i] = cv.moments(contours[i])
# Get the mass centers
mc = [None for i in contours]
for i in range(len(contours)):
mc[i] = (mu[i]['m10'] / (mu[i]['m00'] + 1e-5), mu[i]['m01'] / (mu[i]['m00'] + 1e-5))
# Draw contours
drawing = np.zeros((src_thresh.shape[0], src_thresh.shape[1], 3), dtype=np.uint8)
for i, j in enumerate(contours):
colour = colours[i]['bgr']
cv.drawContours(drawing, contours, i, colour, 2)
area = int(cv.contourArea(contours[i]))
length = int(cv.arcLength(contours[i], True))
print('Contour[{0}] Area: {1}, Length: {2}, Colour: {3}'.format(i, area, length, colours[i]['name']))
cv.imshow('drawing', drawing);cv.waitKey(0);cv.destroyAllWindows() # Show drawing for testing
contour_test('img.jpg')
Run Code Online (Sandbox Code Playgroud)
我cv.imshow
在两个地方添加了测试。
归档时间: |
|
查看次数: |
721 次 |
最近记录: |