【问题标题】:is it possible to change colored areas into black and white where other areas remain same?是否可以将彩色区域更改为黑色和白色,而其他区域保持不变?
【发布时间】:2020-08-13 01:32:34
【问题描述】:

我使用 opencv 包进行图像处理。在我的图像中,想要将彩色区域反转为二进制颜色,否则不会影响其他像素。

为了在此图像中进行解释,我希望将蓝色区域更改为白色,并且内部文本必须为黑色,而不影响下方区域。

【问题讨论】:

  • 这是在 CSS 中吗?
  • 有没有可能答案总是肯定的......这是否可行可能是一个更好的问题,我怀疑会有一个好的通用解决方案......如果它是的话会很容易顶部总是一个 15 像素的栏或其他东西
  • @RingGamesCompany 我确定不是图片
  • 在 python @RingGamesCompany 中没有。
  • 如果您对处理图像和专门分析颜色感兴趣,首先您应该了解 JPEG 通常在颜色方面很差,因为它们偏爱亮度通道并且通常下采样并丢弃颜色通道的重要方面。因此,您应该尽可能尝试将屏幕抓取保存为 PNG。

标签: python opencv image-processing opencv3.0


【解决方案1】:
import cv2
import numpy as np

img = cv2.imread('A.jpg')
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
w=img.shape[1]
m=np.mean(img, axis=1)
m=m>150
mask_img=np.repeat(m, w)
mask_img.shape=img.shape
a=255-img^(255*mask_img)
cv2.imwrite('A_out.jpg', a)

【讨论】:

    【解决方案2】:

    将其转换为 rgb 值数组

    rgbVals = getRGBValues(im) # no idea how to do this offhand
    for row in range(20): # first 20 px
        for col,px in enumerate(rgbVals[row]):
            rgbVals[row][col] = (255,255,255) if rgbVals[row][col] != (255,255,255) else (0,0,0)
    new_im = rgbVals_to_image(rgbVals)
    

    这里更准确

    >>> from scipy import misc
    >>> a = misc.imread("D:\\Downloads\\pic.jpg") # the image from your post
    >>> #get the white(ish) cells
    ... mask = (a[0:20,:,0] > 160) & (a[0:20,:,1] > 160) & (a[0:20,:,1] > 160)
    >>> a[0:20][mask] = [0,0,0] # make the white cells black
    >>> a[0:20][~mask] = [255,255,255] # make the rest white
    >>> misc.imsave('out.jpg',a)
    

    输出并不完美:/(所有 0:20 表示我们只查看前 20 行像素)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-26
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      相关资源
      最近更新 更多