Intelligent Cropping of Negative Scans

This sounds great. I have a couple thousand images that I can upload of various sizes. How many would you like?

Peter

This is great news. Hope it’s progressing well. Would it also be able to straighten the scans? I never seem to be able to scan perfectly level, usually less than 1° or 2° but that still makes quite a difference. Being able to decide how much rebate is kept would also be useful.

Feel free to share as many as you’d like, but I think 100-200 per setup should be good for now… also as part of the training I’ll have it generate variations of the existing, so it will augment the data and make the learning process more robust.

Yep! My goal is to include:

  • Auto Level
  • Auto Perspective Correction
  • Choke Tolerance (to expand or contract the auto-cropped area)
2 Likes

They are already there in Lightroom…but it looks like Lr is “searching from inside out” for perspective corrections. If it can be made to search inwards instead, the transitions between rebate and image should be easier to detect, except for thin or dense negatives.

(Scanning 645 negatives in a digitaliza holder gets me borders that appear black or white respectively)

Hello,Nate
I just wanna know will the "auto crop"function update recently?
Really need this tool.Thank you!

1 Like

I took another path instead of filling the exposed area.
Basic idea of mine is that
(0. compress the image, my GFX scanned is too detailed for edge detecting)

  1. find edges
  2. dilate the edge (make the edge more connected so it won’t cut the image bc of a strip of transparent)
  3. find the contour and keep the largest
  4. crop using this largest contour
    The output look like this:

Note that the dilate step is very important because without dilate, it might treat a single image into several ones due to the transparent area (or dark areas after conversion), see example below:

The amount of dilate (kernel size) may vary on different images

One issue is that for a negative that is very dark, especially night pictures (for example, picture of fire works), it won’t be able to correctly crop, as the edge of the frame is unclear. This is kind of an issue, but for most well-lit images, it works fine.

For example, only the upper area is being cropped for the below picture.

I’m very very new to opencv, just a couple hours of exploration, but I do believe that this auto crop is an essential step for making the process faster.

Code below:

import imageio
import rawpy, cv2
import numpy as np


input_file = ("XXXX.RAF")
with rawpy.imread(input_file) as raw:
    o_image = raw.postprocess()

    image = cv2.resize(o_image,None,fx=0.05,fy=0.05, interpolation=cv2.INTER_AREA)

    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

    blurred = cv2.GaussianBlur(gray, (5, 5), 0)

    edges = cv2.Canny(blurred,0,30)
    cv2.imshow("edges",edges)
    cv2.waitKey(0)

    kernel = np.ones((2,2), np.uint8)
    dilated = cv2.dilate(edges,kernel, iterations=1)
    cv2.imshow("dilated",dilated)
    cv2.waitKey(0)

    # Find contours
    contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    cv2.drawContours(image, contours,-1,(0, 255, 0), 1)
    cv2.imshow("contours",image)
    cv2.waitKey(0)

    largest_contour = max(contours, key=cv2.contourArea)

    # Get the bounding rectangle
    x, y, w, h = cv2.boundingRect(largest_contour)

    x,y,w,h = int(x/0.05),int(y/0.05),int(w/0.05),int(h/0.05)
    cropped_img = o_image[y:y+h, x:x+w]
    imageio.imwrite("output.jpg",cropped_img)