如何划分轮廓?

2024-05-23 18:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检测图像中的物体并测量它们之间的距离。只要物体不是靠得太近就行了。不幸的是,图像的光照不是最佳的,所以看起来像是物体在触摸,尽管它们不是。我试图用一条代表物体的线来确定距离。问题是,一旦物体轮廓连接起来,我就无法确定代表物体的直线,因此无法计算距离。在

输入图像:

enter image description here

代码:

import cv2
import numpy as np

#import image
img = cv2.imread('img.png', 0)

#Thresh
_, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)

#Finding the contours in the image
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#Convert img to RGB and draw contour
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
cv2.drawContours(img, contours, -1, (0,0,255), 2)

#Object1
v = np.matrix([[0], [1]])
rect = cv2.minAreaRect(contours[0])

#determine angle
if rect[1][0] > rect[1][1]:
    ang = (rect[2] + 90)* np.pi / 180
else:
    ang = rect[2]* np.pi / 180
rot = np.matrix([[np.cos(ang), -np.sin(ang)],[np.sin(ang), np.cos(ang)]])
rv = rot*v

#draw angle line
lineSize = max(rect[1])*0.45                #length of line
p1 = tuple(np.array(rect[0] - lineSize*rv.T)[0].astype(int))
p2 = tuple(np.array(rect[0] + lineSize*rv.T)[0].astype(int))
cv2.line(img, p1, p2, (255,0,0), 2)

#Object2
if len(contours) > 1:
    rect = cv2.minAreaRect(contours[1])

    #determine angle
    if rect[1][0] > rect[1][1]:
        ang = (rect[2] + 90)* np.pi / 180
    else:
        ang = rect[2]* np.pi / 180
    rot = np.matrix([[np.cos(ang), -np.sin(ang)],[np.sin(ang), np.cos(ang)]])
    rv = rot*v

    #draw angle line
    lineSize = max(rect[1])*0.45                #length of line
    p1 = tuple(np.array(rect[0] - lineSize*rv.T)[0].astype(int))
    p2 = tuple(np.array(rect[0] + lineSize*rv.T)[0].astype(int))
    cv2.line(img, p1, p2, (255,0,0), 2)


#save output img
cv2.imwrite('output_img.png', img)

输出图像:

enter image description here

这很好,但只要我使用一个带有连接轮廓的图像,就会发生这种情况:

enter image description hereenter image description here

有没有一种方法来划分轮廓或是一个变通方法?在

编辑

多亏了B.M.的建议,我试过是否可以解决侵蚀问题,但不幸的是,新的问题出现了。似乎不可能在侵蚀和阈值/轮廓之间找到平衡。在

示例:

enter image description hereenter image description hereenter image description hereenter image description here


Tags: rect图像imgnplinepicv2物体
3条回答

您可以使用腐蚀技术,如^{}提供的。之后

from cv2 import erode
import numpy as np    
kernel = np.ones((5,25),dtype=np.uint8) # this must be tuned 

im1=erode(im0,kernel)

您将获得一张图像(im0是您的第二张图像),其中明亮区域缩小:

enter image description here

现在你可以测量一段距离,即使必须考虑到侵蚀的影响。在

如果你先搜索轮廓,然后检查是否真的有两个呢。如果只有一个,你可以做一个循环来腐蚀和搜索腐蚀图像上的轮廓,直到你得到两个轮廓。当事件发生时,制作一个黑色的边界框,它比被腐蚀图像上使用的内核数量大,并在“原始图像”上绘制,该图像将物理上分割并创建2个轮廓。然后将代码应用于生成的图像。也许你可以在处理之前上传你最难处理的图片?希望它能给你一点帮助或者给你一个新的想法。干杯!在

示例代码:

import cv2
import numpy as np

img = cv2.imread('cont.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
_, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
k = 2

if len(contours)==1:
    for i in range (0,1000):
        kernel = np.ones((1,k),np.uint8)
        erosion = cv2.erode(threshold,kernel,iterations = 1)
        _, contours, hierarchy = cv2.findContours(erosion,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        if len(contours) == 1:
            k+=1
        if len(contours) == 2:
            break
        if len(contours) > 2:
            print('more than one contour')

x,y,w,h = cv2.boundingRect(contours[0])
cv2.rectangle(threshold,(x-k,y-k),(x+w+k,y+h+k), 0, 1)
_, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img, contours, -1, (0,0,255), 2)

#Object1
v = np.matrix([[0], [1]])
rect = cv2.minAreaRect(contours[0])

#determine angle
if rect[1][0] > rect[1][1]:
    ang = (rect[2] + 90)* np.pi / 180
else:
    ang = rect[2]* np.pi / 180
rot = np.matrix([[np.cos(ang), -np.sin(ang)],[np.sin(ang), np.cos(ang)]])
rv = rot*v

#draw angle line
lineSize = max(rect[1])*0.45                #length of line
p1 = tuple(np.array(rect[0] - lineSize*rv.T)[0].astype(int))
p2 = tuple(np.array(rect[0] + lineSize*rv.T)[0].astype(int))
cv2.line(img, p1, p2, (255,0,0), 2)

#Object2
if len(contours) > 1:
    rect = cv2.minAreaRect(contours[1])

    #determine angle
    if rect[1][0] > rect[1][1]:
        ang = (rect[2] + 90)* np.pi / 180
    else:
        ang = rect[2]* np.pi / 180
    rot = np.matrix([[np.cos(ang), -np.sin(ang)],[np.sin(ang), np.cos(ang)]])
    rv = rot*v

    #draw angle line
    lineSize = max(rect[1])*0.45                #length of line
    p1 = tuple(np.array(rect[0] - lineSize*rv.T)[0].astype(int))
    p2 = tuple(np.array(rect[0] + lineSize*rv.T)[0].astype(int))
    cv2.line(img, p1, p2, (255,0,0), 2)


#save output img

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

enter image description here

我认为你可以采用分水岭分割法(我建议采用ITK)。 组合:

  • 距离图计算
  • 扩张重建
  • 区域最大值
  • 基于标记的形态分水岭 将导致以下分离: separated

一旦分离完成,您可以:

  • 提取轮廓点
  • 计算相关多边形近似
  • 计算两个粒子之间的精确距离 (几何部分可以使用boost几何库,但这是一个c++库)

您还可以使用纯几何/形态学方法:

  • 提取轮廓点
  • 计算相关多边形近似 polygonal approximation
  • 计算相关的voronoi图(可将其视为形状的骨架) voronoi diagram
  • 计算与前一骨架的每个分支关联的厚度
  • 在最小厚度截面切割 image cuts

谨致问候

相关问题 更多 >