在OpenCV C ++中绘制一条穿过blob的曲线 [英] Draw a curve line going through the blobs in OpenCV C++

查看:323
本文介绍了在OpenCV C ++中绘制一条穿过blob的曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试画一条穿过给定blob的线。以下是给定的示例

I am trying to draw a line that goes through the given blobs. The following is a given example

我想要一条曲线在水平方向上穿过多个blob,如下所示。

I want a curve line that goes through multiple blobs in horizontal direction as shown below.

推荐答案

就像例子一样:

import cv2
import numpy as np

img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
(_, contours, _) = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# biggest area
target = max(contours, key=lambda x: cv2.contourArea(x))
cv2.drawContours(img, [target], -1, [0, 0, 255], -1) # debug
# just example of fitting
x = target[:, :, 0].flatten()
y = target[:, :, 1].flatten()
poly = np.poly1d(np.polyfit(x, y, 5))
for _x in range(min(x), max(x), 5): # too lazy for line/curve :)
    cv2.circle(img, (_x, int(poly(_x))), 3, [0, 255, 0])

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

这篇关于在OpenCV C ++中绘制一条穿过blob的曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