Skip to main content

PYTHON / OpenCV Edge Detect

This is going to be a really quick blog post.

Fine Tuned "Ada" Edge Detect result.

IMAGE & WOLFRAM ALPHA APP


Last night, I was again exploring this Wolfram Alpha app on the iPhone, which I paid for extra few dollars to get "Image" upload feature.

I am curious about this kind of thing, so I feed in a photo I took the other day of this very vivid photos of rainbow wings on the street:


I give extra input "Edge Detect" and naturally Wolfram Alpha app can do the Edge Detect thing.


And I also tried "Posterize" filtering.



That is pretty impressive because of these things:

  • It supposed to detect the image and predict what the image is, rarely, but sometimes it can give you the right guess.
  • It gives some Mathematical analytic, sometimes it gives back location of the image 
  • It gives you some options of what "filter" you can apply to the image
  • It gives user a very easy way to process INPUT and OUTPUT.
  • Pretty fast when it works (need fast Internet for uploading and getting the results, etc)
  • The search [IMAGE] allows user to input natural language interpretation. But often failed if it does not understand our request.
So, Wolfram Alpha "brain" is pretty awesome for what it premises, once again I admit.

But I really would like to be able to do this too, and unrestricted to the Wolfram brain / online connection or commands or interpretation.

Open CV => Python 27 => Jupyter => Anaconda

I remember that all this kind of Edge Detect Filter or Image analyzing can be done using Open CV.

Installing OpenCV seems a little bit convoluted somewhat. And after quick browsing I realized that it works "best" on Python 2 only at the moment. Well, I think it is possible to install it for Python 3, but it was not official yet (?). I do want it to work on Python 3 though at some point.

This kind of module installation and compiling that sometimes makes non-programmer getting into programming afraid. It does get a lot easier these days with Anaconda bundle and PIP etc. It still requires users to be familiar with command prompt / terminal commands....

Well, seriously it cannot be helped for now, if you are patient, sometimes you can go far.

Anyway, I did manage to install Python 2 for my Anaconda that comes with Python 3. The step by steps are like this:

1) Python 2 and 3 installed correctly
I need to make sure I have Python 2 for Anaconda. I use Conda command and following the help documentation.

NOTE:
I am using Anaconda 3, bundled with Python and all kind of pre-installed numpy module, etc. I was not familiar with Anaconda until only last week. But I found it to be quite useful for managing module and environment.

2) Testing installation
All well and you should be able to have "environment" with Python 2 installed, and Jupyter Notebook will run. I even manage to pip install wordcloud with Python 2 now installed and ready to run.

3) Installing Open CV for Python 2

This is a little bit confusing but you basically need to get the Open CV package. The EXE is not the normal installation, you need to copy a file into the site-package (Python). 

This blog post gives a simpler direction than the documentation at opencv.org

I "installed" mine like this: (simply by copying that file called "cv2.pyd")
copy C:\opencv\build\python\2.7\x64\cv2.pyd C:\Users\Jimmy\Anaconda3\envs\py27\Lib\site-packages


It's really dumb process, but all I want is the opencv module to load in Python.

4) Once that particular file copied, we can actually use the module.

UPDATE:
Found this blog post on how to create new environment for Anaconda, with Python 3 and opencv3.
https://rivercitylabs.org/up-and-running-with-opencv3-and-python-3-anaconda-edition/

EDGE DETECT USING OPEN CV


Quick Google for Open CV and Python "Edge Detect" lead me to this:
http://docs.opencv.org/trunk/da/d22/tutorial_py_canny.html#gsc.tab=0


By changing the code slightly, so that the input is pointing to the correct path of the "original image" I can get this result below:
This somewhat takes a while to calculate probably because of the resolution of original image? 
 It took Jupyter Notebook a few minutes to give me back the result above, I thought it was frozen at first, however I can get the result and quite happy with it for now.


[CORRECTIONS]
Regarding the "slowness" apparently I forgot to add this line while inside Jupyter:
%matplotlib inline

That is causing some kind of weird slowness process running in the background, after few minutes, I am getting the result as a pop up window. It should not be like this. After adding that "magic" %matplotlib inline, this opencv python script gives me a result like super fast and I can use the "interact" of Jupyter.



