Home > Software engineering >  how to reduce the size of the window opened with cv2.imshow()?
how to reduce the size of the window opened with cv2.imshow()?

Time:08-17

I'm working on this code. I got a problem with the size of the windows showing 'lane1' and 'lane2'. they are so big and uncomfortable to work with. Does opencv have any function that makes it possible to control the size of windows.

PS: the code is about object detection on multiple ROIs. I'd like to count the number of vehicles showing on each line (that means I got to add some more steps on tracking)

import cv2
import numpy as np

def detection1(y2,y1,x2,x1,name):
    roi=img[y2:y1,x2:x1] 
    blob= cv2.dnn.blobFromImage(roi, 1/255, (416, 416), (0,0,0), swapRB=True, crop=False)
    net.setInput(blob)
    output_layers_names = net.getUnconnectedOutLayersNames()
    layerOutputs = net.forward(output_layers_names)
    #showing information on the screen
    boxes = []
    confidences = []
    class_ids = []
    for output in layerOutputs: 
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if classes[class_id] in allowed_objects:
                if confidence > 0.2:
                    center_x = int(detection[0]*(x1-x2)) #(x2-x1)=width
                    center_y = int(detection[1]*(y1-y2)) #(y2-y1)=height
                    w = int(detection[2]*(x1-x2))
                    h = int(detection[3]*(y1-y2))
    
                    x = int(center_x - w/2)
                    y = int(center_y - h/2)
    
                    boxes.append([x, y, w, h])
                    confidences.append((float(confidence)))
                    class_ids.append(class_id)
    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.4)
    if len(indexes)>0:
        for i in indexes.flatten():
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidences[i],2))
            cv2.rectangle(roi, (x,y), (x w, y h), (0,0,0), 1)
            cv2.putText(roi, label   " "   confidence, (x, y-15), font, 1, (255,255,255), 1)
    cv2.imshow(name,roi)
# Load Yolo
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')

classes = []
with open("coco.names", "r") as f:
    classes = f.read().splitlines()
allowed_objects=['car','truck','motorbike','bicycle','bus']

#loading video
cap = cv2.VideoCapture('Traffic_Trim.mp4')
font = cv2.FONT_HERSHEY_PLAIN
#reading frames from video
while True:
    _, img = cap.read()
    detection1(y2=216, y1=1080, x2=1008, x1=1560,name='lane1')
    detection1(y2=216, y1=1080, x2=72, x1=984,name='lane2')
    key = cv2.waitKey(1) 
    if key==27:
        break

cap.release() 
cv2.destroyAllWindows()

CodePudding user response:

you can use cv2.resize to do this task:

dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) 

CodePudding user response:

you can use cv.resizeWindow, coupled with the WINDOW_NORMAL flag to cv.namedWindow.

That spares you from having to resize the image itself (with cv.resize) and it allows you to resize the window with your mouse!

import cv2 as cv
some_image = cv.imread(cv.samples.findFile("lena.jpg"))

cv.namedWindow("some window", cv.WINDOW_NORMAL)
cv.imshow("some window", some_image)
cv.resizeWindow("some window", 200, 400)

cv.waitKey() # to run the GUI

Available cv::WindowFlags in official documentation

  • Related