Color Pixels Extraction using OpenCV Python | OpenCV Tutorial

In Python OpenCV Tutorial, Explained How to Extraction or Detect Pixels Color using OpenCV Python

Get the answers of below questions:

  1. How do you find the color of the pixel of an image?
  2. How do you find the color of an image in Python?
  3. How do I extract color features from an image?
  4. How do we find items of a specific color?
  5. How can I identify a color in an image?

Color Pixels Extraction

How to Detect Road Marking Using OpenCV

# Show an Image
import cv2
import numpy as np

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road1.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

cv2.imshow("Road Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Conver image in gray scale

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Gray Image", gray_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Road Margin Detection using Gray Image

gray_img_copy = np.copy(gray_img)

gray_img_copy[gray_img_copy[:, :] < 200]=0

cv2.imshow("Gray Image", gray_img_copy)

cv2.waitKey(0)
cv2.destroyAllWindows()
gray_img ## 0 - 255 ## 0=Black, 255= White

gray_img_copy[:, :] < 200

gray_img_copy

gray_img_copy[:, 300]

Road Margin Detection using Color Image

img_copy = np.copy(img)

print("img_copy.shape: ", img_copy.shape)

img_copy[(img_copy[:, :, 0] < 200) | (img_copy[:, :, 1] < 200) | (img_copy[:, :, 2] < 200)] = 0

cv2.imshow("Color Image", img_copy)

cv2.waitKey(0)
cv2.destroyAllWindows()

Sign Board Detection

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road5.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 50) | (img_copy[:,:,1] < 150) | (img_copy[:, :, 2] < 150) ]=0

img_2 = np.hstack((cv2.resize(img, (650, 500)), cv2.resize(img_copy, (650, 500))))
cv2.imshow("Yellow Road Image", img_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

Red Color Pixels Extraction

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road6.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 50) | (img_copy[:,:,1] > 50) | (img_copy[:, :, 2] < 90) ]=0

img_2 = np.hstack(( cv2.resize(img, (650, 500)), cv2.resize(img_copy, (650, 500)) ))
cv2.imshow("Color Image VS Color Extracted Image", img_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

Yellow Color Pixels Extraction

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road4.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 50) | (img_copy[:,:,1] < 150) | (img_copy[:, :, 2] < 150) ]=0
cv2.imshow("Yellow Road Image", img_copy)

cv2.waitKey(0)
cv2.destroyAllWindows()

Human Detection

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\sunset.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 90) | (img_copy[:,:,1] > 90) | (img_copy[:, :, 2] > 90) ]=255

img_2 = np.hstack((cv2.resize(img, (650, 500)), cv2.resize(img_copy, (650, 500))))
cv2.imshow("Yellow Road Image", img_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

Leave a Reply

Top