# Image processing on the drawcontours area

**URL:** <https://forum.opencv.org/t/image-processing-on-the-drawcontours-area/1331>\
**Category:** Python\
**Tags:** roi\
**Created:** [February 4, 2021, 6:34am UTC](https://forum.opencv.org/t/image-processing-on-the-drawcontours-area/1331 "2021-02-04T06:34:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Redhwan\_Algabri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/redhwan_algabri/32/890_2.png) [@Redhwan\_Algabri](https://forum.opencv.org/u/Redhwan_Algabri)\
**Post date:** [February 4, 2021, 6:34am UTC](https://forum.opencv.org/t/image-processing-on-the-drawcontours-area/1331/1 "2021-02-04T06:34:55Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [February 4, 2021, 8:28am UTC](https://forum.opencv.org/t/image-processing-on-the-drawcontours-area/1331/2 "2021-02-04T08:28:24Z")

</div>

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
