Ahmad Akel Omar
Ahmad Akel Omar

Reputation: 347

how i can control in area of Contours in opencv python

hi i am struggling with this code here

my quesions is -->>> how can i control the area inside the Contours .. for instance i want to blur or draw a line in this box

i am reading from a video here

this is the box i want to control to for instance draw line inside him

    while True:
    ret, frames = cap.read()
    if frames is None:
        break
    frames = imutils.resize(frames, width=600)
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 11, 17, 17)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    ret, thresh = cv2.threshold(gray, 127, 255, 0)

    # find contours in the thresholded image and initialize the
    # shape detector
    (new, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:30]

    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:  # Select the contour with 4 corners
            NumberPlateCnt = approx  # This is our approx Number Plate Contour
            cv2.GaussianBlur(NumberPlateCnt, (43, 43), 30) # here when i blured

            break
    cv2.drawContours(frames, [NumberPlateCnt], -1, (0, 255, 0), 3)
    cv2.imshow("Final frames With Number Plate Detected", frames)
    cv2.waitKey(0)
    # cv2.imshow("Final frames With Number Plate Detect", cnts)

    if cv2.waitKey(1) & 0xFF == ord('q'):
             break

here when i blured .. what should i replace ??..

          if len(approx) == 4:  # Select the contour with 4 corners
            NumberPlateCnt = approx  # This is our approx Number Plate Contour
            cv2.GaussianBlur(NumberPlateCnt, (43, 43), 30)

            break

Upvotes: 0

Views: 382

Answers (1)

MeiH
MeiH

Reputation: 1865

Sorry I don't have the original image so so I explain :

Make a black image with the same size as frames and change the drawcontour like this:

cv2.drawContours(image, [NumberPlateCnt], -1, (255, 255, 255), -1) # I changed 3 to -1

This will gives you a binary mask which only has the plate area. Inverse this mask (255-image) and name that as image2.

Now multiply first mask (image) to frames and do the blurring on the result. Then multiply second mask (image2) to frames and add the result with the last blurred image. Here is the pseudo code:

final_image = blur((image/255.)*frames) + ((image2/255.)*frames)

That's it.

Upvotes: 1

Related Questions