Ask Your Question
0

I am trying to detect largest tile on the floor.

asked 2017-12-07 15:58:10 -0500

I am trying to detect the largest tile on the floor as shown in the link given below and highlight it with a green rectangle. Additionally, if the file is obstructed with an object, then this should not be highlighted.

Image - https://www.accessfloorsystems.com/me...

Code: - https://pastebin.com/MXQ8ai1e

# import the necessary packages
import imutils
from skimage import exposure
#import exposure
import numpy as np
import argparse
import cv2


# load the query image, compute the ratio of the old height
# to the new height, clone it, and resize it
image = cv2.imread('gb.jpg')
ratio = image.shape[0] / 300.0
orig = image.copy()
image = imutils.resize(image, height = 300)

# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)

# find contours in the edged image, keep only the largest
# ones, and initialize our screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None

# loop over our contours
for c in cnts:
    # approximate the contour
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)

    # if our approximated contour has four points, then
    # we can assume that we have found our screen
    if len(approx) == 4:
        screenCnt = approx
        break


cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("Game Boy Screen", image)
cv2.waitKey(0)
edit retag flag offensive close merge delete

Comments

And your issue with the current code is? Please be clear what is going wrong...

StevenPuttemans gravatar imageStevenPuttemans ( 2017-12-08 04:10:31 -0500 )edit

@nigamankit7. There is something missing.Either cv2.rectangle or cv2.boundingRect.

supra56 gravatar imagesupra56 ( 2017-12-08 05:32:22 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-08 06:52:35 -0500

moHe gravatar image

Your sample code is too complicated as I've seen.

Here is my easy code, may this can help you:)

import cv2
import numpy as np
import matplotlib.pyplot as plt


img = cv2.imread("./floor.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_bl = cv2.bilateralFilter(gray, 9, 75, 75)
ret, thr = cv2.threshold(gray_bl, 200, 255, cv2.THRESH_BINARY_INV)
canvas = np.ones((700, 700), dtype=np.uint8) * 255
canvas[50:50+thr.shape[0], 50:50+thr.shape[1]] = thr
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 15))
dilated = cv2.dilate(canvas, kernel)
_, contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
dilated_rgb = cv2.cvtColor(dilated, cv2.COLOR_GRAY2RGB)
cnt_sorted = sorted(contours, key=lambda x: cv2.contourArea(x))
cnt_sorted.pop()
img_drawn = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in [range(len(cnt_sorted))[-1]]:
    cv2.drawContours(dilated_rgb, cnt_sorted, i, (0, 255, 0), thickness=5)
    plt.imshow(dilated_rgb)
    plt.show()
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-12-07 15:58:10 -0500

Seen: 448 times

Last updated: Dec 08 '17