Mar*_*coM 1 opencv image-processing
OpenCV 中是否有一个开箱即用的improfile函数来沿着一条线从图像中提取强度分布,类似于MATLAB 中的函数?
这里有一个小型的交互式 Python 应用程序来模拟 MATLAB 的improfile.
图像被加载,并显示在 OpenCV 窗口中。记录鼠标按下和按下事件以获取行的开始和结束。线条(白色)显示在图像中,相应的 RGB 强度分布显示在附加的 Matplotlib 窗口中。c在 OpenCV 窗口中按键可以退出无限循环。
这是它的外观:
而且,代码来了:
import cv2
from matplotlib import pyplot as plt
import numpy as np
from skimage import draw
# Actual mouse callback function
def print_coords(event, x, y, flags, param):
# Global variables needed
global image, image_copy, r_start, c_start
# If left mouse button is clicked, start of line
if (event == cv2.EVENT_LBUTTONDOWN):
r_start = x
c_start = y
# If left mouse button is clicked, end of line; plot intensity profile
if (event == cv2.EVENT_LBUTTONUP):
r_end = x
c_end = y
image = cv2.line(image_copy.copy(), (r_start, c_start), (r_end, c_end), (255, 255, 255), 2)
line = np.transpose(np.array(draw.line(r_start, c_start, r_end, c_end)))
data = image_copy.copy()[line[:, 1], line[:, 0], :]
plt.close()
plt.figure('Intensity profile')
plt.plot(data[:, 0], 'b', data[:, 1], 'g', data[:, 2], 'r')
plt.draw()
plt.pause(0.001)
plt.legend(['Blue', 'Green', 'Red'])
plt.ylim((0, 255))
# Read an image
image = cv2.imread('path/to/your/image.png', cv2.IMREAD_COLOR)
image_copy = image.copy()
# Set up window and mouse callback function
cv2.namedWindow("image")
cv2.setMouseCallback("image", print_coords)
# Loop until the 'c' key is pressed
while True:
# Display image; wait for keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# If 'c' key is pressed, break from loop
if key == ord("c"):
break
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
为了获得线的坐标,我使用了linescikit-image 中的函数。这似乎是最快的 Pythonic 方式。
希望能帮助寻找这样一个功能的 Python 人!
| 归档时间: |
|
| 查看次数: |
3432 次 |
| 最近记录: |