Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

Output image:

https://imgur.com/a/oWgVP

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()

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

Output image:

https://imgur.com/a/oWgVP

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()

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)
            #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()