python-3.x - 计算图像中的白色像素
问题描述
对python非常陌生。我使用了指南等中的代码来尝试抓取图像中的白色像素但卡住了。可能超级简单,但我选择白色的 if 语句不是在打球。任何人都可以帮忙吗?
#***convert image to no pixels per shade output
import cv2
import numpy as np
from collections import defaultdict
img = cv2.imread('..\\Snapshots\\Me.png')
pixels = img.reshape(-1,3)
counts = defaultdict(int)
for pixel in pixels:
if pixel[0] == pixel[1] == pixel[2]:
counts[pixel[0]] += 1
for pv in sorted(counts.keys()):
print("(%d,%d,%d): %d pixels" % (pv, pv, pv, counts[pv]))
#***count white pixels
from PIL import Image
im = Image.open('..\\snapshots\\Me.png')
white = 0
other = 0
for pixel in im.getdata():
if pixel == (255, 255, 255, 255): # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
white += 1
else:
other += 1
print('white=' + str(white)+', Other='+str(other))
解决方案
白色 rgb(255, 255, 255)
不是(255, 255, 255, 255)
也试试:
countNonZero(pixel == 255)
推荐阅读
- java - BufferedReader 超时
- compiler-errors - 以下 Fortran 代码有什么问题?(gfortran) (1) 处的 DTIO 虚拟参数必须是 ASSUMED SHAPE ARRAY
- java - 如何使用 Java 中的 YouTube API 检查 YouTube 视频是否存在?
- python - 如何在numpy中执行重复填充?
- html - 有没有办法用键盘快捷键砍掉一个 html 标签?
- python - 如何将 my_dict 传递给重定向,例如 redirect('home',my_dict)
- c# - 如何在更新条带连接帐户时使用公司参数
- c - 结构中“(”标记之前的 C 错误预期标识符
- python - Spyder 控制台不显示输出
- javascript - 如何使父组件成为其子组件的标签?