[英]Problem in converting a gray scale image back to an rgb image with user defined transformation function in python
[英]Python plot gray scale value vs pixel of an image
我正在尝试使用python实现ImageJ绘图配置文件。 在这里,我将读取图像,然后生成直方图,其中图像像素位于X轴,图像灰度值位于Y轴。
到目前为止,我已经完成了以下工作:
from PIL import Image
import numpy as np
import cv2
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
filepath = "default1.jpg"
img = Image.open(filepath).convert('L')
WIDTH, HEIGHT = img.size
pix = img.load()
data = list(img.getdata()) # convert image data to a list of integers
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
if(x == 1000 and y == 80):
print(pix[x, y]) # output is 145. I don't know what is it
如何实现呢? 任何想法将不胜感激。
PIL图像实际上具有直方图方法( 请参阅文档 ),其输出可以使用matplotlib进行绘制:
from PIL import Image
import matplotlib.pyplot as plt
filepath = "image_histogram.jpg"
img = Image.open(filepath).convert('L')
WIDTH, HEIGHT = img.size
pix = img.load()
data = img.histogram()
fig,ax = plt.subplots()
ax.plot(data)
plt.show()
希望这可以帮助。
编辑 :
在聊天中确定了OP的功能之后,并仔细研究了ImageJ代码,这里是可以重新生成OP提供的绘图的python代码。 但是请注意,这不是直方图。 还要注意,ImageJ实际上可以处理感兴趣的复杂区域(图像的子区域),而我的代码不具备以下功能:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
filepath = "image_histogram.jpg"
img = Image.open(filepath).convert('L')
WIDTH, HEIGHT = img.size
pix = img.load()
data = np.asarray(img.getdata())
data = data.reshape((HEIGHT,WIDTH))
fig,ax = plt.subplots()
reduced_data = data.mean(axis=0)
ax.plot(reduced_data)
plt.show()
结果图像:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.