【问题标题】:Find main line (and its angle) in an image在图像中查找主线(及其角度)
【发布时间】:2019-12-16 12:39:54
【问题描述】:

我想识别图像中的主线及其角度(穿过图像中心的线)。我从一个图像开始(参见链接,https://i.stack.imgur.com/JfoHC.png)我已经使用 ImageJ 进行了预处理(减少噪声、阴影和查找边缘)。任何帮助将不胜感激!

到目前为止我的代码,

import cv2 as cv
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as pltt
from scipy.stats import linregress

# Import image and convert to grayscale
im = cv.imread(https://i.stack.imgur.com/JfoHC.png)
pltt.imshow(im)
pltt.show()
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)

# Convert grayscale image into binary mask
ret, thresh = cv.threshold(imgray, 200, 255, cv.THRESH_OTSU)

# Find contours in binary mask and plot binary mask
_, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
window_name = 'Thresh'
pltt.imshow(thresh)
pltt.show()
cnt = contours[0]
M = cv.moments(thresh)
rows,cols = im.shape[:2]

# Fit line to identified contours in image and plot the results
[vx,vy,x,y] = cv.fitLine(cnt, cv.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
line = cv.line(im,(cols-1,righty),(0,lefty),(0,255,0),2)
window_name = 'Image'
color = (0, 255, 0)
thickness = 9
imagee = cv.line(im, (cols-1,righty),(0,lefty),(0,255,0),2)
cv.imshow(window_name, imagee)
pltt.imshow(imagee)
pltt.show()

【问题讨论】:

  • 请把原图也贴出来。我认为边缘检测可能不是最好的第一步。
  • 你应该调试使用的轮廓(渲染它,看看它,...)。 cnt = contours[0] 可能不是您正在寻找的轮廓。在您的图像中,您可以尝试找到最大的轮廓。或者完全忽略轮廓,尝试检测边缘上的线条,而不是拟合线条。
  • 如果您过滤的轮廓与其他轮廓分离,则线拟合适用于您的情况,否则您的拟合线将是错误的。此外,您正在随机选择索引为 0 的轮廓......请选择最大的轮廓。

标签: python-3.x opencv computer-vision


【解决方案1】:

cnt 0 可能不适合查看,如 cmets 中所述。

cv.drawContours(im, contours, 0, (0,250,0), 3) 看到这一点很有用。

退房 - https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html

如果你画出“正确的”——最大的轮廓,你仍然可能会得到一个奇怪的拟合——但它不再远离事实:

MAX = 0 
for i in range(len(contours)):
    if len(contours[i]) > MAX:
        MAX = len(contours[i])
        MAX_ind = i

cv.drawContours(im, contours, MAX_ind, (100,255,0), 3)
pltt.imshow(im)

【讨论】:

    【解决方案2】:

    使用 Opencv 线段检测器能够测量它的大小。看看 cv2.createLineSegmentDetector()

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 1970-01-01
      • 2015-09-18
      • 2020-10-16
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      相关资源
      最近更新 更多