# Python - Trying to Find Average Intensity of a ROI

I am trying to find the average intensity of a binarized ROI. If the average intensity of the ROI is greater than a cutoff point (meaning if the ROI is mostly white), I want a rectangle to be drawn around it. Below is my code to do that. The output, however, is an image where almost every single ROI has a rectangle drawn around it, even the ones that are completely black (meaning its average ROI intensity is 0.0). I would really appreciate your help, thanks.

The block of code with the problem:

 for i in range (0,height, int(boxsize)):
for j in range (0,width, int(boxsize)):
#1. DRAW THE BLOCKS
roi_gray = edge[i:i+int(boxsize),j:j+int(boxsize)]
#2. FIND INTENSITY OF ROI
roi_avg_intensity = np.mean(roi_gray)
#3. BASED ON THAT, SEE IF ROI IS AN OBSTACLE OR NOT
if roi_avg_intensity > cut_off_point:
cv2.rectangle(edge, (j,i), (j+int(boxsize), i+int(boxsize)),(128,128,128),2)


Output image:

https://imgur.com/a/oWgVP

Raw image: https://imgur.com/a/HAYT9

All of the code:

import cv2
import numpy as np
import scipy.signal
import math

#edge detection
ret, thresh = cv2.threshold(roomimg, 127, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
edge = cv2.Canny(thresh, 100, 200)
height,width,channels = roomimg.shape
matrix = []
column = []

#define the dimensions of the grid
def estimate_noise(I):

H, W = I.shape

M = [[1, -2, 1],
[-2, 4, -2],
[1, -2, 1]]

sigma = np.sum(np.sum(np.absolute(scipy.signal.convolve2d(np.array(I), M))))
sigma = sigma * np.sqrt(0.5 * np.pi) / (6 * (W-2) * (H-2))

return sigma

boxsize = math.pow(estimate_noise(edge),-0.708)* 112.32

#defines what are obstacles and what are not
cut_off_point = 15

#U HAVE TO CHANGE CUT OFF POINT BASED ON EVERY IMAGE

for i in range (0,height, int(boxsize)):
for j in range (0,width, int(boxsize)):
#1. DRAW THE BLOCKS
roi_gray = edge[i:i+int(boxsize),j:j+int(boxsize)]
#2. FIND INTENSITY OF ROI
roi_avg_intensity = np.mean(roi_gray)
#3. BASED ON THAT, SEE IF ROI IS AN OBSTACLE OR NOT
if roi_avg_intensity > cut_off_point:
cv2.rectangle(edge, (j,i), (j+int(boxsize), i+int(boxsize)),(128,128,128),2)

cv2.imshow('Image Edges', edge)

if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()

edit retag close merge delete

Hi Stephane, could you show the raw image rather than the output image...

And may I ask why the cut_off_point is set as 15?

( 2018-03-28 09:20:03 -0500 )edit

@moHe I added the raw image to my original question. The cut_off_point is also set to 15 because an average ROI intensity above that means that the ROI is mostly white, while an average ROI intensity below that means the ROI is mostly black.

( 2018-03-29 01:18:59 -0500 )edit

Sort by ยป oldest newest most voted

Hello, Stephane. Is this what you'd expected?

Cancel the thresh operation, otherwise there would be only almost 5 places <= cut_off_point in the whole image as I did the counting.

import cv2
import numpy as np
import scipy.signal
import math

# edge detection
# ret, thresh = cv2.threshold(roomimg, 127, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
thresh = cv2.cvtColor(roomimg, cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(thresh, 100, 200)
height,width,channels = roomimg.shape
matrix = []
column = []

#define the dimensions of the grid
def estimate_noise(I):

H, W = I.shape

M = [[1, -2, 1],
[-2, 4, -2],
[1, -2, 1]]

sigma = np.sum(np.sum(np.absolute(scipy.signal.convolve2d(np.array(I), M))))
sigma = sigma * np.sqrt(0.5 * np.pi) / (6 * (W-2) * (H-2))

return sigma

boxsize = math.pow(estimate_noise(edge),-0.708)* 112.32

#defines what are obstacles and what are not
cut_off_point = 15

#U HAVE TO CHANGE CUT OFF POINT BASED ON EVERY IMAGE

box_num = 0
for i in range (0,height, int(boxsize)):
for j in range (0,width, int(boxsize)):
#1. DRAW THE BLOCKS
roi_gray = edge[i:i+int(boxsize),j:j+int(boxsize)]
#2. FIND INTENSITY OF ROI
roi_avg_intensity = np.mean(roi_gray)
#3. BASED ON THAT, SEE IF ROI IS AN OBSTACLE OR NOT
if roi_avg_intensity > cut_off_point:
# if box_num < 200:
# print("roi_avg_intensity:", roi_avg_intensity)
cv2.rectangle(edge, (j,i), (j+int(boxsize), i+int(boxsize)),(128,128,2))
box_num += 1
cv2.imwrite('./a.jpg', edge)
plt.imshow(edge, cmap='gray')
plt.show()

more

I commented out the ret, thresh = cv2.threshold(roomimg, 127, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C) line, but I still get the same result. Could you please show me the code you are using? Thanks.

( 2018-03-30 09:06:57 -0500 )edit

Alright, I've updated the answer above.

( 2018-04-02 01:32:43 -0500 )edit

Official site

GitHub

Wiki

Documentation