1

I have to center the object of this image 1 in a 1080x1080 square, with the same distance from the top and bottom (100px) image 2 - in other words the object' height should be 880.

Image 1 input_image

Image 2 output_image

I'm a newby and I tried to watch a bunch of tutorial trying to understand how to do it.

I thought that would be possible to detect the object and then the center point of the it and crop it, but the white points of dust on the scan messed up the whole shape detection.

  • Is there a way to make it work, not only for this scan but for every other book scan I'll use?

  • If the object in the input image is not straight, how do I manage to straitghten it up?

Thanks in advance

As suggested I'm adding the code I'm using

Like I said before, it's the result of me watching different tutorial and having very little knowledge, so probably there will be some mistakes.

`

import cv2
import numpy as np


img = cv2.imread('37_gialli-rizzoli.jpg')
hh, ww = img.shape[:2]

lower = np.array([0, 0, 0])
upper = np.array([10, 10, 10])

thresh = cv2.inRange(img, lower, upper)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (20, 20))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

mask = 255 - morph

img_mask = cv2.bitwise_and(img, img, mask=mask)

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

_, contours, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

print("no of shapes: {0}".format(len(contours)))

for cnt in contours:
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    img = cv2.drawContours(img, [box], 0, (0,0,255)) 

cv2.imshow('ImageWindow', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

`

a.cale
  • 11
  • 4
  • If the images have nice contrast like your sample - Get a binary mask via fixed thresholding with a cutoff just past the black distribution. Get the external contours and filter by area. Get the book's external contour and bounding rectangle. Crop. Add borders to the left, right , top and bottom of the book with desired length that might be a function of the image's original dimensions (or not, that is unclear from your requirements). Save new image. – stateMachine Apr 15 '22 at 00:25
  • If the object is not straight, you might want to get its `RotatedRect` to get the corners. Use `getRotationMatrix2D` and `warpAffine` to straighten the object. Now, add borders as suggested above. – stateMachine Apr 15 '22 at 00:29
  • in general, if you wrote code and tried it, and you think the general approach is sensible, please present the code in your question. you mentioned noise, so maybe your chosen threshold was bad, or those bits of dust can be suppressed with a **median filter**. – Christoph Rackwitz Apr 15 '22 at 07:35
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 15 '22 at 09:12
  • Threshold, get largest contour, insert into center of black image that has the size you want. See https://stackoverflow.com/questions/59525640/how-to-center-the-content-object-of-a-binary-image-in-python/59535922#59535922 and https://stackoverflow.com/questions/58150602/how-to-center-another-image-into-a-zero-matrix-in-python/58151404#58151404 – fmw42 Apr 15 '22 at 17:33

0 Answers0