【发布时间】: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