Need to mask the region of interest from RGB image

asked 2020-03-17 01:20:15 -0500

titli gravatar image

updated 2020-03-17 03:29:00 -0500

berak gravatar image

image description

My job is to mask the hand gestures from the RGB image into a binary image. Only the hand gesture should be shown in the resultant image. I have coded so far

import cv2
import numpy as np
from PIL import Image, ImageCms
from skimage.draw import rectangle
im = image = cv2.imread("E:\JU_V2_DIGIT\RGB_Crop\RGB_P3_G7_5.png")
mask = np.ones(shape=im.shape[0:2], dtype="bool")
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

        # blur the image
blur = cv2.blur(gray, (3, 3))

        # binary thresholding of the image
ret, thresh = cv2.threshold(blur, 210, 255, cv2.THRESH_BINARY)
        # ret, thresh = cv2.threshold(gray, 127, 255,0)
        # find contours
        # contours, hierarchy = cv2.findContours(thresh,2,1)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        # cc
cnt = sorted(contours, key=cv2.contourArea, reverse=True)
        # ROI will be object with biggest contour
m = cnt[0]

# draw contours and hull points
# for i in range(len(contours)):
color_contours = (0, 255, 0)  # color for contours
color = (255, 255, 255)  # color for convex hull

# Know the coordinates of the bounding box of the ROI
x, y, w, h = cv2.boundingRect(m)
rr, cc = rectangle(start=(y, y+h), end=(x, x+w-1))

mask[rr, cc] = False
# Apply the mask and display the result
im[mask] = 0

It shows the following error and also the result is not got.

File "C:/Users/Shrouti/PycharmProjects/homography/", line 39, in <module>
    mask[rr, cc] = False
IndexError: index 256 is out of bounds for axis 1 with size 256
edit retag flag offensive close merge delete


Why do you needed namespace from skimage.draw import rectangle The opencv do have api for cv2.rectangle. If you do, you not will getting an error.

supra56 gravatar imagesupra56 ( 2020-03-17 03:54:19 -0500 )edit

I am a beginner in Python. Please will you assist me with how to do this?

titli gravatar imagetitli ( 2020-03-17 09:21:34 -0500 )edit

I'm using Raspberry pi 4B, Debian Buster, Linux.

supra56 gravatar imagesupra56 ( 2020-03-17 20:51:12 -0500 )edit