Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to find contours in a region of interest in image using opencv and Python?
Forum Updated to NodeBB v4.3 + New Features

How to find contours in a region of interest in image using opencv and Python?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 523 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coutKateM
    wrote on 21 Oct 2021, 12:50 last edited by
    #1

    while(True):

    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    a, b = 0, 0
    
    height, width, layers = frame.shape
    start_height = height - height
    left_x = width - 500
    
    #right_area = frame[y:y+height, x:x+300]
    #left_area = frame[y:y+height,]
    
    black_min = np.array([0, 0, 0], np.uint8)
    black_max = np.array([179, 255, 70], np.uint8)
    
    red_min = np.array([0, 70, 50], np.uint8)
    red_max = np.array([20, 255, 255], np.uint8)
    
    red1 = np.array([170, 70, 50], np.uint8)
    red2 = np.array([180, 255, 255], np.uint8)
    
    red_mask = cv2.inRange(hsv, red1, red2)
    red_mask2 = cv2.inRange(hsv, red_min, red_max)
    kernel = np.ones((7, 7), "uint8")
    
    red_mask = cv2.dilate(red_mask, kernel)
    res_red = cv2.bitwise_and(frame, frame, mask=red_mask)
    red_mask2 = cv2.dilate(red_mask2, kernel)
    res_red2 = cv2.bitwise_and(frame, frame, mask=red_mask2)
    
    area1 = frame[a:a + height, b:b + 300]
    area2 = frame[a:a + height, left_x:left_x + 500]
    
    cnts, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    
    for pic, c in enumerate(cnts):
    
        area = cv2.contourArea(c)
        if (700 > area > 350):
            x,y,w,h = cv2.boundingRect(c)
            cv2.rectangle(frame, (x, y),(x + w, y + h),(0, 0, 255), 2)
            cv2.putText(frame, "Red detected", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
    
    
    cv2.imshow("Video from camera", frame)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
    

    I belive that for C++ the solution looks like this:

    findContours(Mask(cv::Rect(x,y,width,height)), contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0) );

    How can I change this line to get the results I want?

    this is the area I'm interested in frame[a:a + height, b:left_x + 300] Thanks in advance!

    P 1 Reply Last reply 21 Oct 2021, 13:44
    0
    • C coutKateM
      21 Oct 2021, 12:50

      while(True):

      _, frame = cap.read()
      hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
      a, b = 0, 0
      
      height, width, layers = frame.shape
      start_height = height - height
      left_x = width - 500
      
      #right_area = frame[y:y+height, x:x+300]
      #left_area = frame[y:y+height,]
      
      black_min = np.array([0, 0, 0], np.uint8)
      black_max = np.array([179, 255, 70], np.uint8)
      
      red_min = np.array([0, 70, 50], np.uint8)
      red_max = np.array([20, 255, 255], np.uint8)
      
      red1 = np.array([170, 70, 50], np.uint8)
      red2 = np.array([180, 255, 255], np.uint8)
      
      red_mask = cv2.inRange(hsv, red1, red2)
      red_mask2 = cv2.inRange(hsv, red_min, red_max)
      kernel = np.ones((7, 7), "uint8")
      
      red_mask = cv2.dilate(red_mask, kernel)
      res_red = cv2.bitwise_and(frame, frame, mask=red_mask)
      red_mask2 = cv2.dilate(red_mask2, kernel)
      res_red2 = cv2.bitwise_and(frame, frame, mask=red_mask2)
      
      area1 = frame[a:a + height, b:b + 300]
      area2 = frame[a:a + height, left_x:left_x + 500]
      
      cnts, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      
      
      for pic, c in enumerate(cnts):
      
          area = cv2.contourArea(c)
          if (700 > area > 350):
              x,y,w,h = cv2.boundingRect(c)
              cv2.rectangle(frame, (x, y),(x + w, y + h),(0, 0, 255), 2)
              cv2.putText(frame, "Red detected", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
      
      
      cv2.imshow("Video from camera", frame)
      if cv2.waitKey(10) & 0xFF == ord('q'):
          break
      

      I belive that for C++ the solution looks like this:

      findContours(Mask(cv::Rect(x,y,width,height)), contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0) );

      How can I change this line to get the results I want?

      this is the area I'm interested in frame[a:a + height, b:left_x + 300] Thanks in advance!

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 21 Oct 2021, 13:44 last edited by
      #2

      @coutKateM and how is this related to the Qt framework?
      Your code snippet has no Qt object at all!
      It looks like you may need to ask this question in an OpenCV forum... more chances to get proper answers I guess

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1

      1/2

      21 Oct 2021, 12:50

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved