我正在尝试将文本框与红点对齐,但我不知道从哪里开始。任何建议/例子将不胜感激。
我使用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)
答案 0 :(得分:1)
以下是如何在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 )
运行canny过滤器;这将检测我们的边缘。
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
现在,枚举功能将帮助我们跟踪存储的轮廓数量。将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()