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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With