The cropped image displays with the wrong dimensions

Greetings.

I have a question regarding a cropping of an image using the OpenCV Python. Specifically, I want to crop an image using the existing coordinates and then preview the cropped image using the cv2.imshow. The problem is – the previewed cropped image doesn’t match the border that was drawn on the initial frame, as it is shown on the screenshots. Moreover – the difference in size of a cropped image and the red rectangle is changing from frame to frame.

This is the function I use to crop the frame from the frame sequence (it crops every frame and enlarges it, receiving the initial frame, (x,y) coordinates and the “step” - int variable):

frame_width = int(cap.get(3))
frame_height = int(cap.get(4)) 
frame_center = [int(frame_width/2), int(frame_height/2)]

def crop_frame_targeted_from_center(frame, x, y, step):
    global frame_width, frame_height, frame_center
    d_w = round(frame_width*step/100)    
    d_h = round(frame_height*step/100)   
    l_x = x - frame_center[0]
    l_y = y - frame_center[1]

    if abs(l_x) > d_w:   
        if l_x >= 0:     
            d_x = d_w     
        else:            
            d_x = -d_w  
    else:                
        d_x = l_x        

    if abs(l_y) > d_h:
        if l_y >= 0:
            d_y = d_h
        else:
            d_y = -d_h
    else:
        d_y = l_y

    x_step_1 = d_w + d_x
    y_step_1 = d_h + d_y
    x_step_2 = frame_width - d_w + d_x
    y_step_2 = frame_height - d_h + d_y

    frame_uncut = copy.copy(frame)
    cv2.rectangle(frame_uncut, (x_step_1, y_step_1, x_step_2, y_step_2), (0, 0, 255), 2)
    cv2.imshow("Frame_uncut", frame_uncut)
    if step > 10:
        stop = 1

    frame_targeted = frame[y_step_1:y_step_2, x_step_1:x_step_2]
    cv2.imshow("Frame_targeted_unresized", frame_targeted)
    h_1, w_1, i_1 = frame_targeted.shape
    if step > 10:
        stop = 1

    if h_1 == y_step_2-y_step_1 and w_1 == x_step_2-x_step_1:
        stop = 1

    frame_targeted = cv2.resize(frame_targeted, (frame_width, frame_height))

    return frame_targeted

During the debug I tried to check the equality of the cropped frame dimensions and the respective coordinates of the preview rectangle drawn on the initial frame. They seem to be the exact match.

Is this a problem with the cv2.imshow or something else? I already used the cropping functionality to receive the cropped image to work with. The only difference is – till this moment I didn’t need to display the cropped image. Now I do need to display it. So what can I do to make the displayed result match the coordinates I input exactly?

Thanks in advance.

MRE please. notably, this is different from copying your entire code or copying a piece of it.

the screenshot only shows us that the smaller window doesn’t match the red rectangle. the screenshot does not provide a way to reproduce your issue.

I understand. The code of the function was added to the post.

UPD.
Tried to crop the image using PIL module in the new project with only this function and cv2.cap frame sequence import. Same problem persists when converting the cropped image back to cv2. format and using cv2.imshow to display it.

UPD 2.
Ok… It seems the error was mine. I didn’t put brackets to separate (x_step_1, y_step_1, x_step_2, y_step_2) in 2 different points. Doing that solved the problem.