Use Python and OpenCV to remove black edges of images in batches

Source: Internet
Author: User

Import Osimport cv2import numpy as npfrom scipy.stats import modeimport timeimport concurrent.futures ' ' multi-process To crop pictures. " def crop (file_path_list): Origin_path, Save_path = file_path_list img = cv2.imread (origin_path) Gray = Cv2.cvtcol Or (IMG, CV2.    Color_bgr2gray) Closed_1 = Cv2.erode (Gray, none, iterations=4) Closed_1 = Cv2.dilate (Closed_1, none, iterations=4) Blurred = Cv2.blur (Closed_1, (9, 9)) # Get the most frequent pixel num = mode (Blurred.flat) [0][0] + 1 # The THR Eshold depends on the mode of your images ' pixels num = num if num <=-Else 1 _, Thresh = Cv2.threshold (blurred , NUM, 255, Cv2.    thresh_binary) # You can control the size of kernel according your need. Kernel = Np.ones ((), np.uint8) closed_2 = Cv2.erode (Thresh, kernel, iterations=4) closed_2 = cv2.dilate (closed _2, Kernel, iterations=4) _, CNTs, _ = Cv2.findcontours (Closed_2.copy (), Cv2. Retr_external, Cv2. Chain_approx_simple) c = Sorted (CNTs, KEy=cv2.contourarea, Reverse=true) [0] # Compute the rotated bounding box of the largest contour rect = Cv2.minarearec    T (c) box = Np.int0 (cv2.boxpoints (rect)) # Draw a bounding box arounded the detected barcode and display the image # cv2.drawcontours (IMG, [box],-1, (0, 255, 0), 3) # cv2.imshow ("Image", IMG) # cv2.imwrite ("Pic.jpg", IMG) # CV  2.waitKey (0) xs = [i[0] for I in box] ys = [i[1] for I in box] x1 = min (xs) x2 = max (xs) y1 = min (ys) y2 = Max (ys) height = y2-y1 width = x2-x1 crop_img = img[y1:y1 + height, x1:x1 + width] Cv2.imwrite (save_pat H, crop_img) # cv2.imshow ("Image", crop_img) # cv2.waitkey (0) print (f ' The {Origin_path} finish crop, most Frequen        T pixel is {num} ') def multi_process_crop (Input_dir): With Concurrent.futures.ProcessPoolExecutor () as executor: Executor.map (crop, input_dir) if __name__ = = "__main__": Data_dir = ' Save_dir = ' path_list = [(Os.path.join (DA Ta_dir, O), Os.path.join(Save_dir, O)) For O in Os.listdir (data_dir)] start = Time.time () multi_process_crop (path_list) print (f ' Total cost {time.time ()- Start} seconds ')

Use Python and OpenCV to remove black edges of images in batches

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.