0

I'm have been breaking my brain on the code below. I believe I found a rather clean way to tackle the issue: Scan an area (or input picture) for one specific color (afterwards maybe multiple). For each pixel that is not equal to the input color "targetColorPix", the picture is cleaned and set in white.

However, what I've noticed with my output (see below) that there are still areas without the distinct color that are included.

Question: Could someone help me out? Does the np.where function truly check that a value has to be equal to the rgb/bgr code or only a part of it (r individually)? It seems odd that the Windows area are still shown, as the color on my PC is black, not light blue (255,255,0).

[Output image with (in)correct areas highlighted][1]

import cv2
import numpy as np
import pyautogui

function(image, color):

targetColor = cv2.imread(color) # In the case of a color image, it is a 3D ndarray of row (height) x column (width) x color (3). shape is a tuple of (row (height), column (width), color (3)).

targetColorPix = targetColor[1,1] #select one color of input file - 

b,g,r = targetColorPix[:]
targetColorPix = [r, g, b]

im = pyautogui.screenshot()
im.save('inputfile.png') #before

img_rgb = np.array(im)

img_rgb2 = np.where(img_rgb == targetColorPix,img_rgb,255) #if equal to selected color stay, otherwise white

rescaled = (255.0 / img_rgb2.max() * (img_rgb2 - img_rgb2.min())).astype(np.uint8)

im = Image.fromarray(rescaled)
im.save('cleaned.png')
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 19 '22 at 12:56
  • 1
    Please show the original image before processing. – fmw42 Jun 19 '22 at 15:22
  • maybe first use some small image so you could display all values and check them manually – furas Jun 19 '22 at 18:54
  • if image has values `(r,g,b)` but you want to put single value `255` then maybe it works in different way. – furas Jun 19 '22 at 18:57
  • please present a [mre]. your question lacks input data and the code is syntactically defective (indentation is broken). – Christoph Rackwitz Jun 19 '22 at 20:58
  • Thanks all for responding. I've updated the request to make it more clear. I've saved the screenshot as inputfile. In the end, this is a simplified version of the underlying code. However, I still don't understand why the upper part of the Input is seen as the same RGB-code. – ProjectM681 Jun 19 '22 at 21:28
  • problem is because it compares every integer value separatelly - for example image with 3 pixels (and 9 integers) `np.array([[[25,25,25],[0,99,177],[25,25,177]]]) == [0,99,177]` gives result with 9 values `[[[ True True True] [False False False] [ True True False]]]` but you need result with 3 values `[False, True, False]`. Instead of `where` and `img == color` you need `mask = (img != color).any(axis=2)` and `img[ mask ] = (255,255,255)` – furas Jun 20 '22 at 11:41

0 Answers0