I will try to apply this to a sequence of images, maybe animation and make a video.
[/CORRECTIONS]

TO PROGRAMMING VS TO WOLFRAM

Ok, now I am sure a LOT of other things can be done using Open CV.

To have edge detection work inside Jupyter with simple interaction is quite big for me, because normally creating interactive interface is never that simple. Even though it is still trivial and all I did was sourcing the script, modifying it, and maybe combine it with another script.

The pre-made sample code or example from other programmers are really great to get one started, until to certain difficulty level depending on how far we like to explore the module. Not sure if that kind of complexity worth pursuing. Maybe it depends on what we like to achieve.

That makes me think a lot: whether one needs to execute the idea of programming slowly from scratch, or just using other people codes (with slight modification), or simply using apps that allow you to do limited things, but faster?

My answer and recommendation at the moment is: Try all of those!

Of course, being able to write script that does X job, means that I can REUSE and completely make the code to do a lot more, like for example BATCH EDGE DETECT and UNLIMITED usage.

(I am trying to think programmings beyond trivial usages.)

Sure, Wolfram Alpha is "fantastic" in a way it makes common users able to use simple codes to achieve amazing result or even by just querying it using natural language. Sometimes Wolfram can feel like programming, sometimes it feels like sandboxing from premade data input, but sometimes more, but still Wolfram Alpha is somebody else's.

The premise is big, but with limitation.

Something one will think deeply from now... This is as difficult as deciding to build a house ourselves (with helps from here and there) VS build house by other people. I think everyone should try both. Maybe, a different analogy is like buying a LEGO and building it with all the provided raw materials, or buying a LEGO that is more or less finished?

Comments

Popular posts from this blog

PYTHON PROCESSING / It only really begins ...

Back into Processing again, with Python! While Daniel Shiffman is continuously inspiring us with his CODING TRAIN series on YouTube, using Processing with Java and Javascript language, I decided to free my mind and trying to really do something using Processing and Python language. Installing Python language version on Processing 3 is easy enough, just first download the latest Processing and install the Python language mode via Add Mode button. Other link that might help: https://github.com/jdf/processing.py http://py.processing.org/tutorials/ BLANK MODE As soon as Processing Python Mode, opens up and running I am presented with a  blank environment. Suddenly I recalled my journey in learning programming from zero until now... With Python, outside Processing, commonly people will be introduced to Python IDE or IDLE environment. Something that looks like Console Window or Command Prompt, where we type single liners command. Python Command Line and IDE normally have t

PYTHON / OpenCV, Recreate Uncanny Manga - Anime Style

Can you tell what it is? Computer Vision. Yesterday, I spend almost whole day exploring this opencv module using Python. What I discovered was revealing. Even at the very basic level, I could produce some interesting Image and Video manipulation using all the code collected from documentation and many, many blog tutorials. If you are a total noob like me, I am still getting used to knowing that the CV in OpenCV means Computer Vision! Actuallly, I recalled that I did try to get into OpenCV few years back ago, when I knew no Python and when Python opencv module was probably still early. It was all C++ code and it was a little bit too hard for me. I read a couple of books about opencv at the library, I did not understand a single thing. That was back then. Today, for strange reason, with a bit of knowledge of Python, I can go a little further. EDGE DETECT IN OPENCV Me holding you know what. What leads me this far is my curiosity on how we can replicate Wolfram Langu

WOLFRAM / Making Text With Rainbow Color

Continuing with my Wolfram Mathematica Trial Experience... I watched and went through some more Mathematica introduction videos, read lots of Mathematica documentation and also going through the Wolfram Lab Online again a few times. There are some major learning curves and Mathematica is a lot different from normal programming language. Sometimes there is a lot of interesting "shortcuts", say like: FindFaces[] , WordCloud[] . Sometimes I got a little confused on how one can do iterations. Normally FOR LOOP concept is introduced early, but in Wolfram, because everything is EXPRESSIONS and ENTITY (like OBJECTS), sometimes it gets quite quirky. Mind you I am still in the first impression and having to look at many tutorials. Lots of NEAT EXAMPLES from documentation, but sometimes I got lost. I found Wolfram to be really awesome with LIST and generating list. It's almost too easy when it works visually. I cannot explain them with my own words yet, but there are