[英]How can I make the pixels of each intensity value in an individual list in python?
I need to make the pixels with the same intensity value in a list so I will get a list of lists of intensity values and each list has the pixels with the same intensity value.我需要在列表中制作具有相同强度值的像素,因此我将获得强度值列表的列表,并且每个列表都有具有相同强度值的像素。
import nibabel as nib
import numpy as np
from PIL import Image
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import asarray
from scipy import ndimage, misc, stats
import cv2
### Load the image
img = nib.load('input_dir/FLAIR1.nii.gz')
img_data = img.get_fdata()
imgs = img_data[:, :, 23]
rows, col = imgs.shape
my_nlist = [[] for k in range(y.shape[0])]
print(im_arr[2,2])
for i in range(0, rows):
for j in range(0, col):
pixel = imgs[i, j]
for k in range(y.shape[0]):
if pixel == [k]:
my_nlist[k].append(pixel)
Don't make it harder on yourself than you have to:) this is exactly what a histogram is meant for!不要让自己变得更难:)这正是直方图的意义所在!
import matplotlib.pyplot as plt
import cv2 as cv # You won't be needing cv2, it is just needed to create this example
img = cv.imread('dir/to/img', 0) # notice the grayscale flag
hist, bins, _ = plt.hist(img) # hist contains the list of pixels with the same intensity for each intensity in the picture
plt.show() # in case you want to visualize it
This is what it will look like:这就是它的样子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.