pythonopencvmaskconvex

Concave mask to convex


I'm detecting an object in an image and create a mask from the contours. The mask is then dilated and smoothed. For example, from an image like this:

chair

I end up with a mask like this:

chair mask

When I crop the image (chair) using the mask, I lose the (background) information between the legs of the chair. To get around this, I want to turn this concave mask into a convex one. For example, into this (created in Photoshop):

convex chair mask

How can I do that?:

The code (show_mask.py):

import sys
from pathlib import Path
from helpers_cv2 import *
import cv2
import numpy

img_path = Path(sys.argv[1])

img = cmyk_to_bgr(str(img_path))

threshed = threshold(img, 240, type=cv2.THRESH_BINARY_INV)
contours = find_contours(threshed)

mask        = mask_from_contours(img, contours)
mask_smooth = smooth_mask(mask, 51)
mask_dilate = dilate_mask(mask_smooth, 51)
mask_smooth = smooth_mask(mask_dilate, 51)

cv2.imshow("img", img)
cv2.imshow("mask_smooth", mask_smooth)

cv2.waitKey(0)
cv2.destroyAllWindows()

helpers_cv2.py:

import os
from pathlib import Path
import math
import cv2
import numpy
from PIL import Image
from PIL import ImageCms
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

cwd = os.path.dirname(os.path.abspath(__file__))

def cmyk_to_bgr(cmyk_img):
    img = Image.open(cmyk_img)
    if img.mode == "CMYK":
        img = ImageCms.profileToProfile(img, "\\Color Profiles\\USWebCoatedSWOP.icc", cwd + "\\Color Profiles\\sRGB_Color_Space_Profile.icm", outputMode="RGB")
    return cv2.cvtColor(numpy.array(img), cv2.COLOR_RGB2BGR)

def threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY):
    if len(img.shape) == 3:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    threshed = cv2.threshold(img, thresh, maxval, type)[1]
    return threshed

def find_contours(img):
    kernel   = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
    morphed  = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    contours = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    return contours[-2]

def max_contour(contours):
    return sorted(contours, key=cv2.contourArea)[-1]

def mask_from_contours(ref_img, contours):
    mask = numpy.zeros(ref_img.shape, numpy.uint8)
    mask = cv2.drawContours(mask, contours, -1, (255,255,255), -1)
    return cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

def dilate_mask(mask, kernel_size=11):
    kernel  = numpy.ones((kernel_size, kernel_size), numpy.uint8)
    dilated = cv2.dilate(mask, kernel, iterations=1)
    return dilated

def smooth_mask(mask, kernel_size=11):
    blurred  = cv2.GaussianBlur(mask, (kernel_size, kernel_size), 0)
    threshed = threshold(blurred)
    return threshed

Full size images, and codes can be found in this repository.


Solution

  • After vising the page ma3oun mentioned, I've managed to make it work. All I had to do was add a few lines to my code:

    contours = find_contours(mask_smooth)
    hull = []
    for i in range(len(contours)):
        hull.append(cv2.convexHull(contours[i], False))
    hull = mask_from_contours(img, hull)
    
    cv2.imshow("hull", hull)
    

    The output image:

    convex hull

    Code (show_convex_hull.py):

    import sys
    from pathlib import Path
    from helpers_cv2 import *
    import cv2
    import numpy
    
    from pprint import pprint
    
    img_path = Path(sys.argv[1])
    
    img = cmyk_to_bgr(str(img_path))
    
    threshed = threshold(img, 240, type=cv2.THRESH_BINARY_INV)
    contours = find_contours(threshed)
    
    mask        = mask_from_contours(img, contours)
    mask_smooth = smooth_mask(mask, 51)
    mask_dilate = dilate_mask(mask_smooth, 51)
    mask_smooth = smooth_mask(mask_dilate, 51)
    
    # convex hull ops
    # find contours from the mask (contours needs to be calculated again because the mask is updated)
    contours = find_contours(mask_smooth)
    # find and store hull points
    hull = []
    for i in range(len(contours)):
        hull.append(cv2.convexHull(contours[i], False))
    # create a mask from hull points
    hull = mask_from_contours(img, hull)
    
    cv2.imshow("hull", hull)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()