我想对 png 图像进行二值化。如果可能的话我想使用枕头。我见过使用两种方法:
image_file = Image.open("convert_image.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
Run Code Online (Sandbox Code Playgroud)
此方法似乎通过抖动图像来处理填充浅色的区域。我不想要这种行为。例如,如果有一个浅黄色圆圈,我希望它变成一个黑色圆圈。
更一般地说,如果像素的 RGB 是 (x,y,z),如果 x<=t OR y<=t OR z<=t 对于某个阈值 0<t<255,我希望像素变黑
我可以将图像转换为灰度或 RGB,然后手动应用阈值测试,但这似乎效率低下。
我见过的第二种方法是这样的:
threshold = 100
im = im2.point(lambda p: p > threshold and 255)
Run Code Online (Sandbox Code Playgroud)
从这里我不知道这是如何工作的,也不知道阈值是什么或在这里做什么以及“and 255”做什么。
我正在寻找如何应用方法 2 的解释或使用 Pillow 的替代方法。
在图像上使用 open CV 中的阈值函数来获取二值图像,通过 Otsu 的阈值处理,我得到的图像由于图像部分的光照条件不同而具有白点
或者使用自适应阈值来修复光照条件,它无法准确地表示 Otsu 实际可以表示的铅笔填充的气泡。
如何获得所代表的填充气泡和没有补丁的固定照明条件?这是原始图像

这是我的代码
#binary image conversion
thresh2 = cv2.adaptiveThreshold(papergray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 21, 13)
thresh = cv2.threshold(papergray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv2.imshow("Binary", thresh) #Otsu's
cv2.imshow("Adpative",thresh2)
Run Code Online (Sandbox Code Playgroud) 我是Python新手,我想读取像jpg、png这样的图像。并将其转换为二值图像。这是我的工作:
from PIL import Image
import numpy
def main( ):
name= 'b.jpg'
img= Image.open (name);
for pixel in iter(img.getdata()):
print(pixel)
img.convert("1").show();
del image;
if __name__=='__main__':
main()
Run Code Online (Sandbox Code Playgroud) 我正在处理二进制图像,之前使用此代码来查找二进制图像中的最大区域:
# Use the hue value to convert to binary
thresh = 20
thresh, thresh_img = cv2.threshold(h, thresh, 255, cv2.THRESH_BINARY)
cv2.imshow('thresh', thresh_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Finding Contours
# Use a copy of the image since findContours alters the image
contours, _ = cv2.findContours(thresh_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#Extract the largest area
c = max(contours, key=cv2.contourArea)
Run Code Online (Sandbox Code Playgroud)
这段代码并没有真正完成我需要它做的事情,现在我认为最好提取二进制图像中最中心的区域。
这就是代码目前正在提取的内容,但我希望能够获得提取的第一个二进制图像中的中心圆。