Ask Your Question
0

the code to extract the green boxes from the picture "Python-Interview.jpg" and copy them to a folder.[SOLVED]

asked 2020-02-12 11:01:24 -0500

updated 2020-02-16 09:35:43 -0500

supra56 gravatar image
# 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

image description

edit retag flag offensive close merge delete

Comments

and the problem is ?

berak gravatar imageberak ( 2020-02-14 05:51:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-02-15 09:16:38 -0500

updated 2020-02-16 09:22:13 -0500

supra56 gravatar image

Code:

import cv2 as cv

image = cv.imread("Python-Interview.jpg", -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)
print("Number of countours = "+ str(len(cnts)))

# contours of interest to us
sq_cnts = [] 
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
      aspectRatio = float(w) / h
      print (aspectRatio,"Aspectratio") 
      if aspectRatio > 1.0 and aspectRatio <= 1.08:
          cv.drawContours(image,[cnt], 0, (0,255,0), 3)
          sq_cnts.append(cnt)

          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    
            cv.imwrite(str(i)+'.jpg',newimg)
            cv.imshow('img.jpg', newimg)


cv.waitKey(0)
cv.destroyAllWindows()

C:\fakepath\croped.png

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-12 11:01:24 -0500

Seen: 394 times

Last updated: Feb 16 '20