Look for the outline of the numbers in the picture and crop the numbers.

Source: Internet
Author: User

This digital image from the Mnist library

The general process is: Display the original image-"grayscale (the main role is to change the number of bits of data)-" To do the anti-binary (make it into a white background)-"Look for contour-" Draw outline-"cut

Why grayscale? Originally the picture is the gray-scale ah? It turns out that there are the following reasons:

ImportCv2#Import CV LibraryImportOsimage_array= Cv2.imread ('D:/number.png')#Reading PicturesCv2.imshow ("src", Image_array)#Show original picture#print (Image_array.shape)#print (image_array.size)#print (Image_array.dtype)Image_array=cv2.cvtcolor (Image_array,cv2. Color_bgr2gray)#Convert images to grayscale images from cv_32 to cv_8uKernel= Cv2.getstructuringelement (cv2. Morph_rect, (3, 3))#define the size of the corrosive nucleidilated = Cv2.dilate (Image_array,kernel)#for CorrosionCv2.imshow ("eroded Image", dilated);#display of corroded imagesret, binary= Cv2.threshold (dilated, 127, 255, Cv2. THRESH_BINARY_INV)#to reverse the binary value ofImage,contours,hierarchy= cv2.findcontours (binary, Cv2. Retr_tree, Cv2. Chain_approx_simple)#looking for outlinesCv2.imshow ('Imageshow', image)#* * Display return value image, in fact, with the input parameters of the Thresh original no difference * *#Print (np.size (contours)) ##Print (contours[0]) # Prints the coordinates of all points of the first outline, changing the 0 here to 0--(total contour number-1) to print the coordinates of all points of the corresponding contour#print (hierarchy) #** prints out the relationship between the corresponding outlines * * forIinchRange (0,len (contours)):#Draw a contourX, Y, W, h =Cv2.boundingrect (Contours[i]) cv2.rectangle (image, (x, y)+W,Y+H), (253,255,0), 5) Cv2.imshow ("finally", image)#Show FinalNewImage= image[y + 2:y + h-2, x + 2:x + w-2]#use Y to determine the height, and then use X to determine the width #裁剪轮廓Nrootdir = ("e:/cut_image/")if  notOs.path.isdir (Nrootdir): Os.makedirs (Nrootdir) cv2.imwrite (Nrootdir+ STR (i) +". jpg", NewImage)Print(i) cv2.waitkey (0)

Display effect:

  

Look for the outline of the numbers in the picture and crop the numbers.

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.