سؤال

I have "n" number of contour detected images(frame). I wants to find mean value for the rectangle portion of that image.
(Instead of finding mean value for a full image, i need to calculate the mean value for the rectangle portion of that image.)
enter image description here
enter image description here
I have rectangle's x,y position and width, height values. First Image x,y,w,h is 109,45 ,171,139 and second image x,y,w,h is 107,71,175,110.

I get the values using the below code.
cv2.rectangle(frame, (x,y),(x+w,y+h), (0,0,255), 3)
I know using "ROI" concept we can do mean calculation.
So, I referred some links.
Ex. Get the ROI of two binary images and find difference of the mean image intesities between 2 ROI in python.

But, I have confused with the parameter settings. Can anyone help me to resolve my problem ? Thanks in advance...

هل كانت مفيدة؟

المحلول

There's easier way to get rectangle from an image in Python. Since cv2 operates on NumPy arrays, you can use normal slicing (note, that i corresponds to y and j - to x, not the other way):

rect = image[i:i+h, j:j+w]

And taking mean is even simpler:

rect.mean()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top