import cv2
import numpy as np
img=cv2.imread("E:/new.png",cv2.IMREAD_GRAYSCALE)
cv2.imshow("Original",img)image1,contours,hier=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
image1,contours,hier=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(img,8,cv2.CV_32S)
statcontours=[]
for cnt in contours:
rect = cv2.minAreaRect(cnt)
(x, y), (w, h), angle = rect
statcontours.append(rect)
cv2.drawContours(rgb_img,[box],0,(0,0,255),2)
cv2.imwrite("bbimage21",img)