Cut out A4 print in the image

Cut out A4 print in the image

Converts a print existing in a photo into an image cropped from directly above. image.png You can convert the upper photo to the lower photo. image.png

Code notes

--Since it is converted to a vertically long A4 file, rewrite x and y in the case of landscape. --Since x is the length of the short side, rewrite it appropriately.

code

cutting_a4.py


#Receive a color image, cut out the paper and return it
def cutting_paper(img):
    x = 2000 #X coordinate after cutting
    y = int(x*1.415)
    img_gray = gray(img)
    #Binarization
    ret,img_th1  = cv2.threshold(img_gray,220,255,cv2.THRESH_TOZERO_INV)
    img_not = cv2.bitwise_not(img_th1)
    ret,img_th2  = cv2.threshold(img_not,0,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU )
    #Contour extraction
    contours, hierarchy = cv2.findContours(img_th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    #The second largest square is the outline of the paper, so paper_Take out tap
    pic_tap = None #Image contour
    paper_tap = None #Paper contour
    for i, con in enumerate(contours):
        size = cv2.contourArea(con)
        if pic_tap is None:
            pic_tap = (con,size)
            continue
        if pic_tap[1] <= size:
            pic_tap = (con,size)
            continue
        elif paper_tap is None:
            paper_tap = (con,size)
        elif paper_tap[1] <= size:
            paper_tap = (con,size)
        
    #Approximate straight line and draw
    epsilon = 0.1*cv2.arcLength(paper_tap[0],True)
    paper_corners= cv2.approxPolyDP(paper_tap[0],epsilon,True)#Paper vertex coordinates
    fix_con = np.array([[[0,0]],[[x,0]],[[x,y]],[[0,y]]], dtype="int32")#Size after shaping

    M = cv2.getPerspectiveTransform(np.float32(paper_corners),np.float32(fix_con))#Generation of transformation matrix
    img_trans = cv2.warpPerspective(img,M,(x,y))#conversion
    return img_trans

img = cv2.imread('input.png',-1)
cv2.imwrite("output.png ", img)
plt.imshow(plt.imread("output.png "))

Recommended Posts

Cut out A4 print in the image
I tried to cut out a still image from the video
Try loading the image in a separate thread (OpenCV-Python)
The image is a slug
Find out the apparent width of a string in python
I wrote a script that splits the image in two
Cut out an image with python
Detect mosaic points in the image
Let's cut the face from the image
Use print in a Python2 lambda expression
I want to print in a comprehension
Write the test in a python docstring
Just print the selected object in Blender
Change the list in a for statement
Run the Python interpreter in a script
Print the include header path in clang
Arrange the numbers in a spiral shape
[Python] Get the files in a folder with Python
[Python3] Code that can be used when you want to cut out an image in a specific size
Use the latest pip in a virtualenv environment
Get the caller of a function in Python
What is wheezy in the Docker Python image?
Make a copy of the list in Python
Find the number of days in a month
A program that searches for the same image
Get only the subclass elements in a list
Determining if there are birds in the image
Determine the numbers in the image taken with the webcam
Get a panoramic image in Google Street View
Detect folders with the same image in ImageHash
Set a fixed IP in the Linux environment
Output in the form of a python array
In print, the non-ascii characters in the list look great
[Python] Find the transposed matrix in a comprehension
Put out a shortened URL string in Python
Isn't there a default value in the dictionary?
[Python] Change the text color and background color of a specific keyword in print output
Find out the maximum number of characters in multi-line text stored in a data frame
Check if the string is a number in python
Created a fooling image for the caption generative model
Get the file name in a folder using glob
A list of stumbling blocks in Django's image upload
Cut a part of the string using a Python slice
Register a task in cron for the first time
[Python] Get the numbers in the graph image with OCR
Convert the image in .zip to PDF with Python
Write a log-scale histogram on the x-axis in python
[Python] Mask the image into a circle using Pillow
Display a histogram of image brightness values in python
A reminder about the implementation of recommendations in Python
Python OpenCV tried to display the image in text.
What does the last () in a function mean in Python?
It is difficult to install a green screen, so I cut out only the face and superimposed it on the background image
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
When reading an image with SimpleITK, there is a problem if there is Japanese in the path
A program that automatically resizes the iOS app icon to the required image size in Python
Image format in Python
dict in dict Makes a dict a dict
Image uploader in Flask
Image normalization in TensorFlow
[Sublime Text 2] Always execute a specific file in the project