# OpenCV update, Heaviside Step Function, deriving parametric functions of shapes, Sobel filter

**URL:** <https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931>\
**Category:** Libraries\
**Created:** [June 16, 2020, 9:59pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931 "2020-06-16T21:59:30Z")\
**Posts on this page:** 15\
**Page:** 3

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 11:53am UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/41 "2020-08-19T11:53:14Z")

</div>

just looking online and it does seem to indicate that my approach is correct at least the result seem to match the online quality

the traditional lena picture is expected to give this output

> **[bamcis-io/SobelFilter](https://github.com/bamcis-io/SobelFilter)**
>
> A Sobel Edge Detection Filter written in OpenCL and C++. - bamcis-io/SobelFilter

  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1336e350c35d20a51b05e8143260a851ae6b59c4.jpeg)

can you post the output of openCv please so we can compare?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 19, 2020, 12:05pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/42 "2020-08-19T12:05:47Z")

</div>

> [@paulgoux](#):
>
> similar to this?

The best approximation I got compared to yours is this.  
( Of course click to amplify)  
The last exact same picture you posted is from the tutorial I linked.  
You have to invert it.

BTW .“I meant setting the multiplier to 1 not zero”  
Of course, I played with all the parameters.

 ![house1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f549a90f63d798b500a3db3ecb44ea3b9c2f188b.jpeg) ![house2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cdedc6f558bba3c0c31d68e20d99a02afe196a0a.jpeg)

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 12:44pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/43 "2020-08-19T12:44:32Z")

</div>

the first one is very close, perhaps a bit more noise, and the second one is just extracting the vertical lines.

In any case I’m quite pleased with the result.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 19, 2020, 2:03pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/44 "2020-08-19T14:03:20Z")

</div>

> [@paulgoux](#):
>
> the first one is very close

Hehe. The first one is yours !! You asked for a comparison.  
I just changed the offset a little. 😁 😁

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 2:25pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/45 "2020-08-19T14:25:38Z")

</div>

Then it seems openCv should be able to produce the same result, however the bottom image is missing the horizontal edges.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 19, 2020, 3:42pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/46 "2020-08-19T15:42:21Z")

</div>

