python-3.x - 查找图像中的主线(及其角度)

标签 python-3.x opencv computer-vision

我想确定图像中的主线及其 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()

Result from image pre-processing and edge detection using ImageJ
Result from binary threshold using Python 3.7 (OpenCV)
Result from my code above: Line fitted to contours

最佳答案

如注释中所述,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)

result

关于python-3.x - 查找图像中的主线(及其角度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59356834/

相关文章:

machine-learning - 图像识别和唯一性检测

python - Vigenère Cipher 函数实现

python-3.x - 在进程之间共享 NetworkX 图,无需额外内存成本(只读)

java - Android Studio OpenCV 示例

c++ - opencv/c++ 中适当的阈值函数

python - 如何在交通车辆计数器中每辆车只计数一次?

Python:无法从 hashlib 导入 scrypt

python - 有人可以解释一下这里提到的策略元类的目的吗

java - 使用 jframe 在 mat 中显示图像(OpenCV 3.00)

python - OpenCV 中的图像转换