3

OpenCV used to have function GetSubRect see e.g. https://docs.opencv.org/2.4/modules/core/doc/old_basic_structures.html

So the following lines where valid:

top = cv.GetSubRect(tmp, (0, 0, W, H/4))
left = cv.GetSubRect(tmp, (0, 0, W/4, H))
bottom = cv.GetSubRect(tmp, (0, H*3/4, W, H/4))
right = cv.GetSubRect(tmp, (W*3/4, 0, W/4, H))

whitenesses = []
whitenesses.append(cv.Sum(top)[2])
whitenesses.append(cv.Sum(left)[2])
whitenesses.append(cv.Sum(bottom)[2])
whitenesses.append(cv.Sum(right)[2])

Now OpenCV has moved on and uses numpy array all over.

https://stackoverflow.com/a/58211775/1497139 tries to explain this but has no upvotes.

ROI = image[y1:y2, x1:x2]

Would now be the new approach. But then it should be possible to write a GetSubRect function that is compatible to the old style this way.

How would such a GetSubRect function look like?

Why is there no such function in OpenCV any more to ease the migration?

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • 1
    "Why is there no such function in OpenCV any more to ease the migration?" - `GetSubRect` was part of the C API, which has been deprecated since release 3.0 (circa 2013). Given that you've ran into an issue 6 years later...I guess there was enough time to easily migrate, wouldn't you agree? (I mean, I still have some apps using 2.4.x but I'm well aware there will be some work involved to upgrade them) – Dan Mašek Oct 18 '19 at 23:05
  • No - I won't agree. Migration should not be forced upon people but always be easy no matter what the timing is. – Wolfgang Fahl Oct 19 '19 at 04:09

1 Answers1

3

The first part I could answer my self now:

Code:

def getSubRect(self,image,rect):
    x,y,w,h = rect
    return image[y:y+h,x:x+w]

test:

def test_getSubRect():
    image=cv2.imread("testMedia/chessBoard001.jpg",1)
    subImage=getSubRect(image,(0,0,200,200))
    iheight, iwidth, channels = subImage.shape
    assert iheight==200
    assert iwidth==200
    assert channels==3

sum code

# get the intensity sum of a hsv image
def sumIntensity(self,image):
    h,s,v=cv2.split(image)
    height, width = image.shape[:2]
    sumResult=np.sum(v)
    return sumResult
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186