【问题标题】:scatter plot of pixel values at the same locations in the image图像中相同位置的像素值散点图
【发布时间】:2021-02-12 05:23:58
【问题描述】:

假设我们从灰度图像中提取具有特定值的像素,然后在同一图像上使用散点图突出显示这些像素。

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

image = cv2.imread('images/wa_state_highway.jpg')
copy_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray_image = cv2.cvtColor(copy_image, cv2.COLOR_RGB2GRAY)

plt.matshow(gray_image, cmap='gray')

# Get the index of elements with value 12
result = np.where(gray_image == 12)

print('Tuple of arrays returned : ', result, sep='\n')

print('List of coordinates where element with value 0 exists in red channel : ')
# zip the 2 arrays to get the exact coordinates
listOfCoordinates = list(zip(result[0], result[1]))
# iterate over the list of coordinates
# for cord in listOfCoordinates:
#     print(cord)
    
x_val = [x[0] for x in listOfCoordinates]
y_val = [x[1] for x in listOfCoordinates]

plt.scatter(x_val, y_val)
plt.show()

我需要将散点的坐标与图像上对应的坐标进行匹配。


@warped 评论后解决:

【问题讨论】:

  • 试试plt.scatter(y_val, x_val)
  • @warped merci beaucoup,我更新了帖子。
  • @sci9 如果您解决了问题,请发布一个自我回答并接受它,这样您已经解决的问题就会显示为这样。否则,想要帮助的人可能会浪费时间阅读整个问题。 :-/

标签: python image opencv matplotlib image-processing


【解决方案1】:

数字图像通过一对坐标 (x,y) 与正 x 轴向右,y 轴正向下,所以x 指定,y 指定,(0,0) 表示左上角像素。 那么像素对将采用(col, row) 的形式。

另一方面,矩阵的一个条目是使用两个索引编写的,例如 (x,y),其中 x 是 数字, y 是 号码。那么矩阵索引将采用(row, col) 的形式。

假设我们有一个由np.where 返回的元组列表,我们还需要一个步骤来将矩阵索引转换为像素坐标,只需反转每个元组中的元素。

# Create a 5x5 image using just grayscale, numerical values
tiny_image = np.array([[0, 20, 30, 150, 120],
                      [200, 200, 250, 70, 3],
                      [50, 180, 85, 40, 90],
                      [240, 100, 50, 255, 10],
                      [30, 0, 75, 190, 220]])

# To show the pixel grid, use matshow
# plt.matshow(tiny_image, cmap='gray')

# Get the index of elements with value zero (black pixles)
result = np.where(tiny_image == 250);

listOfCoordinates = list(zip(result[0], result[1]))
print('Matrix index: ', listOfCoordinates)
print(tiny_image[1,2])

x_val= [x[0] for x in listOfCoordinates]
y_val = [x[1] for x in listOfCoordinates]

# Reverse each tuple in a list of tuples: https://www.geeksforgeeks.org/python-reverse-each-tuple-in-a-list-of-tuples
print('Pixle Coordinates: ', [tup[::-1] for tup in listOfCoordinates])

plt.matshow(tiny_image, cmap='gray')
plt.scatter(y_val, x_val)
plt.show()


# print(len(result))
# print(result[0].shape)
# print(result[1].shape)
# print(tiny_image.shape)
# print(tiny_image.size)

# print(result[0][0:100])
# print(result[1][0:100])
Matrix index:  [(1, 2)]
250
Pixle Coordinates:  [(2, 1)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2011-07-07
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2013-02-20
    相关资源
    最近更新 更多