Image processing on the drawcontours area

I’d like to convert only the drawcontours area to RGB images, then convert it to HSV again in order to update lower and upper values every frame over time.
Note: I’d like to avoid using the ROI of the rectangle area because of drawcontours is the actual area.

Full my code:

import cv2
import numpy as np
import time

cap = cv2.VideoCapture(0)
width = cap.get(3)  # float
height = cap.get(4)  # float
time.sleep(2.0)
while (1):
    _, img = cap.read()
    clone1 = img.copy()
    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue

    black_lower = np.array([0, 0, 0], np.uint8)
    black_upper = np.array([180, 255, 30], np.uint8)
    black = cv2.inRange(hsv, black_lower, black_upper)
    # Morphological Transform, Dilation

    kernal = np.ones((5, 5), "uint8")
    black = cv2.dilate(black, kernal)
    res_black = cv2.bitwise_and(img, img, mask=black)
    (_, contours, hierarchy) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:1]  # get largest  contour area
    for pic, contour in enumerate(cnts):
        area = cv2.contourArea(contour)
        if (area > 300):
            x, y, w, h = cv2.boundingRect(contour)


            img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
            roi1 = clone1[y:y + h, x:x + h]
            cv2.imshow("roi1", roi1)
            cv2.putText(img, "Black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))
            bbbbb = cv2.drawContours(img, [contour], -1, (0, 255, 0), 3)  # segmentation
            # roi2 = clone1[contour[[0]]]
            # cv2.imshow("roi2", roi2)

    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

If I understand well your question you can

  • draw contour in a mask
  • convert image in hsv
  • copy hsv pixel in bgr image using mask