> [@paulgoux](#):
>
> Then it seems openCv should be able to produce the same result

With sobel filter I doubt it. The lines are vertical because I’ve choose the derivative parameters that way to give some contrast at least. See [here](https://www.tutorialspoint.com/opencv/opencv_sobel_operator.htm) just some lines and pictures. You can change the derivative parameter from 0 to 2. I’ve tried also the ScharrEdges and the CannyEdges but none of them gives an outcome as nice as yours. I’m afraid you have to accept the credit.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 3:59pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/47 "2020-08-19T15:59:06Z")

</div>

one last thing I forgot

```auto
sobel.pixels[p] = color(255-k+offset);
sobel.pixels[p] = color(255-(k-b2));
sobel.pixels[p] = color(b2-k);

```

these correspond to the 3 outputs of the car image not sure they are in order, but one gives clean lines, the next clean lines with some shading and the last combines the derived lines with the brightness of the original image for a pencil colored look.

Feel free to use the repo, hopefully I’ll make it into a library later.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 19, 2020, 4:04pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/48 "2020-08-19T16:04:35Z")

</div>

> [@paulgoux](#):
>
> for a pencil colored look.

That’s the way to go for a library. Looking forward.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 24, 2020, 9:26am UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/49 "2020-08-24T09:26:23Z")

</div>

I still didn’t give up.  
[Bryan Chung](http://www.magicandlove.com/blog/) has made a CVImage class that makes it possible to use the latest OpenCV library.So now you can use full range of parameters in the sobel function. With this I could give the house picture some more details now, but still not as your outcome.

 ![sobel_new](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7d6a444ad5cb73377e50d3c2d8d33299236d677f.png)  
If I remember well you wrote somewhere that you are writing a neural network to do character recognition. The newer openCV makes YOLO (You only look once) detection possible in Processing.  
See here a video of Real-Time Object Detection with openCV

[![](https://img.youtube.com/vi/eeIEH2wjvhg/maxresdefault.jpg "YOLO Object Detection with OpenCV") ](https://www.youtube.com/watch?v=eeIEH2wjvhg)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 24, 2020, 10:07am UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/50 "2020-08-24T10:07:17Z")

</div>

For those whom interested:  
To use the latest openCV library (4.4.0 at the time of this post) you need to paste the jar’s and dll in a folder “code” within your sketch folder.  
I’ve prepared a zip of this folder with all the jar’s build and dll. You can download it [here](https://drive.google.com/file/d/1luQ0m76r1V6u5dqu6uyS8rHKBn_r_JL-/view?usp=sharing).  
I also made a new repository [here,](https://github.com/EmmanuelPil/OpenCV-code-and-libraries-for-Processing) where I plan to post various codes that can’t be done with the older openCV-processing version. An example is for example the warpAffine function.

 ![warp](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4e3f68c452296db273beee2ddd62d25c8a056d20.jpeg)

> [@noel](#):
>
> I need to retrieve the hierarchy array (outer (parent) and inner (child) shapes) of the contours. The methods of OpenCV\_processing lib do not give access to that,

This I also solved and posted it on my repo [here](https://github.com/EmmanuelPil/OpenCV-code-and-libraries-for-Processing/blob/master/hierarchy). It uses the same image and thus the same result as on the openCV [tutorial](https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html) about hierarchy. This is handy when you convert contours into shapes and you want holes in them.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 24, 2020, 12:43pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/51 "2020-08-24T12:43:54Z")

</div>

Just adding thresholding at the moment this produces an image which is binary based on the output of the sobel, with the aim of using histeresis to thin the lines too and produce canny edge detection, still have to read some more into the process, but the thresholding currently works this combined with my previous work should provide good contours which you can trim dependent on size. I can also speed it up by only scanning for walls which will allow processing of images greater than the current resolution.

Will post results later.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [September 24, 2020, 1:45am UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/52 "2020-09-24T01:45:17Z")

</div>

The program has now been updated with shaders, so everything now works using gpu at 60fps.

Only issue I’m trying to overcome is the speed is fine so long as the canvas is manipulated without posting to a PImage or creating an image using get afterwards. If I do then the speed is reduced by a factor of 2. is there an alternative method to convert canvas data to PImage.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 28, 2020, 3:29pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/53 "2020-10-28T15:29:06Z")

</div>

repo updated

> **[paulgoux/canny](https://github.com/paulgoux/canny)**
>
> an attempt at canny currently working on shader version - paulgoux/canny

chain together image operations and create a workflow like this.

```auto
String []wf = {"gaussianS(2.0,5)","sobel(10.0)","sobelMax2(200.0)","canny(5,130.0,50)"};

```

call the workflow like this.

```auto
img.displayWF(wf);

```

click through workflow to see output of operations.

```auto
void displayWF(String []s){
    logic();
    workflow(s);
    if(imagesWF.size()>0)
    image(imagesWF.get(counter),x,y);
    
  };

```

I’m currently pretty happy with the cpu version of canny.  
blur  
 ![Screen Shot 10-28-20 at 03.12 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bca50a5ac8233488dd4d6ad53353e1e9c24b6a12.jpeg)  
sobel  
 ![Screen Shot 10-28-20 at 03.12 PM 001](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a3540eed4ff9107e03df46ce378bcb3d81620d68.jpeg)  
surpress max  
 ![Screen Shot 10-28-20 at 03.12 PM 002](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4809f71869a81a43c986788b0a5d160996392d74.jpeg)  
finish  
 ![Screen Shot 10-28-20 at 03.12 PM 003](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4379cac0dd3e67f8d0f206058cd630b5fa3733af.jpeg)

there’s always a few bits here and there, I guess the next steps would be dilation and erosion.  
This version currently doesn’t have the contour tracing, however that step is complete it just has to be added to the code, and then edges can be extracted from the picture.

Working on a shader version to speed things up a bit but currently running into some problems extracting the sobel gradient required for the max surpression, will update when fixed.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 28, 2020, 4:24pm UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/54 "2020-10-28T16:24:43Z")

</div>

You are doing a really nice job with these filters.  
I just tested them all out;  
I hope to see a library.

 ![images](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a8c14934a85fa35fdc85a2252eeb82b45f4a9c11.jpeg)

---

<div class="post-metadata">

**Author:** ![niuuin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/niuuin/32/7517_2.png) [@niuuin](https://discourse.processing.org/u/niuuin)\
**Post date:** [April 20, 2021, 5:46am UTC](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931/55 "2021-04-20T05:46:17Z")

</div>

> [@noel](#):
>
> 形状编辑器，其中包括地理库

Thank you for your help!

[Previous page](https://discourse.processing.org/t/opencv-update-heaviside-step-function-deriving-parametric-functions-of-shapes-sobel-filter/21931.md?page=2)
