File size: 1,240 Bytes
d7deef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import streamlit as st
import cv2
import numpy as np
from PIL import Image

def warp_perspective(image, points):
    # Input and output dimensions
    w, h = 300, 400  # You can adjust this based on the desired output size
    input_pts = np.array(points, dtype=np.float32)
    output_pts = np.array([[0, 0], [w, 0], [w, h], [0, h]], dtype=np.float32)
    
    # Compute perspective matrix and warp the image
    matrix = cv2.getPerspectiveTransform(input_pts, output_pts)
    warped_img = cv2.warpPerspective(image, matrix, (w, h))
    
    return warped_img

st.title("Custom Shape Cropping & Perspective Correction")

uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

# Provide a placeholder for the user to input 4 vertices
points = []
for i in range(4):
    coords = st.text_input(f"Enter point {i+1} (format: x,y)", "")
    x, y = map(int, coords.split(',')) if ',' in coords else (0, 0)
    points.append([x, y])

if uploaded_file and len(points) == 4:
    image = Image.open(uploaded_file).convert('RGB')
    image_np = np.array(image)
    
    corrected_image = warp_perspective(image_np, points)
    
    st.image(corrected_image, caption='Corrected Image.', channels="BGR", use_column_width=True)