Ask Your Question

Revision history [back]

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

click to hide/show revision 2
No.2 Revision

Code:

import cv2 as cv

cv image = cv.imread("Python-Interview.jpg", -1)

-1) # convert to gray and binarize

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)

9) # note: erosion and dilation works on white forground

forground binary_img = cv.bitwise_not(binary_img)

cv.bitwise_not(binary_img) # dilate the image

image kernel = cv.getStructuringElement(cv.MORPH_RECT, (1,1)) dilated_img = cv.morphologyEx(binary_img, cv.MORPH_DILATE, kernel,iterations=1)

kernel,iterations=1) # find contours, discard contours which do not belong to a rectangle

rectangle (cnts, _)= cv.findContours(dilated_img.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE) print("Number of countours = "+ str(len(cnts)))

str(len(cnts))) # contours of interest to us

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)

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()

cv.waitKey(0) cv.destroyAllWindows() C:\fakepath\croped.pngC:\fakepath\croped.png