如何解决检测灰度图像的某些像素
我有这段代码,可以让您检测某个像素值。现在,我正在检测超过某个值的像素(27)。我的想法是仍然检测它们,但要检测另一个像素值(我想将像素从65像素检测到75像素,即另一个像素间隔)。我该怎么办?
您可能会看到,T正在检测灰度图像,所以我对红色,绿色和蓝色具有相同的值。
任何改进此程序以更快地工作的想法都会受到赞赏。例如,使用os.walk引入Daytime文件夹中的所有图像,而我不知道该怎么做。
谢谢。
daytime_images = os.listdir("D:/TR/Daytime/")
number_of_day_images = len(daytime_images)
day_value = 27
def find_RGB_day(clouds,red,green,blue):
img = Image.open(clouds)
img = img.convert('RGB')
pixels_single_photo = []
for x in range(img.size[0]):
for y in range(img.size[1]):
h,s,v,= img.getpixel((x,y))
if h <= red and s <= green and v <= blue:
pixels_single_photo.append((x,y))
return pixels_single_photo
number = 0
for _ in range(number_of_day_images):
world_image = ("D:/TR/Daytime/" + daytime_images[number])
pixels_found = find_RGB_day(world_image,day_value,day_value)
coordinates.append(pixels_found)
number = number+1
解决方法
一些想法:
-
如果您说图像是灰度的,那么您应该将它们作为单通道灰度图像处理,而不是不必要地将其内存占用量增加三倍,并通过将它们提升为三倍来进行比较RGB
-
与其使用在Python中非常慢的嵌套
for
循环,要么使用 Numpy 或 OpenCV 获得10倍至1000倍的加速。类似的示例here。 -
如果要处理的图像很多,它们都是独立的,并且拥有不错的CPU和RAM,请考虑使用多重处理来让所有可爱的内核并行处理图像。简单示例here。
第二个建议最有可能产生最好的股息,所以我将其扩展为:
from PIL import Image
import Numpy as np
# Open an image and ensure greyscale
im = Image.open('image.png').convert('L')
# Make into Numpy array
na = np.array(im)
# Make a new array that is True where pixels between 65..75 and False elsewhere
x = np.logical_and(na>65,na<75)
# Now count the True pixels
result = np.count_nonzero(x)
对于此400x100的图片,该数字为2,200:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。