Search code examples
pythonopencvcomputer-visionrectanglesroi

How to get each value of x,y,w,h in this crop image?


i want to ask something. In opencv there is cv2.rectangle to build a rectangle of object interest. After I got the object interest which is represent with rectangle, I want to get the region of interest box. To do that I used crop = frame[y:y+h, x:x+w].

Now I want to get the value of x,y,w,h of that cropped box. How to do that?

Here is my code

while bol:
        capture = cv2.VideoCapture(0)
        ret, frame = capture.read()
        
        #load object detector
        path = os.getcwd()+'\haarcascade_frontalface_default.xml'
        face_cascade = cv2.CascadeClassifier(path)
        
        #convert image to grayscale
        imgGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        for (x, y, w, h) in face_cascade.CascadeClassifier(imgGray):
             cv2.rectangle(frame, (x,y), (x+w, y+h), (255,255,255), 3)
             _croppedImage = frame[y:y+h, x:x+w] 
             ROI = _croppedImage

how to get value of x,y,w,h of _croppedImage ?


Solution

  • The value of x & y for _croppedImage is of no importance and significance. This is because the _croppedImage is simply an image cropped out of another image.

    To get the w & h for _croppedImage, you can use .shape() method. w & h of an image is simply the width and height of the image respectively.

    h, w = _croppedImage[:2]