Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

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