我想确定图像中的主线及其 Angular (横穿图像中心的线)。我从使用ImageJ预处理过的图像开始(请参阅/image/JfoHC.png链接)(减少噪点,阴影和查找边缘)。任何帮助将非常感激!
到目前为止,我的代码
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(/image/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 0可能不是正确的选择。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)
关于python-3.x - 查找图像中的主线(及其角度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59356834/