-3

我正在尝试将文本框与红点对齐,但我不知道从哪里开始。任何建议/示例将不胜感激。

我的形象


我使用 skimage 和 peakutils 来获取梯带和车道的位置,现在我想对它们进行注释

%matplotlib inline
import skimage
import numpy as np
import matplotlib.pyplot as plt 
from skimage import data
from skimage import io
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.util import invert, crop
import peakutils
from peakutils.plot import plot as pplot
import pandas as pd
from scipy.misc import toimage
from skimage import feature

def ladder_peaks(image):
    image = io.imread(image)
    image_grey = rgb2gray(image)
    image_grey = invert(image_grey)
    image_otsu = threshold_otsu(image_grey)
    image_otsu = image_grey > image_otsu
    xi,yi = image_otsu.shape
    width_per_lane=int(xi/10)
    imagecopy_otsu = np.copy(image_otsu)
    imagecopy_otsu = imagecopy_otsu[:,0:(width_per_lane*2)]
    ladder_mean = imagecopy_otsu.mean(1)
    count = 0
    x = []
    for i in ladder_mean:
        x.append(count)
        count+=1
    x = np.asarray(x)
    indexes = peakutils.indexes(ladder_mean, thres=0.4, min_dist=80)
    indexes = indexes.tolist()
    origin = image
    for i in indexes:
        image[i:(i+30),0:30,:] = [255,0,0]
    io.imshow(image)
4

1 回答 1

2

这是如何在opencv中完成此操作的实现。我会尽可能多地解释。

导入必要的库。

import cv2
import numpy as np

现在打开文件供opencv读取

image = cv2.imread('images/red-dots.jpg')

保留原始副本,因为我们将处理第一张图像。

original_image = image

现在将颜色格式从默认 BGR 转换为 RGB。此步骤不是必须的,我鼓励您以 BGR 颜色格式尝试此操作作为练习。

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

现在设置你的上限和下限,以帮助我们只关注红色调。

min_red= np.array([210, 0, 0])
max_red = np.array([255, 33, 33])

inRange 函数将允许我们忽略不在我们限制范围内的所有内容。

image_red = cv2.inRange(image, min_red, max_red )

运行一个精明的过滤器;这将检测我们的边缘。

edged = cv2.Canny(image_red, 50, 200)

现在我们要生成轮廓,注意标志。我们只想要简单的轮廓。

contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

这个函数将找到我们轮廓的质心。cv2.moments 的“魔力”让我们很容易做到这一点。接下来,它将文本放置在轮廓质心的位置周围。

def label_contour_center(image, c):
    # Places some text over the contours
        M = cv2.moments(c)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, "#{}".format(i + 1), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, .3, (255,150,250), 1)
        return image

现在 enumerate 函数将帮助我们跟踪我们存储了多少轮廓。将i其视为计数变量和c包含每个单独轮廓数据的变量。

for i,c in enumerate(contours):
    orig = label_contour_center(original_image, c)

现在显示图像,并在键盘事件时将其销毁。

cv2.imshow('Red dots', original_image)

cv2.waitKey(0)
cv2.destroyAllWindows()
于 2017-06-20T20:27:28.140 回答