# How to identify the correct coordinates for cropping image

**URL:** <https://forum.opencv.org/t/how-to-identify-the-correct-coordinates-for-cropping-image/8381>\
**Category:** Python\
**Tags:** numpy, programming\
**Created:** [April 13, 2022, 5:01am UTC](https://forum.opencv.org/t/how-to-identify-the-correct-coordinates-for-cropping-image/8381 "2022-04-13T05:01:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![opencvnoob](https://avatars.discourse-cdn.com/v4/letter/o/c0e974/32.png) [@opencvnoob](https://forum.opencv.org/u/opencvnoob)\
**Post date:** [April 13, 2022, 5:01am UTC](https://forum.opencv.org/t/how-to-identify-the-correct-coordinates-for-cropping-image/8381/1 "2022-04-13T05:01:16Z")

</div>

I am using a face detection library(retina-face) to detect face on the following image

 ![model_2_0](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d50ea4927d363b0d55db42fbca047c2b77307a68.jpeg)

The library being used is retina-face which gives bounding box coordinates in the format (x1,y1,x2,y2).  
The Bounding box for the face is as follows  
face\_area=[242, 92, 269, 129]

Now,when I run the following part of the code,I am able to draw the bounding box on the image to represent the face

```auto
img=cv2.imread('model_2_0.jpg')
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.rectangle(img,(face_area[2],face_area[3]),(face_area[0],face_area[1]),(255,0,0),1)

```

The result of this is as follows  
 ![img_bbox](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/ddde125d1671eff1fc1e9b2f7b4395f3c0791e52.png)

But when I use slicing to just obtain the face part of the image either I run into zero-sized array error or I get an incorrect result.  
I am trying to crop as follows

```auto
face=img[face_area[1]:face_area[0],face_area[3]:face_area[2])

```

The incorrect crop is shown below  
 ![crop_incorrect](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f1cf740f782ff09156d32d1f30aef794a16785e8.png)

Any idea what I am doing wrong here,and is there a better way to crop rather than using slicing

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 13, 2022, 7:28am UTC](https://forum.opencv.org/t/how-to-identify-the-correct-coordinates-for-cropping-image/8381/2 "2022-04-13T07:28:30Z")

</div>

you just got the order of the coords wrong,  
if you get (x1, y1, x2, y2) from the detection, the correct numpy slice is::

```
img[y1:y2, x1:x2] 

```

also your cv2.rectangle() coords should better be:

```
(x1,y1), (x2,y2)
```

---

<div class="post-metadata">

**Author:** ![opencvnoob](https://avatars.discourse-cdn.com/v4/letter/o/c0e974/32.png) [@opencvnoob](https://forum.opencv.org/u/opencvnoob)\
**Post date:** [April 13, 2022, 4:40pm UTC](https://forum.opencv.org/t/how-to-identify-the-correct-coordinates-for-cropping-image/8381/3 "2022-04-13T16:40:57Z")

</div>

Thanks for the tip,I was able to get the result.On a side note the cropped image tends to be extremely coarse as shown below  
 ![cropped_face_mediapipe_Nupsample](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a1c915946e1a794d6174507286634f916555c89c.png)  
I am currently upsampling it using cv2.pyrUp and then resize it to (256,256) to get this image  
 ![cropped_face_mediapipe_upsample](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c2b6dbc3e79f63a170158a547fe3f820c83ddfdd.png)  
Would you recommend that’s the correct approach. I am looking for a better image where the eyes can be clearly seen as shown in the original image

 ![model_0_0](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/1a1902582b7f9297244d34e4e92a81918224c3f1.jpeg)
