Sobel on binary inputs [closed]

asked 2018-03-21 05:25:22 -0600

ThoBar gravatar image

updated 2019-09-13 12:04:38 -0600

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

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-17 05:17:36.759156

Comments

You say you don't want to use find contours, and yet, you want to find contours. You already have all of your edges if you have a binary image, there is no reason to do edge detection, use cv::findContours() or write your own loop. You should be able to get every contour for every class in a single pass over the image.

Der Luftmensch gravatar imageDer Luftmensch ( 2018-03-21 07:43:35 -0600 )edit