旋转图像以使特征与OpenCV Python中的Xaxis对齐

2024-03-29 09:38:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的显微图像中,我使用OpenCV中的形态学算子提取了水平白线网格。我无法完全消除噪音,这就是为什么中间有一些白线。网格线需要与x轴平行。在显微镜读数过程中,无法确保完美的平行性。在这种情况下,直线从左向右略微向上移动。 如何使用OpenCV或任何其他Python软件包将线重新对齐到x轴,以便它们与图像的下边缘和上边缘平行

我对OpenCV比较陌生,所以如果有人能告诉我什么操作或函数有助于解决这个问题,我将不胜感激

谢谢

White lines on a black grid


Tags: 图像网格过程水平情况opencv边缘直线
1条回答
网友
1楼 · 发布于 2024-03-29 09:38:14

您可以拟合直线,获得平均角度并旋转图像

建议的解决方案使用以下阶段:

  • 阈值(二值化)图像
  • 对连接线应用闭合形态学操作
  • 找到轮廓线
  • 迭代轮廓并为每个轮廓拟合一条线。
    计算每条线的角度,并建立角度列表
  • 计算“接近中间角”的角度的平均角度
  • 按平均角度旋转图像

代码如下:

import cv2
import numpy as np
import math

img = cv2.imread("input.png", cv2.IMREAD_GRAYSCALE)  # Read input image as grayscale.

threshed = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1]  # threshold (binarize) the image

# Apply closing for connecting the lines
threshed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, np.ones((1, 10)))

# Find contours
contours = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]  # [-2] indexing takes return value before last (due to OpenCV compatibility issues).

img2 = cv2.cvtColor(threshed, cv2.COLOR_GRAY2BGR)  # BGR image - used for drawing

angles = []  # List of line angles.

# Iterate the contours and fit a line for each contour
# Remark: consider ignoring small contours
for c in contours:
    vx, vy, cx, cy = cv2.fitLine(c, cv2.DIST_L2, 0, 0.01, 0.01) # Fit line
    w = img.shape[1]
    cv2.line(img2, (int(cx-vx*w), int(cy-vy*w)), (int(cx+vx*w), int(cy+vy*w)), (0, 255, 0))  # Draw the line for testing
    ang = (180/np.pi)*math.atan2(vy, vx) # Compute the angle of the line.
    angles.append(ang)

angles = np.array(angles)  # Convert angles to NumPy array.

# Remove outliers and 
lo_val, up_val = np.percentile(angles, (40, 60))  # Get the value of lower and upper 40% of all angles (mean of only 10 angles)
mean_ang = np.mean(angles[np.where((angles >= lo_val) & (angles <= up_val))])

print(f'mean_ang = {mean_ang}')  # -0.2424

M = cv2.getRotationMatrix2D((img.shape[1]//2, img.shape[0]//2), mean_ang, 1)  # Get transformation matrix - for rotating by mean_ang

img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]), cv2.INTER_CUBIC) # Rotate the image

# Display results
cv2.imshow('img2', img2)
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

结果:
img2(用于测试):
enter image description here

img(旋转后):
enter image description here


注:

  • 代码只是一个例子——我不希望它能解决所有显微镜图像

相关问题 更多 >