# Canny differences between Python & Android

**URL:** <https://forum.opencv.org/t/canny-differences-between-python-android/16000>\
**Category:** Android/Java\
**Tags:** programming\
**Created:** [December 28, 2023, 5:25pm UTC](https://forum.opencv.org/t/canny-differences-between-python-android/16000 "2023-12-28T17:25:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gareth\_Hutchins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/gareth_hutchins/32/9421_2.png) [@Gareth\_Hutchins](https://forum.opencv.org/u/Gareth_Hutchins)\
**Post date:** [December 28, 2023, 5:25pm UTC](https://forum.opencv.org/t/canny-differences-between-python-android/16000/1 "2023-12-28T17:25:42Z")

</div>

I’m trying to use Canny Edge detection in an Android App.

I tested in Python first using the below steps:

gray = cv.cvtColor(img, cv.COLOR\_RGB2GRAY)

kernel = np.ones((5,5),np.uint8)

dilate = cv.morphologyEx(gray, cv.MORPH\_CLOSE, kernel, iterations= 30)

blurred = cv.GaussianBlur(dilate, (5, 5), 0)

edges = cv.Canny(blurred, 50, 150)

Which resulted in a solid outlined image of a piece of paper.

However, when I run the similar steps through on the Android App in Kotlin using this code

val tmp = Mat(tempImage._width_ , tempImage._height_ , _CV\_8UC1_ )  
Utils.bitmapToMat(tempImage, tmp)  
//Convert to Grey  
cvtColor(tmp, tmp, _COLOR\_RGB2GRAY_ )  
//dilate  
val kernel = Mat.ones(5, 5, CvType._CV\_8U_ )  
val point = Point(-1.0,-1.0)  
morphologyEx(tmp, tmp, _MORPH\_CLOSE_ , kernel, point, 30)  
//Blur  
GaussianBlur(tmp, tmp, Size(5.0, 5.0), 0.0)  
//Canny  
Canny(tmp,tmp,50.0,150.0)

The resulting image is as follows  
 ![Android](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/52052eaba41725fa7c5cefd893f5dbdb9ea926de.png)  
Which is a dashed outline of a page instead of a solid one

The images up to the Canny Step are identical across the two.

Python and Android are both using 4.8

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 29, 2023, 6:44am UTC](https://forum.opencv.org/t/canny-differences-between-python-android/16000/2 "2023-12-29T06:44:16Z")

</div>

> [@Gareth\_Hutchins](#):
>
> val tmp = Mat(tempImage._width_ , tempImage._height_ , _CV\_8UC1_ )

W/H swapped, should be:

```
Mat(H,W,type)

```

> [@Gareth\_Hutchins](#):
>
> cvtColor(tmp, tmp, _COLOR\_RGB2GRAY_ )

also, this would throw an exception, if tmp _really_ was CV\_8U only (Utils.bitmapToMat probably reallocated it !)

---

<div class="post-metadata">

**Author:** ![Gareth\_Hutchins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/gareth_hutchins/32/9421_2.png) [@Gareth\_Hutchins](https://forum.opencv.org/u/Gareth_Hutchins)\
**Post date:** [December 29, 2023, 8:54am UTC](https://forum.opencv.org/t/canny-differences-between-python-android/16000/3 "2023-12-29T08:54:21Z")

</div>

Thanks, it didn’t make a difference.  
Here are the images comparisons step by step

 ![Comparisons](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/af403d83b9f47704620f12e3b8590fd9d13f581c.jpeg)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [December 29, 2023, 10:36am UTC](https://forum.opencv.org/t/canny-differences-between-python-android/16000/4 "2023-12-29T10:36:23Z")

</div>

[MRE](https://stackoverflow.com/help/minimal-reproducible-example) required. screenshot of a bunch of plots may be tolerable illustration but it’s neither input nor output data.

the axis labels in your small screenshot of that plot are barely readable. I can barely make out that your picture is sized about 3000 by 4000 pixels.

your code does not contain _any_ means to display or save your data. since you didn’t disclose how you made your picture, we are left to guess.

I’m guessing you used imshow(), and you’re wondering why the single-pixel lines in your huge resolution image don’t show up completely on your screen, which is smaller than the picture.

yes, that’s a deficit in imshow(). it’s using nearest neighbor resampling. that’s the cheapest option. when downscaling, that will entirely drop some pixel rows and columns of the image to be displayed.

if you’d save the image (NOT a screenshot), then look at it, and zoom in, you’ll see it’s all there.

the problem itself, as well as the difficulty in debugging your problem, are caused by you sharing and looking at small representations of the actual data.
