1

I have a simple image file as below, where I have defined a set of coordinates, I want to crop.

The cropping selection is happening on the frontend, which means that the image that the user is seeing, is scaled down to fit the user's screen. Below is the information of the image:

imageWidth: 965
imageHeight: 1365.46875

Based on above width and height, the coordinates of the box you see in the image, is:

"x":405.5,
"y":130,
"height":36
"width":129

(All numbers are in pixels)

Image I want to crop

On my backend, in my Python code, I want to make sure that I crop the same as the users are seeing. So I figured, I would have to calculate the x,y scaling factor:

import numpy as np
import cv2

#Open the original image and get widht/height
image = cv2.imread(image_file)
height, width = image.shape[0:2]

The original image's height and width is: H: 3508, W: 2479

I then calculate the scaling factors:

# Set scaling factors
scalingFactorX = width / imageWidth
scalingFactorY = height / imageHeight

Which equals: Scaling X: 2.5689119170984456, Scaling Y: 2.569081130564138

After calculating the scaling factors, I convert the original coordinates to scale accordingly:

crop_x = scalingFactorX * box['x']
crop_y = scalingFactorY * box['y']
crop_w = scalingFactorX * box['width']
crop_h = scalingFactorY * box['height']

And then finally, I crop the image and save it:

crop = image[int(crop_y):int(crop_y)+int(crop_h),
             int(crop_x): int(crop_x) + int(crop_w)]
cv2.imwrite("cropped_image.png", crop)

However, the cropped area is not correct. It's simply a blank image (I guess the coordinates are way off, which means it ultimately crops another part of the page):

The result is a 331 x 92 pixel blank image.

I am not sure what I am doing wrong?

Edit:

The original image can be found here.

Community
  • 1
  • 1
oliverbj
  • 5,771
  • 27
  • 83
  • 178
  • @martineau this is the converted coordinates (`crop = `): `333:425, 1041:1372` - I am not quite sure how I can check these coordinates again the original picture, to see, visually, if I am way off? – oliverbj Sep 22 '19 at 18:32
  • Do the math by hand — that doesn't seem like it would be too difficult — and compare that to what you're actually getting. – martineau Sep 22 '19 at 18:40
  • I did the math in an excel sheet, and got the same result (same scaling factors and same coordinates) – oliverbj Sep 22 '19 at 18:41
  • 1
    Then it seems likely the way you're using the information to crop the image is wrong. Sorry, I'm no `cv2` expert, but perhaps you should take a closer look at its documentation and/or see if you can find some example code somewhere. – martineau Sep 22 '19 at 18:45
  • `height, width = image.shape[0:2]` looks like the first dimension is height and second is width. Did you swap them in your slice? I got different numbers than you but the first dimension slice should be larger than the second. – wwii Sep 22 '19 at 19:04
  • When printing `print("H: {}, W: {}".format(height, width))`, I get: `H: 3508, W: 2479`, which seems correct? – oliverbj Sep 22 '19 at 19:08
  • Why is `imageWidth` less than `imageHeight` ? Your example picture wider than it is tall. I think you've gotten your heighth/width, x/y mixed up. – wwii Sep 22 '19 at 19:24
  • @wwii That's just a cropping I did. The actual image is located here: https://imgur.com/fO2HS1O – oliverbj Sep 22 '19 at 19:26
  • What is `box`? And is its `'x','y','width','height'` designations the same? – wwii Sep 22 '19 at 20:31
  • 1
    Why do you have an actual image and a link to an original image in your question? Just the correct image would be better - we don't need two, thank you. – Mark Setchell Sep 22 '19 at 21:04

0 Answers0