is there a way where one can detect rgb value of a certain pixel and use it in an if condition?

  • Last Update :
  • Techknowledgy :

The indices start at 0, so it's:

if pix[0] == 27 and pix[1] == 161 and pix[2] == 226:

or you could make it more explicit by using:

if pix.red == 27 and pix.green == 161 and pix.blue == 226:

And of course, as suggested by @Aditya Santoso:

if pix == (27, 161, 226):

Suggestion : 2

August 6, 2017 at 8:43 pm,August 4, 2017 at 7:11 am,August 8, 2016 at 6:37 pm,August 8, 2016 at 1:29 pm

Open up your favorite editor and create a file named detect_color.py :

#
import the necessary packages
import numpy as np
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image")
args = vars(ap.parse_args())

# load the image
image = cv2.imread(args["image"])

Let’s go ahead and define this list of colors:

# define the list of boundaries
boundaries = [
   ([17, 15, 100], [50, 56, 200]),
   ([86, 31, 4], [220, 88, 50]),
   ([25, 146, 190], [62, 174, 250]),
   ([103, 86, 65], [145, 133, 128])
]

Let’s take a look:

# loop over the boundaries
for (lower, upper) in boundaries:
   # create NumPy arrays from the boundaries
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")

# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)

# show the images
cv2.imshow("images", np.hstack([image, output]))
cv2.waitKey(0)

The most efficient method to check if there are any white pixels in a mask is to use:

if cv2.countNonZero(mask) > 0:
   # do something

If you do:

image = cv2.imread(args["image"])
print(image)
impath = 'C:\\Users\\naresh\\Desktop\\pokemon_games.jpg'
image = cv2.imread(impath)
cv2.imshow('pokemon_games', image)
print(image)

You need two separate calls to cv2.imshow, one for each image that you want to display:

cv2.imshow("Original", image)
cv2.imshow("Result", output)

You can use cv2.countNonZero on the returned mask. For example:

if cv2.countNonZero(mask):
   # do processing here

Suggestion : 3

RGB is a device-dependent color model: different devices detect or reproduce a given RGB value differently, since the color elements (such as phosphors or dyes) and their response to the individual red, green, and blue levels vary from manufacturer to manufacturer, or even in the same device over time. Thus an RGB value does not define the same color across devices without some kind of color management. ,The main purpose of the RGB color model is for the sensing, representation, and display of images in electronic systems, such as televisions and computers, though it has also been used in conventional photography. Before the electronic age, the RGB color model already had a solid theory behind it, based in human perception of colors. ,The RGB color model itself does not define what is meant by red, green, and blue colorimetrically, and so the results of mixing them are not specified as absolute, but relative to the primary colors. When the exact chromaticities of the red, green, and blue primaries are defined, the color model then becomes an absolute color space, such as sRGB or Adobe RGB; see RGB color space for more details. ,where # equals the proportion of red, green, and blue respectively. This syntax can be used after such selectors as "background-color:" or (for text) "color:".

The syntax in CSS is:

rgb(#, #, #)

For example, a color on the DCI-P3 color space can be indicated as :

color(display - p3 # # #)

Suggestion : 4

If the image is a truecolor image, this function returns the RGB value of that pixel as integer. Use bitshifting and masking to access the distinct red, green and blue component values: , Returns the index of the color of the pixel at the specified location in the image specified by image. ,A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().,This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

int(119)
int(123)
int(180)
array(4) {
   ["red"] =>
   int(119)["green"] =>
      int(123)["blue"] =>
      int(180)["alpha"] =>
      int(127)
}

Suggestion : 5

Last Updated : 06 Dec, 2021,GATE CS 2021 Syllabus

true
true
false
false