Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simple crop the bounding box in python opencv [duplicate]

I am trying to learn opencv and implementing a research project by testing some used cases. I am trying to crop the bounding box of the inside the image using python opencv . I have successfully created the bounding box but failed in crop

this is the image

enter image description here

import cv2
import matplotlib.pyplot as plt
img = cv2.imread("Segmentacion/Img_183.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    
dst = cv2.Canny(gray, 0, 150)
blured = cv2.blur(dst, (5,5), 0)    
MIN_CONTOUR_AREA=200
img_thresh = cv2.adaptiveThreshold(blured, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
Contours,imgContours = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in Contours:
    if cv2.contourArea(contour) > MIN_CONTOUR_AREA:
        [X, Y, W, H] = cv2.boundingRect(contour)
        box=cv2.rectangle(img, (X, Y), (X + W, Y + H), (0,0,255), 2)

cropped_image = img[X:W, Y:H]
print([X,Y,W,H])
cv2.imwrite('contour.png', cropped_image )
like image 842
Tayyab Vohra Avatar asked Jan 22 '26 07:01

Tayyab Vohra


1 Answers

I figured it out the formula for cropping the bounding box from the original image

cropped_image = img[Y:Y+H, X:X+W]
print([X,Y,W,H])
plt.imshow(cropped_image)
cv2.imwrite('contour1.png', cropped_image)
like image 120
Tayyab Vohra Avatar answered Jan 24 '26 20:01

Tayyab Vohra