绘制一副图片沿着一条横线,RGB通道值得变化规律

沿着这条横线,R通道值变化折线图
检测结果:
在这里插入图片描述在这里插入图片描述
上述图片相关代码:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# 读取图像
img = cv2.imread('detect2.jpg')

# 指定横线的y坐标
y = 285  # 可以自行更改

# 获取图像的R通道数值
r_channel = img[y,:,2]  # 注意,OpenCV用BGR格式读取图像,所以R通道的顺序为2

# 在图像上绘制选定的横线
img_with_line = cv2.line(img.copy(), (0, y), (img.shape[1] - 1, y), (0, 255, 0), 1)  # 绘制绿色横线

# 创建一个和R通道相同长度的数组作为x坐标
x = np.arange(len(r_channel))

# 创建两个子图:第一个展示带有标记线的原图,第二个展示对应的R通道数值
fig, ax = plt.subplots(2, 1, figsize=(10,10))

# 显示带有横线的原图
ax[0].imshow(cv2.cvtColor(img_with_line, cv2.COLOR_BGR2RGB))
ax[0].set_title('Original Image with Specified Line')

# 显示R通道的数值
ax[1].plot(x, r_channel, color='red')
ax[1].set_xlabel('Pixel Position Along the Line')
ax[1].set_ylabel('Red Channel Value')
ax[1].set_title('R Channel Value Along the Specified Line')

plt.tight_layout()
plt.show()

以同样的方法,绘制GB通道的折线图
在这里插入图片描述
在这里插入图片描述
上图相关代码:

# Import necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np

# Load image
img = cv2.imread('detect2.jpg')

# Specify y-coordinate for the line
y = 285

# Get the R, G and B channel values along the line
r_channel = img[y,:,2]
g_channel = img[y,:,1]
b_channel = img[y,:,0]

# Draw the specified line on a copy of the image
img_with_line = cv2.line(img.copy(), (0, y), (img.shape[1] - 1, y), (0, 255, 0), 1)

# Create an x-coordinate array of the same length as the channel data
x = np.arange(len(r_channel))

# Create the figure and axis for original image
fig1, ax1 = plt.subplots(figsize=(6,6))
# Plot the image with the specified line
ax1.imshow(cv2.cvtColor(img_with_line, cv2.COLOR_BGR2RGB))
ax1.set_title('Original Image with Specified Line')
plt.show()

# Create the figure and axes for the channel plots
fig2, ax2 = plt.subplots(3, 1, figsize=(6,10))

# Plot R channel value along the line
ax2[0].plot(x, r_channel, color='red')
ax2[0].set_xlabel('Pixel Position Along the Line')
ax2[0].set_ylabel('Red Channel Value')
ax2[0].set_title('R Channel Value Along the Specified Line')

# Plot G channel value along the line
ax2[1].plot(x, g_channel, color='green')
ax2[1].set_xlabel('Pixel Position Along the Line')
ax2[1].set_ylabel('Green Channel Value')
ax2[1].set_title('G Channel Value Along the Specified Line')

# Plot B channel value along the line
ax2[2].plot(x, b_channel, color='blue')
ax2[2].set_xlabel('Pixel Position Along the Line')
ax2[2].set_ylabel('Blue Channel Value')
ax2[2].set_title('B Channel Value Along the Specified Line')

# Set layout to tight and show the figure
plt.tight_layout()
plt.show()

注释:可以通过修改:fig1, ax1 = plt.subplots(figsize=(6,6)),调整输出图的大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值