title: Finding the bounding boxes of different symbols/letters
body: <p>I am learning about OpenCV bounding boxes and so far it has been good to me. However, I am having trouble finding the bounding slightly more complicated letters/symbols.</p>
  
  <p>I have looked at the documentation, but I couldn't find anything relating to how to find bounding boxes of complicated symbols/letters (eg %, ! or the letter j or i)</p>
  
  <p>I have attached my input image.<a href="https://i.stack.imgur.com/ScodY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ScodY.jpg" alt="Input Image"></a></p>
  
  <p>The code I use to process the bounding boxes.</p>
  
  <pre><code>import cv2
  import numpy as np
  
  img=cv2.imread('test.jpg')
  imgCont=img.copy()
  imgFinal=img.copy()
  
  
  imggray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  imgcanny=cv2.Canny(imggray,200,240)
  contours,_ = cv2.findContours(imgcanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  
  rect_d=[] #store the dimensions of my bounding boxes
  min_cont_area=10
  for cnt in contours:
      #print(cv2.contourArea(cnt))
      if cv2.contourArea(cnt)&gt;min_cont_area: #Limit the contours based on area?  
          box_d=cv2.boundingRect(cnt)
          x,y,w,h=box_d
          rect_d.append([x,y,w,h]) 
          cv2.rectangle(imgCont, (x,y),(x+w,y+h), (0, 0, 255),1)
  
  i=0
  roi_n=[] #store each bounding box for later
  for d in range(0,len(rect_d)):    
      x=rect_d[i][0] #Find x 
      y=rect_d[i][1] #Find y 
      w=rect_d[i][2] #Find width 
      h=rect_d[i][3] #Find height 
      roi=imgFinal[y:y+h,x:x+w]
      roi_n.append(roi)
      cv2.imwrite('Boxed_ROIs_'+str(i) + '.jpg', roi) 
      i+=1
  
  cv2.imshow('Final Result',imgCont)
  cv2.imwrite('Final Result.jpg',imgCont)
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  
  </code></pre>
  
  <p>The output I get from my code.
  <a href="https://i.stack.imgur.com/Sf5Nr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Sf5Nr.jpg" alt="My output"></a></p>
  
  <p>If anyone can help me understand how to go about :</p>
  
  <p>1) Find irregular bounding polygons (not boxes) eg with the "square root of 3" have a irregular bounding polygon for the "square root" and a bounding box for "3"</p>
  
  <p>2) Is it possible to say merge bounding boxes horizontally/vertically/diagonally. Eg it would help if I know how to vertically merge the bounding boxes for the "=" sign or diagonally merge the bounding boxes for "%".</p>
  
  <p>3) Is it also possible to split bounding boxes into several bounding boxes?</p>
  
  <p>My desired output would look something like this.
  <a href="https://i.stack.imgur.com/gslMo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gslMo.jpg" alt="enter image description here"></a></p>
  
  <p>**********************UPDATE********************************************</p>
  
  <p>I managed to update based on your suggestions however, I am not sure how to get the square root symbol, ! and = symbol on its own bounding boxes.</p>
  
  <pre><code>import cv2
  import numpy as np
  
  img=cv2.imread('test.jpg')
  imgCont=img.copy()
  imgFinal=img.copy()
  
  imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  
  ret,imgThresh = cv2.threshold(imgGray,249,250,cv2.THRESH_BINARY_INV)
  
  kernel_erosion = np.ones((1,1),np.uint8)
  imgErode = cv2.erode(imgThresh,kernel_erosion,iterations = 2)
  
  kernel_open = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(1,2))
  imgOpen = cv2.morphologyEx(imgErode, cv2.MORPH_OPEN, kernel_open)
  
  kernel_dilate = np.ones((2,2),np.uint8)
  imgDilate = cv2.dilate(imgOpen,kernel_dilate,iterations = 4)
  
  contours,_ = cv2.findContours(imgDilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  
  rect_d=[] #store the dimensions of my bounding boxes
  min_cont_area=5
  for cnt in contours:
      #print(cv2.contourArea(cnt))
      if cv2.contourArea(cnt)&gt;min_cont_area: #Limit the contours based on area?  
          box_d=cv2.boundingRect(cnt)
          x,y,w,h=box_d
          rect_d.append([x,y,w,h]) 
          cv2.rectangle(imgCont, (x,y),(x+w,y+h), (0, 0, 255),1)
  
  i=0
  roi_n=[] #store each bounding box for later
  for d in range(0,len(rect_d)):    
      x=rect_d[i][0] #Find x 
      y=rect_d[i][1] #Find y 
      w=rect_d[i][2] #Find width 
      h=rect_d[i][3] #Find height 
      roi=imgFinal[y:y+h,x:x+w]
      roi_n.append(roi)
      cv2.imwrite('Boxed_ROIs_'+str(i) + '.jpg', roi) 
      i+=1
  
  #cv2.imshow('Final Result',imgCont)
  #cv2.imwrite('Final Result.jpg',imgCont)
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>My updated output:
  <a href="https://i.stack.imgur.com/hRX2r.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hRX2r.jpg" alt="Update Output"></a></p>