How to use your code to get x,y,w,h of each window
mohseniaref opened this issue · 2 comments
mohseniaref commented
Hi,
Thanks so much for sharing your nice code
I was wondering if I can extract the center of each rectangular and width and height of the rectangular using your code?How to use your code to get x,y,w,h of each window ?
Best regards,
Mohammad
AlexEidt commented
Hi Mohammad,
Thanks for your questions.
For a window defined by x, y, w, h, the center will be (x + w/2, y + h/2).
To get each window as an (x, y, w, h) tuple, I would suggest starting at line 68 in the code and making the following modifications.
windows = {} # Window tuples (x, y, w, h)
wid = 0 # Window ID
for _ in trange(iterations):
if h > min_height and w > min_width:
hw, hh = w // 2, h // 2
# Original Image Integral Image bounding box
tl = I[y+hh, x+hw] + I[y, x] - I[y, x+hw] - I[y+hh, x] # Top Left
tr = I[y+hh, x+w] + I[y, x+hw] - I[y, x+w] - I[y+hh, x+hw] # Top Right
bl = I[y+h, x+hw] + I[y+hh, x] - I[y+hh, x+hw] - I[y+h, x] # Bottom Left
br = I[y+h, x+w] + I[y+hh, x+hw] - I[y+hh, x+w] - I[y+h, x+hw] # Bottom Right
# Squared Grayscale Image Integral Image bounding box
tls = Isq[y+hh, x+hw] + Isq[y, x] - Isq[y, x+hw] - Isq[y+hh, x]
trs = Isq[y+hh, x+w] + Isq[y, x+hw] - Isq[y, x+w] - Isq[y+hh, x+hw]
bls = Isq[y+h, x+hw] + Isq[y+hh, x] - Isq[y+hh, x+hw] - Isq[y+h, x]
brs = Isq[y+h, x+w] + Isq[y+hh, x+hw] - Isq[y+hh, x+w] - Isq[y+h, x+hw]
# Grayscale Image Integral Image bounding box
tlg = Ig[y+hh, x+hw] + Ig[y, x] - Ig[y, x+hw] - Ig[y+hh, x]
trg = Ig[y+hh, x+w] + Ig[y, x+hw] - Ig[y, x+w] - Ig[y+hh, x+hw]
blg = Ig[y+h, x+hw] + Ig[y+hh, x] - Ig[y+hh, x+hw] - Ig[y+h, x]
brg = Ig[y+h, x+w] + Ig[y+hh, x+hw] - Ig[y+hh, x+w] - Ig[y+h, x+hw]
tlw, tlh = hw, hh
trw, trh = w - hw, hh
blw, blh = hw, h - hh
brw, brh = w - hw, h - hh
edited[y:y+hh, x:x+hw] = tl / (tlw * tlh)
edited[y:y+hh, x+hw:x+w] = tr / (trw * trh)
edited[y+hh:y+h, x:x+hw] = bl / (blw * blh)
edited[y+hh:y+h, x+hw:x+w] = br / (brw * brh)
if set_border:
border(edited[y:y+hh, x:x+hw])
border(edited[y:y+hh, x+hw:x+w])
border(edited[y+hh:y+h, x:x+hw])
border(edited[y+hh:y+h, x+hw:x+w])
heapq.heappush(quadrants, (-error(tlg, tls, tlw * tlh), x, y, tlw, tlh, wid+0))
heapq.heappush(quadrants, (-error(trg, trs, trw * trh), x + hw, y, trw, trh, wid+1))
heapq.heappush(quadrants, (-error(blg, bls, blw * blh), x, y + hh, blw, blh, wid+2))
heapq.heappush(quadrants, (-error(brg, brs, brw * brh), x + hw, y + hh, brw, brh, wid+3))
windows[wid+0] = (x, y, tlw, tlh)
windows[wid+1] = (x + hw, y, trw, trh)
windows[wid+2] = (x, y + hh, blw, blh)
windows[wid+3] = (x + hw, y + hh, brw, brh)
wid += 4
if quadrants:
_, x, y, w, h, uid = heapq.heappop(quadrants)
del windows[uid] # Remove a splitted window
else:
break
windows = list(windows.values()) # This will store all windows in the final image as (x, y, w, h) tuples.
mohseniaref commented
Thanks so much for your nice reponse!