#====================================================================================
# CBCropCenterVStrip_V1.0
#
# «CB Crop Center Vertical Strip»
# -------------------------------------------
# Cesare Brizio, https://www.cesarebrizio.it
# License - CC BY-SA 4.0
# -------------------------------------------
#     The program accepts as input a series of image files - originally jpg,
#     any other standard can be supported by editing the image suffix hard-coded
#     in the relevant instructions.
#
#     Requirements for the images include the following:
#     - all the images are taken with the rotation axis perfectly vertical
#     - the rotation axis is perfectly aligned at the exact center of the
#       field of view and consequently of the image
#     - each image is taken at a different angular position, with constant 
#       consecutive and equal rotation intervals
#     - all the images have exactly equal height in px, 
#     - all the images are taken with an objective above 50 mm of focal length 
#       to minimize barrel distorsion.
#
#     A hard-coded parameter (600 in the original version of the program) defines
#     the width in pixel of the central strip that will be cut from each image and
#     saved as a separate file.
#
#     The intended use of the program is in support of the "rollout photograpy"
#     (also known as "peripheral photography") of seashells, as illustrated in
#     the article by Cesare Brizio on "The Festivus", with reference to
#     shell pictures taken at equal rotation intervals around the longitudinal 
#     axis.
#
#     The program results can be input in the Image Composite Editor (ICE) by Microsoft.
#     While ICE could not effectively merge whole images of the shell, it delivers 
#     satisfactory results if fed with the central strips generated by this program.
#
# New in version 1 (9 October 2026): First version, separate save of strips.
# New in version 2 (): 
# New in version 3 ():
#
#============================================================================================
# The program was partly generated by GOOGLE GEMINI via simple prompts,
# and was subsequently heavly modified
#=============================================================================================

import cv2
import glob
import numpy as np

# Load images in sequence order
image_paths = sorted(glob.glob("shell_*.jpg"))
strips = []

# Adjust width depending on overlap/angle step size
STRIP_WIDTH = 600 

count = 1

for path in image_paths:
    img = cv2.imread(path)
    h, w, c = img.shape
    
    # Calculate center bounding box
    center_x = w // 2
    left = center_x - (STRIP_WIDTH // 2)
    right = center_x + (STRIP_WIDTH // 2)
    
    # Extract strip
    strip = img[:, left:right]

    # save each strip as a separate image
    image_strip_name = "strip_"+str(count).zfill(2)+".jpg"
    cv2.imwrite(image_strip_name, strip)
    count = count + 1

    strips.append(strip)