Skip to content

[Vectorization] Step 5 of Centerline Vectorization  #849

Closed

Description

@ankit-kumar-dwivedi

This issue is a part of Vectorization of bitmaps [GSoC 2019]

Issue description: Step 5 of Centerline Vectorization

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

Track the progress here -> https://github.com/synfig/synfig/milestone/5

Activity

  1. added
    Assigned
    GSoC
    on Jun 21, 2019
  2. ankit-kumar-dwivedi commented on Jun 21, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    Now I would Like to add @blackwarthog @ice0 in the discussion that in the image below I am at the highlighted step
    image

    As you can see that further steps involve stroke and color palette which are not present in synfig
    So I am thinking to store the RGBA value for single sequence and then use that for that sequence in the skeleton

    and as per @morevnaproject

    And if you will do that, then we will not need palette at step 7 - we can keep existing colors

  3. changed the title [-]Step 5 of Centerline Vectorization [/-] [+][Vectorization] Step 5 of Centerline Vectorization [/+] on Jun 21, 2019
  4. ankit-kumar-dwivedi commented on Jun 24, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject
    openToonz use PixelCM32 (a 32-bit color map 12+12+8 bits (ink,paint,tone)) to represent the pixel and the function
    int getTone() const { return m_value & 0xFF; };
    is used to compare with the threshold while taking color samples in function - > samplecolor (...)

    So what should I use to replace threshold comparison?
    and it stores an int value as m_color in the sequence which is given by int getInk() const { return m_value >> 20; };

    So I am thinking to store the RGBA value for single sequence and then use that for that sequence in the skeleton

    as per my comment I think replacing int m_color with synfig::Color::m_color will do the work

  5. morevnaproject commented on Jun 26, 2019

    @morevnaproject
    Member

    openToonz use PixelCM32 (a 32-bit color map 12+12+8 bits (ink,paint,tone)) to represent the pixel

    @ankit-kumar-dwivedi Sorry, I do not understand what (ink,paint,tone) model means. In particular, I do not understand what the difference of ink from paint. ^__^"

    getTone()

    Logically "Tone" = "Hue" in HSV color model (https://en.wikipedia.org/wiki/Hue). But my intuition tells me that "Tone" means "Value" (from HSV) here. Is it so?

    In any case, Synfig::Color object has get_hue() function to get Hue(https://github.com/synfig/synfig/blob/master/synfig-core/src/synfig/color/color.h#L183).

    If you need "Value" of HSV, then it requires converting to HSV - https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both

    @blackwarthog your thoughts on this?

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

    @ankit-kumar-dwivedi
    MemberAuthor

    @ankit-kumar-dwivedi Sorry, I do not understand what (ink,paint,tone) model means. In particular, I do not understand what the difference of ink from paint. ^__^"

    Actually, I was not able to find where the RGB to C-map conversion is taking place so I thought you might be already knowing it (・_・ヾ
    I am trying to find more about it. Currently looking at the code responsible for loading raster images in Opentoonz

  7. added a commit that references this issue on Jun 26, 2019
  8. morevnaproject commented on Jun 27, 2019

    @morevnaproject
    Member

    @ankit-kumar-dwivedi It looks I figured out what PixelCM32 means. This seems to be pixels from "Toonz Raster Level" (it is not the same as ordinary "Raster Level").

    The explanation on how Toonz Raster Level works can be found here - https://morevnaproject.org/2017/03/17/toonz-raster-level/

    As you can see, in Toonz Raster Level one image actually contains two "layers" - one for "ink" (lines) and one for "paint" (fills). So, each pixel has information for two colors - color of "ink" and color of "paint". There is also a third component - "tone", which represents transparency of this part of image (applied after "ink" mixed with "paint").

    More than that, values of "ink" and "paint" are not really colors - (my guess) they are numbers, referencing colors from palette (by index).

    32-bit color map 12+12+8 bits (ink,paint,tone)

    So, first 12 bytes reference color number for "ink" from palette.
    Next 12 bytes reference color index for "paint".
    Last 8 bytes is a transparency value.

    I hope that helps. ^__^

    Not sure why this function uses "Toonz Raster Level" (PixelCM32) instead of ordinary "Raster Level".

  9. ankit-kumar-dwivedi commented on Jun 27, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    Not sure why this function uses "Toonz Raster Level" (PixelCM32) instead of ordinary "Raster Level".

    @morevnaproject That is because they converted it to Toonzraster level see 373 and 378
    https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/tcenterlinecolors.cpp#L373-L378

    As you can see, in Toonz Raster Level one image actually contains two "layers" - one for "ink" (lines) and one for "paint" (fills). So, each pixel has information for two colors - color of "ink" and color of "paint". There is also a third component - "tone", which represents transparency of this part of image (applied after "ink" mixed with "paint").

    But then the imported image has no such property so how do they get converted?

    More than that, values of "ink" and "paint" are not really colors - (my guess) they are numbers, referencing colors from palette (by index).

    I think so as the function definition says:

    Take samples of image colors to associate each sequence to its corresponding palette color. Currently working on colormaps.

    And this again leaves me with the question what should I comapre with the threshold. I think I should use the same as I used in polygonizer the luminance model.
    So now I am going to use it. But from now onwards we will not be able to compare the results with toonz as we will be using outline layer in place of stroke

  10. morevnaproject commented on Jun 28, 2019

    @morevnaproject
    Member

    That is because they converted it to Toonzraster level see 373 and 378

    so how do they get converted?

    It worth to look how conversion from regular bitmap to TRasterCM32P is actually done. Probably this is done via assignment operator, which is possibly defined in TRasterCM32P class.

  11. ankit-kumar-dwivedi commented on Jun 28, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @morevnaproject actually there is no assignment operator defined
    Let us see what is happening here
    TRasterP &ras is passed to the function void calculateSequenceColors(....)
    and then inside the function we see TRasterCM32P cm = ras
    Now have a look at the image below

    IMG_20190628_151019
    here ⬇️ implies inheritance and all classes except TRaster are template classes

  12. ankit-kumar-dwivedi commented on Jun 28, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    These classes are defined in traster.h just in case if I overlooked any of them

  13. ankit-kumar-dwivedi commented on Jun 28, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    Here is something interesting I found in pixelevaluator (inside polygonizer)
    If the T in TRasterPT

    • if T = TPixelCM32
      getBlackOrWhite(int x, int y) { return m_ras->pixels(y)[x].getTone() < m_threshold; }
    • if T = TPixel32
      getBlackOrWhite(int x, int y) { return std::max(m_ras->pixels(y)[x].r, std::max(m_ras->pixels(y)[x].g, m_ras->pixels(y)[x].b)) < m_threshold * (m_ras->pixels(y)[x].m / 255.0); }
      So @morevnaproject I think it will be okay if I replace the tone() and threshold comaprison with the above method
  14. 4 remaining items

  15. blackwarthog commented on Jun 30, 2019

    @blackwarthog
    Collaborator
  16. ankit-kumar-dwivedi commented on Jun 30, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    @blackwarthog

    nap - is not a point

    EDIT: Okay I found it here

    in synfig you may use Vector3::nan() or Point3::nan() (Point3 if typedef of Vector3) for 3D

    Thanks :)

    synfig stores image rows from top to bottom so, by this logic you will read an image from bottom to top is it right for you?

    Actually, this was how I fixed the image roation see -> #735 (comment)
    For a given value of (int x, int y) to get the respective RGBA values

    • Earlier I was using
    synfig::rendering::SurfaceResource::LockRead<synfig::rendering::SurfaceSW> lock( m_ras->rendering_surface ); 
    	const Surface &surface = lock->get_surface(); 
    	const Color *row = surface[x]; 
    	int r = 255.0*pow(row[y].get_r(),1/2.2);
    	int g = 255.0*pow(row[y].get_g(),1/2.2);
    	int b = 255.0*pow(row[y].get_b(),1/2.2);
    	int a = 255.0*pow(row[y].get_a(),1/2.2);
    
    
    • and changing it to
    synfig::rendering::SurfaceResource::LockRead<synfig::rendering::SurfaceSW> lock( m_ras->rendering_surface ); 
    	const Surface &surface = lock->get_surface(); 
      const int Y = surface.get_h() - y -1; 
    	const Color *row = surface[Y]; 
    	int r = 255.0*pow(row[x].get_r(),1/2.2);
    	int g = 255.0*pow(row[x].get_g(),1/2.2);
    	int b = 255.0*pow(row[x].get_b(),1/2.2);
      int a = 255.0*pow(row[x].get_a(),1/2.2);
    

    fixed the error.

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

    @ankit-kumar-dwivedi
    MemberAuthor
  18. morevnaproject commented on Jul 1, 2019

    @morevnaproject
    Member

    Just some clarifications on #849 (comment):

    It worth to look how conversion from regular bitmap to TRasterCM32P is actually done.

    Actually, there are no conversion happens in calculateSequenceColors(....). See below.

    TRasterP &ras is passed to the function void calculateSequenceColors(....)
    and then inside the function we see TRasterCM32P cm = ras

    ...and just a little below at line 378 there is a check if cm is valid pointer:

    if (cm && g.currConfig->m_maxThickness > 0.0) {
    

    You might see a similar check in centerlineVectorize() function here:

    https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/tcenterlinevectorizer.cpp#L173

    So, if we pass regular raster image to calculateSequenceColors(....) then content of function is just doesn't invoked at all, as the pointer check returns false. ^__^

  19. morevnaproject commented on Jul 1, 2019

    @morevnaproject
    Member

    Closing after ankit-kumar-dwivedi/synfig@7734ac9

    Congratulations! ^__^

  20. ankit-kumar-dwivedi commented on Jul 1, 2019

    @ankit-kumar-dwivedi
    MemberAuthor

    Just some clarifications on #849 (comment):

    It worth to look how conversion from regular bitmap to TRasterCM32P is actually done.

    Actually, there are no conversion happens in calculateSequenceColors(....). See below.

    TRasterP &ras is passed to the function void calculateSequenceColors(....)
    and then inside the function we see TRasterCM32P cm = ras

    ...and just a little below at line 378 there is a check if cm is valid pointer:

    if (cm && g.currConfig->m_maxThickness > 0.0) {
    

    You might see a similar check in centerlineVectorize() function here:

    https://github.com/morevnaproject/opentoonz/blob/ac3067fb0cdbdcbc4f7fca959cf0d6c212586fb4/toonz/sources/toonzlib/tcenterlinevectorizer.cpp#L173

    So, if we pass regular raster image to calculateSequenceColors(....) then content of function is just doesn't invoked at all, as the pointer check returns false. ^__^

    @morevnaproject does this implies that I don't need to recreate this at all? o:-)

  21. morevnaproject commented on Jul 3, 2019

    @morevnaproject
    Member

    @morevnaproject does this implies that I don't need to recreate this at all? o:-)

    You need this only if you are going to detect and apply colors to strokes. ^__^

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    AssignedGSoC

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions