Ask Your Question
1

Python - Trying to Find Average Intensity of a ROI

asked 2018-03-27 08:15:32 -0600

Stephane gravatar image

updated 2018-03-29 01:17:14 -0600

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)
            #4. ADD TO ARRAY

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


roomimg = cv2.imread("/Users/2020shatgiskessell/Desktop/medium2.jpg")

#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)
            #4. ADD TO ARRAY


cv2.imshow('Image Edges', edge)

if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

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?

moHe gravatar imagemoHe ( 2018-03-28 09:20:03 -0600 )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.

Stephane gravatar imageStephane ( 2018-03-29 01:18:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2018-03-29 01:50:33 -0600

moHe gravatar image

updated 2018-04-02 01:35:03 -0600

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

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


roomimg = cv2.imread("./TXw1CdF.jpg")

# 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
            #4. ADD TO ARRAY
cv2.imwrite('./a.jpg', edge)
plt.imshow(edge, cmap='gray')
plt.show()
edit flag offensive delete link more

Comments

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.

Stephane gravatar imageStephane ( 2018-03-30 09:06:57 -0600 )edit

Alright, I've updated the answer above.

moHe gravatar imagemoHe ( 2018-04-02 01:32:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-27 08:15:32 -0600

Seen: 7,037 times

Last updated: Apr 02 '18