Skip to content

[GSOC] Vectorization of bitmaps #735

Closed

Description

@ankit-kumar-dwivedi

I'm Ankit Dwivedi a CSE Student. I'm a contributor at Synfig. I was a part of NjackWOC 18 at Synfig.
I would like to work for the project https://synfig-docs-dev.readthedocs.io/en/latest/gsoc/2019/ideas.html#vectorization-of-bitmaps
As this is something I personally feel is an important feature that Synfig must-have. Even when I'm working for graphic design I encounter a situation where I have to vectorize the raster image to increase the quality and reduce the size.
On my initial research, I found that potrace can be a good option. Inkscape uses potrace to convert the raster image to vector and I have tried that feature. I would love to have any suggestions and comments from the project mentor @blackwarthog and @morevnaproject
Hope to have an exciting summer 😊

--

Watch the progress here - https://github.com/ankit-kumar-dwivedi/synfig/commits/vectorization
Watch milestone board here - https://github.com/synfig/synfig/milestone/5

Activity

  1. morevnaproject commented on Mar 2, 2019

    @morevnaproject
    Member

    Just want to put a project description here:

    Allow to convert bitmap images to vector data with a single command, through integration with Potrace (http://potrace.sourceforge.net/).
    Alternative - instead of Potrace, use an algorithm of vectorization from OpenToonz (https://opentoonz.readthedocs.io/ja/latest/drawing_animation_levels.html#converting-raster-drawings-to-vectors), which works better.

    From my point of view, the vectorization algorithm of OpenToonz works better, because it identifies separate lines and outputs strokes (result = many separate Outline Layers). This is good for further editing vectorized image.

    Demo - https://youtu.be/QkpeR72fOzY

    The Potrace tool outputs singles region layer (result = single Region Layer), which is not very comfortable for editing. On the other hand, I believe integration with Potrace might be easier to implement (but I might be wrong).

  2. ankit-kumar-dwivedi commented on Mar 3, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    The Potrace tool outputs singles region layer (result = single Region Layer), which is not very comfortable for editing. On the other hand, I believe integration with Potrace might be easier to implement (but I might be wrong).

    Well.. yes it seems easy to integrate potrace with synfig that's because it is documented and being used by several other projects. However, on trying myself, I found opentoonz is better and have more accuracy and options.
    On finding about the algorithm they use I was unable to directly find any algorithm mentioned anywhere which they use for the raster to vector conversion. Sadly I found no developer documentation for opentoonz :( This means I need to analyze the process and identify the necessary steps which are required so I will let you know after I investigate further.

    @morevnaproject Also can you please let me know how much work to be done before gsoc start? I am asking this in regards to point no 2 under the gsoc application faq.

    Show evidence of past experience or achievements related to the proposal.

    What is the priority of this project?
    I hope I have not chosen the least priority project 😓

  3. morevnaproject commented on Mar 3, 2019

    @morevnaproject
    Member

    On finding about the algorithm they use I was unable to directly find any algorithm mentioned anywhere which they use for the raster to vector conversion. Sadly I found no developer documentation for opentoonz :( This means I need to analyze the process and identify the necessary steps which are required so I will let you know after I investigate further.

    git grep "Convert to Vectors" in opentoonz sources dir gives me:

    ...
    toonz/sources/toonz/mainwindow.cpp:  createMenuLevelAction(MI_ConvertToVectors, tr("Convert to Vectors..."), "");
    ...
    

    So, name of the action is MI_ConvertToVectors. So I did git grep MI_ConvertToVectors and got this:

    stuff/profiles/layouts/rooms/Default/menubar_template.xml:    <command>MI_ConvertToVectors</command>
    stuff/profiles/layouts/rooms/StudioGhibli/menubar_template.xml:    <command>MI_ConvertToVectors</command>
    stuff/profiles/layouts/rooms/StudioGhibli/room4_menubar.xml:    <command>MI_ConvertToVectors</command>
    stuff/profiles/layouts/shortcuts/defopentoonz.ini:MI_ConvertToVectors=
    stuff/profiles/layouts/shortcuts/otadobe.ini:MI_ConvertToVectors=
    stuff/profiles/layouts/shortcuts/otharmony.ini:MI_ConvertToVectors=
    stuff/profiles/layouts/shortcuts/otretas.ini:MI_ConvertToVectors=
    toonz/sources/toonz/castselection.cpp:    // enableCommand(m_browser, MI_ConvertToVectors, &CastBrowser::vectorize);
    toonz/sources/toonz/castviewer.cpp:  // MI_ConvertToVectors se sono stati selezionati solo livelli non vettoriali
    toonz/sources/toonz/castviewer.cpp:    menu->addAction(cm->getAction(MI_ConvertToVectors));
    toonz/sources/toonz/mainwindow.cpp:  createMenuLevelAction(MI_ConvertToVectors, tr("Convert to Vectors..."), "");
    toonz/sources/toonz/menubar.cpp:  addMenuItem(levelsMenu, MI_ConvertToVectors);
    toonz/sources/toonz/menubar.cpp:  addMenuItem(levelMenu, MI_ConvertToVectors);
    toonz/sources/toonz/menubarcommandids.h:#define MI_ConvertToVectors "MI_ConvertToVectors"
    toonz/sources/toonz/vectorizerpopup.cpp:    MI_ConvertToVectors);
    

    So, I believe it is good to start looking in toonz/sources/toonz/vectorizerpopup.cpp.

    Also can you please let me know how much work to be done before gsoc start? I am asking this in regards to point no 2 under the gsoc application faq - "Show evidence of past experience or achievements related to the proposal."

    About achievements related to proposal I would like to see the following:

    • One feature, which is related to adding a new item in Layer's right-click menu. I think "Edit in External Tool" Options #516 is a very good candidate for that.
    • If we will choose to go with OpenToonz, then I ask you to describe in your project proposal the main logic of vectorization code. This shouldn't be a detailed description - just show me an understanding where (in which files/functions) the main logic is coded. ^__^

    What is the priority of this project?
    I hope I have not chosen the least priority project

    The priority of project doesn't plays any role when we choosing who will participate in our GSoC activities. When choosing students we are looking at:

    • How many code contributions student has done to Synfig.
    • How complex those contributions are.
    • How many of his/her contributions are related to his proposal (or does he/she has a previous experience with related topics).

    Also other things we consider:

    • If contributor has experience with other projects.
    • Personal motivation of the student.
  4. added
    GSoC
    Assigned
    on Mar 3, 2019
  5. ankit-kumar-dwivedi commented on Mar 17, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject I have few things to ask regarding opentoonz codebase should I ask here or mail you?

  6. morevnaproject commented on Mar 17, 2019

    @morevnaproject
    Member

    @ankit-kumar-dwivedi It's better to ask here. It's possible @blackwarthog might want to join our discussion. ^__^

  7. ankit-kumar-dwivedi commented on Mar 17, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject okay
    I needed to ask that the function doVectorize (which is calling vCore.vectorize(..some args..) where the actual thing is happening)
    is responsible for raster to vector which is defined in vectorizerpopup.h/cpp
    https://github.com/ankit-kumar-dwivedi/opentoonz/blob/bcc4cf8c2922ff7de541d6eb715f6ff3dde63f86/toonz/sources/toonz/vectorizerpopup.cpp#L212-L226

    and is called in
    https://github.com/ankit-kumar-dwivedi/opentoonz/blob/bcc4cf8c2922ff7de541d6eb715f6ff3dde63f86/toonz/sources/toonz/vectorizerpopup.cpp#L304

    which in turn is called in run()

    then why?? start() is called and not run() when convert button is pressed 😕
    https://github.com/ankit-kumar-dwivedi/opentoonz/blob/bcc4cf8c2922ff7de541d6eb715f6ff3dde63f86/toonz/sources/toonz/vectorizerpopup.cpp#L970

    EDIT : I guess it is related to multithreading, in java also there is run() and start() function I have done multithreading in java not C++ but it's quite similar I guess.

  8. morevnaproject commented on Mar 18, 2019

    @morevnaproject
    Member

    then why?? start() is called and not run() when convert button is pressed

    As you can see from https://github.com/ankit-kumar-dwivedi/opentoonz/blob/bcc4cf8c2922ff7de541d6eb715f6ff3dde63f86/toonz/sources/toonz/vectorizerpopup.h#L265 Vectorizer class is based on QThread class.

    From QT documentation - https://doc.qt.io/qt-5/qthread.html#start -

    void QThread::start(QThread::Priority priority = InheritPriority)
    Begins execution of the thread by calling run().

    ^__^

  9. ankit-kumar-dwivedi commented on Mar 20, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject As I got your mail so I verified that there is a crash in case of using the outlinevectorizer method.

    To compare "opentoonz" and "opentoonz morevna" I installed both on same Windows 10.

    Also, I used the same file. On converting to vector( outlinevectorizer ) using "opentoonz" it ran successfully, however, "opentoonz morevna" crashed. So I can confirm the crash is only in morevna edition and not on original opentoonz :/
    I am comparing both codebase to know what is the possible reason. Is morevna edition using the tnewoutlinevectorizer.cpp ? The crash was supposed to fix by opentoonz/pull/2057

  10. morevnaproject commented on Mar 21, 2019

    @morevnaproject
    Member

    I am comparing both codebase to know what is the possible reason. Is morevna edition using the tnewoutlinevectorizer.cpp ? The crash was supposed to fix by opentoonz/pull/2057

    @ankit-kumar-dwivedi The fix of opentoonz/opentoonz#2057 is included into Morevna Edition 1.3.

    Please consider, that OpenToonz Morevna Edition doesn't includes all features/fixes of official OpenToonz 1.3. Actually, OpenToonz Morevna Edition 1.3 has only features of official OpenToonz 1.2.1 + our additions (Assistants, Color selector, etc.). This might be the reason why this happens.

  11. ankit-kumar-dwivedi commented on Mar 24, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject I have mailed you my pre proposal please suggest any changes and let me know your thoughts on it (^_^)

  12. ankit-kumar-dwivedi commented on Apr 8, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject this feature is intended to be only for gui?

  13. morevnaproject commented on Apr 8, 2019

    @morevnaproject
    Member

    Yes!

  14. ankit-kumar-dwivedi commented on Apr 11, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject the configuration input dialog for opentoonz(centerline) looks like this
    Screenshot from 2019-04-10 19-41-42
    Are we going to create the same options dialog or there are some changes required?

    And in the centerline algorithm, the first step in processing anti-aliased images so I am starting with the pre-processing part when the layer is anti-aliased.
    see toonz/sources/toonzlib/Naa2TlvConverter.cpp
    Will push the changes on a new branch in my repository.
    Will keep you updated on what I am working on [^_^]

  15. 70 remaining items

  16. morevnaproject commented on Jun 14, 2019

    @morevnaproject
    Member

    It would be nice to have progressbar so user can see how the process is going. But this is not critical at the moment. I suggest to add it after you get vectorization working.

  17. ankit-kumar-dwivedi commented on Jun 15, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    The problem of different values is (probably) somewhere else.

    @morevnaproject You will be glad to know this is fixed :)
    also, can you or @blackwarthog help me understand what is this frame in the following lines of code.
    https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/vectorizerparameters.cpp#L234

  18. ankit-kumar-dwivedi commented on Jun 17, 2019

    @ankit-kumar-dwivedi
    MemberAuthor
  19. ankit-kumar-dwivedi commented on Jun 17, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject is there any submission format for the evaluation?

  20. ankit-kumar-dwivedi commented on Jun 18, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject I found that weight in the line https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonz/vectorizerpopup.cpp#L366
    is actually frame which is passed to centerlineconfiguration. Is this frame same as the frame in image below??
    FRAME

    If yes then what will be the synfig equivalent of frame?

    Also according to Toonz documentation

    Thickness Calibration Start: and End: calibrates the vector stroke thickness defined according to the Threshold value; a low value will reduce the stroke thickness preserving its integrity. A different value inserted in the Start / End field determines an animation of the thickness along the length of the level.

    Then why are they using frame to calculate the thickness ratio?
    I apprciate any pointer @blackwarthog @ice0

  21. morevnaproject commented on Jun 19, 2019

    @morevnaproject
    Member

    @ankit-kumar-dwivedi Hi! Let's take a look at this formula from https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/vectorizerparameters.cpp#L234 -

    conf.m_thicknessRatio = (1 - frame) * m_cThicknessRatioFirst + frame * m_cThicknessRatioLast
    

    The meaning of this formula is to take a value from [0;1] segment and find its relative position on another segment [m_cThicknessRatioFirst; m_cThicknessRatioLast].

    FullSizeRender

    frame is a value from [0;1] segment. This is confirmed by code that you referenced -
    https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonz/vectorizerpopup.cpp#L364-L366

    Thickness Calibration Start: and End: calibrates the vector stroke thickness ...; ... A different value inserted in the Start / End field determines an animation of the thickness along the length of the level.

    The last sentence is important. Actually I was not aware that Start / End fields has that meaning.
    I have made some tests - yes, it actually allows to "animate" stroke width when vectorization is done for a sequence of images.

    I will keep thinking on that and will post more.

  22. morevnaproject commented on Jun 19, 2019

    @morevnaproject
    Member

    is there any submission format for the evaluation?

    As I understand from https://google.github.io/gsocguides/mentor/evaluations:
    The evaluation guidelines should be provided online by Google at specified times with deadlines.

  23. morevnaproject commented on Jun 19, 2019

    @morevnaproject
    Member
  24. ankit-kumar-dwivedi commented on Jun 19, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    The last sentence is important. Actually, I was not aware that Start / End fields have that meaning.
    I have made some tests - yes, it actually allows to "animate" stroke width when vectorization is done for a sequence of images.

    I will keep thinking on that and will post more.

    Okay @morevnaproject I will be testing the skeletonizer output today and will start working on
    Next step - organizeGraphs(skeletons, globals);

    The raw skeleton data obtained from StraightSkeletonizer class need to be grouped in joints and sequences before proceeding further

    Now need to start thinking how to represent the output that is because the steps after organizeGraphs(skeletons, globals); involves TStroke and TVectorImageP

  25. morevnaproject commented on Jun 19, 2019

    @morevnaproject
    Member

    can you or @blackwarthog help me understand what is this frame in the following lines of code.
    https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/vectorizerparameters.cpp#L234

    Okay, here is how I understood it. The frame variable in this function is a frame number from timeline. But it is represented by value from [0; 1] range, where 0 is first frame of all selected frames and 1 is last frame.

    FullSizeRender

    This is done in two steps:

    (1) - frame number transformed into value from [0;1] range. See lines here - https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonz/vectorizerpopup.cpp#L336-L337 - and here - https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonz/vectorizerpopup.cpp#L362-L363

    (2) Then this value is passed to VectorizerParameters::getCenterlineConfiguration function and it calculates thickness multiplier that should be applied at current frame - https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/vectorizerparameters.cpp#L233-L234

    This way, when user has multiple frames selected for vectorization, the thickness can be animated.

    So, after we understood the meaning of this functionality, let's answer the question - do we need that in Synfig?

    No.

    In Synfig we can control (and animate) outline thickness using Outline Grow parameter of group - https://wiki.synfig.org/Group_Layer#Outline_Grow_Parameter

    So, it' is ok to assume that conf.m_thicknessRatio variable always = 1.0 (at https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/vectorizerparameters.cpp#L233-L234 ).

  26. ankit-kumar-dwivedi commented on Jun 21, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    As it is inconvenient to keep track of every step and some important comments can be lost which contain steps that are currently not top priority but should be implemented later on.

    Hence it is better to keep track from milestone now.

    So let me brief you about the algorithm for centerlinevectorization from Toonz and where I am

    • step-1 If the source is a full-color non-antialiased image so this needs to be processed
      this is currently not implemented see here

    • step 2 - centerlinepolygonizer
      Extracts a polygonal, minimal yet faithful representation of image contours
      Contours polygons (see here )

    • step 3 - centerlineskeletonizer
      The process of skeletonization reduces all objects in an image to lines, without changing the essential structure of the image. (see here)

    Further steps are following and have their own issue in the milestone

    • step 4 - organizeGraphs
      The raw skeleton data obtained from StraightSkeletonizer class need to be grouped in joints and sequences before proceding further (In progress)

    • step 5
      Take samples of image colors to associate each sequence to its corresponding palette color

    • step 6
      Converts each forward or single Sequence of the image in its corresponding Stroke.

    • step 7
      Take samples of image colors to associate each stroke to its corresponding palette color.

    • step 8
      copy/apply the strokes to an empty vector image and return the vector image after further adjustments

    • Further misc adjustments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

AssignedGSoC

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions