Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Need to mask the region of interest from RGB 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) cv2.imshow("Thresholded",thresh) # 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] print(m)

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) print(x) print(y) print(w) print(h) rr, cc = rectangle(start=(y, y+h), end=(x, x+w-1))

print(rr) mask[rr, cc] = False

Apply the mask and display the result

im[mask] = 0 cv2.imshow(im) cv2.waitKey(10)

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

File "C:/Users/Shrouti/PycharmProjects/homography/Scorecard.py", line 39, in <module> mask[rr, cc] = False IndexError: index 256 is out of bounds for axis 1 with size 256

click to hide/show revision 2
None

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

berak gravatar image

Need to mask the region of interest from RGB 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)

cv2.COLOR_BGR2GRAY)

        # blur the image

blur = cv2.blur(gray, (3, 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) cv2.imshow("Thresholded",thresh) # 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] print(m)

print(m) # draw contours and hull points

points # for i in range(len(contours)):

range(len(contours)): color_contours = (0, 255, 0) # color for contours color = (255, 255, 255) # color for convex hull

hull # Know the coordinates of the bounding box of the ROI

ROI x, y, w, h = cv2.boundingRect(m) print(x) print(y) print(w) print(h) rr, cc = rectangle(start=(y, y+h), end=(x, x+w-1))

x+w-1)) print(rr) mask[rr, cc] = False

False # Apply the mask and display the result

result im[mask] = 0 cv2.imshow(im) cv2.waitKey(10)

cv2.waitKey(10)

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

File "C:/Users/Shrouti/PycharmProjects/homography/Scorecard.py", line 39, in <module>
    mask[rr, cc] = False
IndexError: index 256 is out of bounds for axis 1 with size 256

256