<jupyter_start><jupyter_text>https://realpython.com/python-opencv-color-spaces/<jupyter_code>import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('./images-originals/tl10-168.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
#resize for quicker processing
dim = (224, 224)
img = cv2.resize(img, dim, interpolation=cv2.INTER_LINEAR)<jupyter_output><empty_output><jupyter_text>Visualizing the img in RGB color space (3D scatter plot)<jupyter_code>from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib import colors
r, g, b = cv2.split(img)
fig = plt.figure()
axis = fig.add_subplot(1, 1, 1, projection="3d")
pixel_colors = img.reshape((np.shape(img)[0]*np.shape(img)[1], 3))
norm = colors.Normalize(vmin=-1.,vmax=1.)
norm.autoscale(pixel_colors)
pixel_colors = norm(pixel_colors).tolist()
axis.scatter(r.flatten(), g.flatten(), b.flatten(), facecolors=pixel_colors, marker=".")
axis.set_xlabel("Red")
axis.set_ylabel("Green")
axis.set_zlabel("Blue")
plt.show()<jupyter_output><empty_output><jupyter_text>Visualizing Nemo in HSV Color Space<jupyter_code>hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
plt.imshow(hsv_img)
plt.show()
# To visualize th 3d scatter plot in HSV color space
h, s, v = cv2.split(hsv_img)
fig = plt.figure()
axis = fig.add_subplot(1, 1, 1, projection="3d")

axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors, marker=".")
axis.set_xlabel("Hue")
axis.set_ylabel("Saturation")
axis.set_zlabel("Value")
plt.show()<jupyter_output><empty_output><jupyter_text>Picking Out a Range<jupyter_code>import colorsys
rgb_col = (166, 112, 65)
#rgb_col = (166/255, 112/255, 65/255)
hsv_col = colorsys.rgb_to_hsv(0.2, 0.4, 0.3)
print(hsv_col)
l_square = np.full((10, 10, 3), rgb_col, dtype=np.uint8) *255
d_square = np.full((10, 10, 3), hsv_col, dtype=np.uint8) 
plt.subplot(1, 2, 1)
plt.imshow(hsv_to_rgb(d_square))
plt.subplot(1, 2, 2)
plt.imshow(l_square)
plt.show()
from matplotlib.colors import hsv_to_rgb
light_blob = (0, 0, 0)#(24, 26, 78) #9e672f
dark_blob = (100, 90, 255)
lb_square = np.full((10, 10, 3), light_blob, dtype=np.uint8) / 255
db_square = np.full((10, 10, 3), dark_blob, dtype=np.uint8) / 255
plt.subplot(1, 2, 1)
plt.imshow(hsv_to_rgb(db_square))
plt.subplot(1, 2, 2)
plt.imshow(hsv_to_rgb(lb_square))
plt.show()<jupyter_output><empty_output><jupyter_text>Masking<jupyter_code>mask = cv2.inRange(img, light_blob, dark_blob)
result = cv2.bitwise_and(img, img, mask=mask)
plt.subplot(1, 2, 1)
plt.imshow(mask, cmap="gray")
plt.subplot(1, 2, 2)
plt.imshow(result)
plt.show()
cv2.imwrite("./images-originals/mask.jpg", mask)
cv2.imwrite("./images-originals/result.jpg", result)<jupyter_output><empty_output><jupyter_text>Trackbars to get how cv2.range workshttps://theailearner.com/tag/cv2-inrange-opencv-python/<jupyter_code>import cv2
import numpy as np
 
def nothing(x):
    pass
 
# Open the camera
cap = cv2.VideoCapture(0) 
 
# Create a window
cv2.namedWindow('image')
 
# create trackbars for color change
cv2.createTrackbar('lowH','image',0,179,nothing)
cv2.createTrackbar('highH','image',179,179,nothing)
 
cv2.createTrackbar('lowS','image',0,255,nothing)
cv2.createTrackbar('highS','image',255,255,nothing)
 
cv2.createTrackbar('lowV','image',0,255,nothing)
cv2.createTrackbar('highV','image',255,255,nothing)
 
while(True):
    ret, frame = cap.read()
 
    # get current positions of the trackbars
    ilowH = cv2.getTrackbarPos('lowH', 'image')
    ihighH = cv2.getTrackbarPos('highH', 'image')
    ilowS = cv2.getTrackbarPos('lowS', 'image')
    ihighS = cv2.getTrackbarPos('highS', 'image')
    ilowV = cv2.getTrackbarPos('lowV', 'image')
    ihighV = cv2.getTrackbarPos('highV', 'image')
    
    # convert color to hsv because it is easy to track colors in this color model
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_hsv = np.array([ilowH, ilowS, ilowV])
    higher_hsv = np.array([ihighH, ihighS, ihighV])
    # Apply the cv2.inrange method to create a mask
    mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
    # Apply the mask on the image to extract the original color
    frame = cv2.bitwise_and(frame, frame, mask=mask)
    cv2.imshow('image', frame)
    # Press q to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
cap.release()
cv2.destroyAllWindows()
import cv2
import numpy as np
 
def nothing(x):
    pass
 
# use img instead of cam
m_img = img
 
# Create a window
cv2.namedWindow('image')
 
# create trackbars for color change
cv2.createTrackbar('lowH','image',0,179,nothing)
cv2.createTrackbar('highH','image',179,179,nothing)
 
cv2.createTrackbar('lowS','image',0,255,nothing)
cv2.createTrackbar('highS','image',255,255,nothing)
 
cv2.createTrackbar('lowV','image',0,255,nothing)
cv2.createTrackbar('highV','image',255,255,nothing)
 
while(True):
    #ret, frame = cap.read()
 
    # get current positions of the trackbars
    ilowH = cv2.getTrackbarPos('lowH', 'image')
    ihighH = cv2.getTrackbarPos('highH', 'image')
    ilowS = cv2.getTrackbarPos('lowS', 'image')
    ihighS = cv2.getTrackbarPos('highS', 'image')
    ilowV = cv2.getTrackbarPos('lowV', 'image')
    ihighV = cv2.getTrackbarPos('highV', 'image')
    
    # convert color to hsv because it is easy to track colors in this color model
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    lower_hsv = np.array([ilowH, ilowS, ilowV])
    higher_hsv = np.array([ihighH, ihighS, ihighV])
    # Apply the cv2.inrange method to create a mask
    mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
    # Apply the mask on the image to extract the original color
    m_img = cv2.bitwise_and(m_img, m_img, mask=mask)
    cv2.imshow('image', m_img)
    # Press q to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
cap.release()
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15168635507618558, 'fraction_numerical': 0.04468412942989214, 'mean_word_length': 5.26824034334764, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '234549', 'license': 'CC0-1.0', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2299, 'n_words': 465, 'path': 'Colorspace_for_bnwmixer.ipynb', 'repo_name': 'trucabrac/Blob-process---tests', 'size': '5841'}