-2

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 ?

Ines
  • 11
  • 5

1 Answers1

0

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]
Rahul Kedia
  • 1,400
  • 6
  • 18
  • i see because we don't know the coordinate if the cropped image is taken out from the frame, right? For width is it `shape[0]` or `shape[1]` ? And what is the meaning of `:2` in `_croppedImage[:2]` – Ines Jul 24 '20 at 10:01
  • hieght is shape[0] and width is shape[1]. Actually, .shape method returns 3 values if the image is more than 1 channeled. The third value being number of channels. By using [:2], I am taking only the initial 2 values of .shape which are height and width. – Rahul Kedia Jul 24 '20 at 10:21
  • how to store it to image variable then? I can't access the shape() method if I call it directly from _croppedImage. For example like this `self._croppedImage.shape[0]`, I can't do that, I can't get the value of shape[0] – Ines Jul 24 '20 at 14:27