I am trying to get coordinates of each room for a map/navigation app. the code below finds every corner and the code even further down finds the centre of the room. i don't know how to combine the two so that i get arrays of coordinates of corners in each room separately.
#finds every corner
img = cv2.imread('out.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,5,3,0.04)
ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(50,50),(-1,-1),criteria)
img[dst>0.1*dst.max()]=[0,0,255]
#find centre of each room
IMAGE_NAME = 'out.png'
#Remove Small Items
im_gray = cv2.imread(IMAGE_NAME, cv2.IMREAD_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
#find all your connected components
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(im_bw, connectivity=8)
#connectedComponentswithStats yields every seperated component with information on each of them, such as size
#the following part is just taking out the background which is also considered a component, but most of the time we don't want that.
sizes = stats[1:, -1]; nb_components = nb_components - 1
CodePudding user response:
Here is an example based on findContours using opencv_contrib_python-4.5.5
:
import cv2 as cv
img = cv.imread("UYret.png")
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
print(contours)
cv.drawContours(img, contours, -1, (0, 255, 0), 3)
cv.imwrite("out.png", img)
The selected approximation method will reduce the contour-points to the room corners. So contours
returns a list of arrays, each of them containing the coordinates of the corners of one of the rooms:
(
...
array([[[9271, 7560]],
[[9271, 7795]],
[[9782, 7795]],
[[9782, 7560]]], dtype=int32),
...
)
In out.png
you can see the rooms that have been found coloured in green. The shapes of the "room" outside are detected, too. You can easily filter out these surrounding rooms using the structure that is returned in hierarchy
(probably you are only interested in leaves) or by assuming that a real room has limited size.