Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Sobel on binary inputs

Hey guys!

Context: I'm working a lot with segmentation images (HxWx1 with class values as uint8) and I'm trying to extract classwise contour images. So for each class c I want to get a boolean image of the contour pixels of that class. For fast implementation I chose the sobel operator over contour finding methods due to performance.

I got some code running but it seems quite nasty due to two factors 1) sobel does not support boolean inputs 2) sobel does not provide edges in all directions with a single function call

here's my code:

import numpy as np
import cv2

def binary_sobel(binary_mask):
    # cv2.Sobel does not work with binaries so we use
    # an uint8 array as an efficient substitution
    temp_mask = np.uint8(binary_mask)
    edges_horz = cv2.Sobel(temp_mask, cv2.CV_64F, 1, 0)
    edges_vert = cv2.Sobel(temp_mask, cv2.CV_64F, 0, 1)

    binary_edge_mask = np.zeros(binary_mask.shape, 'bool')
    binary_edge_mask[np.where(edges_horz != 0)] = 1
    binary_edge_mask[np.where(edges_vert != 0)] = 1

    return binary_edge_mask

The matlab implementation is just binary_edge_mask = edge('Sobel', binary_mask) which is kind of nice. Do I miss some options / other ways?

Best, ThoBar

Sobel on binary inputs

Hey guys!

Context: I'm working a lot with segmentation images (HxWx1 with class values as uint8) and I'm trying to extract classwise contour images. So for each class c I want to get a boolean image of the contour pixels of that class. For fast implementation I chose the sobel operator over contour finding methods due to performance.

I got some code running but it seems quite nasty due to two factors 1) sobel does not support boolean inputs 2) sobel does not provide edges in all directions with a single function call

here's my code:

import numpy as np
import cv2

def binary_sobel(binary_mask):
    # cv2.Sobel does not work with binaries so we use
    # an uint8 array as an efficient substitution
    temp_mask = np.uint8(binary_mask)
    edges_horz = cv2.Sobel(temp_mask, cv2.CV_64F, 1, 0)
    edges_vert = cv2.Sobel(temp_mask, cv2.CV_64F, 0, 1)

    binary_edge_mask = np.zeros(binary_mask.shape, 'bool')
    binary_edge_mask[np.where(edges_horz != 0)] = 1
    binary_edge_mask[np.where(edges_vert != 0)] = 1

    return binary_edge_mask

The matlab implementation is just binary_edge_mask = edge('Sobel', binary_mask) which is kind of nice. Do I miss some options / other ways?

I'm running python3 with opencv 3.3.1

Best, ThoBar

Sobel on binary inputs

Hey guys!

Context: I'm working a lot with segmentation images (HxWx1 with class values as uint8) and I'm trying to extract classwise contour images. So for each class c I want to get a boolean image of the contour pixels of that class. For fast implementation I chose the sobel operator over contour finding methods due to performance.

I got some code running but it seems quite nasty due to two factors 1) sobel does not support boolean inputs 2) sobel does not provide edges in all directions with a single function call

here's my code:

import numpy as np
import cv2

def binary_sobel(binary_mask):
    # cv2.Sobel does not work with binaries so we use
    # an uint8 array as an efficient substitution
    temp_mask = np.uint8(binary_mask)
    edges_horz = cv2.Sobel(temp_mask, cv2.CV_64F, 1, 0)
    edges_vert = cv2.Sobel(temp_mask, cv2.CV_64F, 0, 1)

    binary_edge_mask = np.zeros(binary_mask.shape, 'bool')
    binary_edge_mask[np.where(edges_horz != 0)] = 1
    binary_edge_mask[np.where(edges_vert != 0)] = 1

    return binary_edge_mask

The matlab implementation is just binary_edge_mask = edge('Sobel', binary_mask) which is kind of nice. Do I miss some options / other ways?

I'm running python3 with opencv 3.3.1

Best, ThoBar