Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv.GetSubRect migration to OpenCV 4

Tags:

python

opencv

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?

like image 284
Wolfgang Fahl Avatar asked Dec 02 '25 00:12

Wolfgang Fahl


1 Answers

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
like image 193
Wolfgang Fahl Avatar answered Dec 04 '25 13:12

Wolfgang Fahl