简体   繁体   English

删除低于某个阈值的像素

[英]Removing pixels below a certain threshold

I have a grayscale image with something written in the front and something at the back.我有一个灰度图像,前面写着一些东西,后面写着一些东西。 I'd like to filter out the back part of the letters and only have the front.我想过滤掉字母的后面部分,只有前面。 It's only grayscale and not RGB, and I'd rather not have to calculate pixels manually.它只是灰度而不是 RGB,我宁愿不必手动计算像素。

Is there any library function I can use to do this?我可以使用任何库函数来执行此操作吗? I'm new to python and at the moment, using PIL library, so that's my preference.我是 python 的新手,目前正在使用 PIL 库,所以这是我的偏好。 But if there are other libraries, I'm open to that as well.但如果还有其他图书馆,我也愿意接受。

Here's the image:这是图片: 在此处输入图像描述

Are you looking for a function that automatically strips the background for any given image, or just one that can filter out pixels that meet a certain criteria for this particular image?您是在寻找一种可以自动去除任何给定图像背景的功能,还是可以过滤掉满足该特定图像特定条件的像素的功能?

The eval function applies the same transformation to every pixel in an image. eval 函数对图像中的每个像素应用相同的变换。 This works for your image.这适用于您的形象。

with Image.open("jFmbt.jpg") as im:
    im = im.convert("L")
    out_image = Image.eval(im, lambda x: 256 if x > 175 and x < 250  else x)

A very commonly used library for that would be OpenCV .一个非常常用的库是OpenCV

import cv2 as cv

# 0 flag -> read image as greyscale
img = cv.imread("img.jpg", 0)

# threshold
ret, thresh = cv.threshold(img, 150, 255, cv.THRESH_BINARY)

# result
cv.imwrite("output.jpg", thresh)

The resulting image would be:结果图像将是:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM