the code to extract the green boxes from the picture "Python-Interview.jpg" and copy them to a folder.[SOLVED]
# Import relevant libraries
image = cv.imread(fpath, -1)
# convert to gray and binarize
gray_img = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
binary_img = cv.adaptiveThreshold(gray_img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 9, 9)
# note: erosion and dilation works on white forground
binary_img = cv.bitwise_not(binary_img)
# dilate the image
kernel = cv.getStructuringElement(cv.MORPH_RECT, (1,1))
dilated_img = cv.morphologyEx(binary_img, cv.MORPH_DILATE, kernel,iterations=1)
# find contours, discard contours which do not belong to a rectangle
(cnts, _) = cv.findContours(dilated_img.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
sq_cnts = [] # contours of interest to us
for cnt in cnts:
approx = cv.approxPolyDP(cnt,0.01*cv.arcLength(cnt,True),True)
if len(approx) == 4:
(x, y, w, h) = cv.boundingRect(cnt)
#///////// fill remaining code here
for i in range(len(sq_cnts)):
# find squares
(x, y, w, h) = cv.boundingRect(sq_cnts[i])
newimg = image[y:y+h,x:x+w] # crop the image
# Write the image
The image
and the problem is ?