【问题标题】:How to find the location of the rightmost black pixel如何找到最右边黑色像素的位置
【发布时间】:2016-07-18 15:23:42
【问题描述】:

在框架内显示笑脸的黑白图像。

我想要的是找出笑脸最右边的位置。 (在这种情况下,黑色应在图像的“184,91”左右)

我希望通过使用下面列出图像中的颜色,然后看看可以进一步寻找什么。

from PIL import Image
im = Image.open("face.jpg")
print im.convert('RGB').getcolors() # or print im.getcolors()

但是它返回None,我被卡住了。

我怎样才能获得脸部的最正确点?

【问题讨论】:

  • 不是问题的答案,但你知道有更多的黑点是星空边框吗?
  • @tobias_k,感谢您的评论。我在想是否有办法将搜索限制在某个范围内,例如从“40,30”到“200,160”等。
  • 嗯,你可以crop 图片,但是你需要知道笑脸的大小(为了不裁剪太多)来确定笑脸的大小......跨度>
  • 好的。你能分享一种方法来检测图像中最正确的彩色点(将其作为裁剪图像)吗?谢谢。
  • 不幸的是,我不能(否则我会有);我可以重现给出 None 的方法,但恐怕我知道的 PIL 太少,无法提供帮助。

标签: python numpy image-processing python-imaging-library scikit-image


【解决方案1】:

这是我想出的解决方案:

import numpy as np
from skimage import io

img = io.imread('https://i.stack.imgur.com/sbqcu.jpg', as_grey=True)

left, right, top, bottom = 25, 25, 20, 20
crop = img[top: -bottom, left:- right]
threshold = .85
smiley = crop < threshold

rows, cols = np.nonzero(smiley)
rightmost = cols.max()
indices = np.nonzero(cols==rightmost)

for r, c, in zip(rows[indices], cols[indices]):
    print('(%d, %d)' % (r + top, c + left))

上面的代码产生:

(87, 184)
(88, 184)
(89, 184)
(90, 184)
(91, 184)
(92, 184)
(93, 184)
(94, 184)
(95, 184)
(96, 184)
(97, 184)
(98, 184)

这与笑脸的最右边有一条垂直的直线是一致的。

必须仔细选择阈值以避免检测到噪声像素:

threshold = .95
io.imshow(crop < threshold)

【讨论】:

    【解决方案2】:

    作为一种间接的方式,也许我可以: 1.移除框架 2.修剪白色

    剩下的是核心图像本身。如果核心图像是规则图案,图像的维度可以告诉坐标。

    from PIL import Image
    
    img = Image.open("c:\\smile.jpg")
    img2 = img.crop((30, 26, 218, 165))  # remove the frame
    img2.save("c:\\cropped-smile.jpg")
    

    Trim whitespace using PIL 教如何去除圆圈白色部分。

    def trim(im):
        bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
        diff = ImageChops.difference(im, bg)
        diff = ImageChops.add(diff, diff, 2.0, -100)
        bbox = diff.getbbox()
        if bbox:
            return im.crop(bbox)
    
    im = Image.open("c:\\cropped-smile.jpg")
    im = trim(im)
    im.save("c:\\final-smile.jpg")
    

    现在获取核心图像的尺寸。

    im = Image.open("c:\\final-sbqcu.jpg")
    w, h = im.size
    

    我得到的是 131,132。所以 131,66 应该是图像最右边的点。

    【讨论】:

    • 但是这个 131,66 是笑脸最右边的位置,而不是整个图像中最右边的位置。
    猜你喜欢
    • 2012-07-29
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多