Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Memory leaks cv::kmeans in cv:grabCut

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins my code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame>
    <frame>
      <ip>0x12FB28</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>filterAndCutImage(cv::Mat const&amp;, cv::Mat&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x1283DC</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>validateParameter(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;, cv::Size_&lt;int&gt; const&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x111176</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>

Is there any other algorithm in OpenCV which would allow me the image background segmentation? I am doing it based on the single image, so something like BackgroundSubtractorMOG2will not be of much use here.

Memory leaks cv::kmeans in cv:grabCut

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins my the code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame>
    <frame>
      <ip>0x12FB28</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>filterAndCutImage(cv::Mat const&amp;, cv::Mat&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x1283DC</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>validateParameter(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;, cv::Size_&lt;int&gt; const&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x111176</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>

Is there any other algorithm in OpenCV which would allow me the image background segmentation? I am doing it based on the single image, so something like BackgroundSubtractorMOG2will not be of much use here.

Memory leaks cv::kmeans in cv:grabCut

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins the code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame>
    <frame>
      <ip>0x12FB28</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>filterAndCutImage(cv::Mat const&amp;, cv::Mat&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x1283DC</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>validateParameter(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;, cv::Size_&lt;int&gt; const&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x111176</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>

If I run grabCut on the same data in loop I get the different results. But if I just stop the program and restart it, the output is the same. Here is my code which I run for grabCut face segmentation: cv::Mat1b mask(input.rows, input.cols); cv::Mat marked_area(input.rows, input.cols, CV_8U, cv::Scalar(255)); // let's set all of them to possible background first mask.setTo(cv::GC_PR_BGD);

// cut out a small area in the middle of the image
int m_rows = 0.75 * input.rows;
int m_cols = 0.6 * input.cols;
// of course here you could also use cv::Rect() instead of cv::Range to select
// the region of interest
cv::Mat1b fg_seed = mask(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                         cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
// mark it as foreground
fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);


// next marking
m_rows = 0.3 * input.rows;
m_cols = 0.8 * input.cols;
fg_seed = mask(cv::Range(input.rows * 0.1, m_rows),
               cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));

fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows * 0.1, m_rows),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);

// Glasses
cv::ellipse(mask, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::GC_FGD, CV_FILLED);
cv::ellipse(marked_area, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::Scalar(0), CV_FILLED);

// Show selected areas
//    cv::resize(marked_area, marked_area, cv::Size(marked_area.cols * 1.6, marked_area.rows * 1.6));
//    cv::imshow("Set as foreground", marked_area);
//    cv::waitKey(3);

// select first 5 rows of the image as background
cv::Mat1b bg_seed = mask(cv::Range(mask.rows-3, mask.rows-1),cv::Range::all());
bg_seed.setTo(cv::GC_BGD);

cv::Mat colour_input;
cv::cvtColor(input , colour_input , CV_GRAY2RGB);

cv::grabCut(colour_input, mask, cv::Rect(), bgModel, fgModel, 1, cv::GC_INIT_WITH_MASK);
// let's get all foreground and possible foreground pixels
cv::Mat1b mask_fgpf = ( mask == cv::GC_FGD) | ( mask == cv::GC_PR_FGD);
// and copy all the foreground-pixels to a temporary image
cv::Mat1b output = cv::Mat1b::zeros(input.rows, input.cols);
input.copyTo(output, mask_fgpf);

Is there any other algorithm in OpenCV which would allow me the image background segmentation? I am doing it based on the single image, so something like BackgroundSubtractorMOG2will not be of much use here.

Memory leaks cv::kmeans in cv:grabCut

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins the code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame>
    <frame>
      <ip>0x12FB28</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>filterAndCutImage(cv::Mat const&amp;, cv::Mat&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x1283DC</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>validateParameter(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;, cv::Size_&lt;int&gt; const&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x111176</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>

If I run grabCut on the same data in loop I get the different results. But if I just stop the program and restart it, the output is the same. Here is my code which I run for grabCut face segmentation: segmentation:

cv::Mat1b mask(input.rows, input.cols);
  cv::Mat marked_area(input.rows, input.cols, CV_8U, cv::Scalar(255));
 // let's set all of them to possible background first
    mask.setTo(cv::GC_PR_BGD);

mask.setTo(cv::GC_PR_BGD);

// cut out a small area in the middle of the image
int m_rows = 0.75 * input.rows;
int m_cols = 0.6 * input.cols;
// of course here you could also use cv::Rect() instead of cv::Range to select
// the region of interest
cv::Mat1b fg_seed = mask(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                         cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
// mark it as foreground
fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);


// next marking
m_rows = 0.3 * input.rows;
m_cols = 0.8 * input.cols;
fg_seed = mask(cv::Range(input.rows * 0.1, m_rows),
               cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));

fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows * 0.1, m_rows),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);

// Glasses
cv::ellipse(mask, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::GC_FGD, CV_FILLED);
cv::ellipse(marked_area, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::Scalar(0), CV_FILLED);

// Show selected areas
//    cv::resize(marked_area, marked_area, cv::Size(marked_area.cols * 1.6, marked_area.rows * 1.6));
//    cv::imshow("Set as foreground", marked_area);
//    cv::waitKey(3);

// select first 5 rows of the image as background
cv::Mat1b bg_seed = mask(cv::Range(mask.rows-3, mask.rows-1),cv::Range::all());
bg_seed.setTo(cv::GC_BGD);

cv::Mat colour_input;
cv::cvtColor(input , colour_input , CV_GRAY2RGB);

cv::grabCut(colour_input, mask, cv::Rect(), bgModel, fgModel, 1, cv::GC_INIT_WITH_MASK);
// let's get all foreground and possible foreground pixels
cv::Mat1b mask_fgpf = ( mask == cv::GC_FGD) | ( mask == cv::GC_PR_FGD);
// and copy all the foreground-pixels to a temporary image
cv::Mat1b output = cv::Mat1b::zeros(input.rows, input.cols);
input.copyTo(output, mask_fgpf);

Is there any other algorithm in OpenCV which would allow me the image background segmentation? I am doing it based on the single image, so something like BackgroundSubtractorMOG2will not be of much use here.

Memory leaks cv::kmeans in cv:grabCut

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins the code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame>
    <frame>
      <ip>0x12FB28</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>filterAndCutImage(cv::Mat const&amp;, cv::Mat&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x1283DC</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>validateParameter(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;, cv::Size_&lt;int&gt; const&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x111176</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>

If I run grabCut on the same data in loop I get the different results. But if I just stop the program and restart it, the output is the same. Here is my code which I run for grabCut face segmentation:

cv::Mat1b mask(input.rows, input.cols);

cv::Mat marked_area(input.rows, input.cols, CV_8U, cv::Scalar(255));
// let's set all of them to possible background first
mask.setTo(cv::GC_PR_BGD);

// cut out a small area in the middle of the image
int m_rows = 0.75 * input.rows;
int m_cols = 0.6 * input.cols;
// of course here you could also use cv::Rect() instead of cv::Range to select
// the region of interest
cv::Mat1b fg_seed = mask(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                         cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
// mark it as foreground
fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);


// next marking
m_rows = 0.3 * input.rows;
m_cols = 0.8 * input.cols;
fg_seed = mask(cv::Range(input.rows * 0.1, m_rows),
               cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));

fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows * 0.1, m_rows),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);

// Glasses
cv::ellipse(mask, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::GC_FGD, CV_FILLED);
cv::ellipse(marked_area, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::Scalar(0), CV_FILLED);

// Show selected areas
//    cv::resize(marked_area, marked_area, cv::Size(marked_area.cols * 1.6, marked_area.rows * 1.6));
//    cv::imshow("Set as foreground", marked_area);
//    cv::waitKey(3);

// select first 5 rows of the image as background
cv::Mat1b bg_seed = mask(cv::Range(mask.rows-3, mask.rows-1),cv::Range::all());
bg_seed.setTo(cv::GC_BGD);

cv::Mat colour_input;
cv::cvtColor(input , colour_input , CV_GRAY2RGB);

cv::grabCut(colour_input, mask, cv::Rect(), bgModel, fgModel, 1, cv::GC_INIT_WITH_MASK);
// let's get all foreground and possible foreground pixels
cv::Mat1b mask_fgpf = ( mask == cv::GC_FGD) | ( mask == cv::GC_PR_FGD);
// and copy all the foreground-pixels to a temporary image
cv::Mat1b output = cv::Mat1b::zeros(input.rows, input.cols);
input.copyTo(output, mask_fgpf);

Is there any other algorithm in OpenCV which would allow me the image background segmentation? I am doing it based on the single image, so something like BackgroundSubtractorMOG2will not be of much use here.

Memory leaks cv::kmeans in cv:grabCut

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins the code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame>
    <frame>
      <ip>0x12FB28</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>filterAndCutImage(cv::Mat const&amp;, cv::Mat&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x1283DC</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>validateParameter(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;, cv::Size_&lt;int&gt; const&amp;, cv::Size_&lt;int&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x111176</ip>
      <obj>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <skaux>match-leak-kinds: possible</skaux>
    <sframe> <fun>calloc</fun> </sframe>
    <sframe> <fun>allocate_dtv</fun> </sframe>
    <sframe> <fun>_dl_allocate_tls</fun> </sframe>
    <sframe> <fun>allocate_stack</fun> </sframe>
    <sframe> <fun>pthread_create@@GLIBC_2.2.5</fun> </sframe>
    <sframe> <fun>_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj</fun> </sframe>
    <sframe> <fun>_ZN2cv10ThreadPool12reconfigure_Ej</fun> </sframe>
    <sframe> <fun>_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd</fun> </sframe>
    <sframe> <fun>_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE</fun> </sframe>
    <sframe> <fun>_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii</fun> </sframe>
    <sframe> <fun>_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE</fun> </sframe>
    <sframe> <fun>_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:allocate_dtv
   fun:_dl_allocate_tls
   fun:allocate_stack
   fun:pthread_create@@GLIBC_2.2.5
   fun:_ZN2cv12WorkerThreadC1ERNS_10ThreadPoolEj
   fun:_ZN2cv10ThreadPool12reconfigure_Ej
   fun:_ZN2cv21parallel_for_pthreadsERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv13parallel_for_ERKNS_5RangeERKNS_16ParallelLoopBodyEd
   fun:_ZN2cv6kmeansERKNS_11_InputArrayEiRKNS_17_InputOutputArrayENS_12TermCriteriaEiiRKNS_12_OutputArrayE
   fun:_ZN2cv7grabCutERKNS_11_InputArrayERKNS_17_InputOutputArrayENS_5Rect_IiEES5_S5_ii
   fun:_Z17filterAndCutImageRKN2cv3MatERS0_RKNS_5Size_IiEE
   fun:_Z17validateParameterRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES6_RKSt6vectorIN2cv3MatESaIS9_EERKS7_IiSaIiEERKNS8_5Size_IiEESL_
   fun:main
}
]]>
    </rawtext>
  </suppression>
<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>

If I run grabCut on the same data in loop I get the different results. But if I just stop the program and restart it, the output is the same. Here is my code which I run for grabCut face segmentation:

cv::Mat1b mask(input.rows, input.cols);

cv::Mat marked_area(input.rows, input.cols, CV_8U, cv::Scalar(255));
// let's set all of them to possible background first
mask.setTo(cv::GC_PR_BGD);

// cut out a small area in the middle of the image
int m_rows = 0.75 * input.rows;
int m_cols = 0.6 * input.cols;
// of course here you could also use cv::Rect() instead of cv::Range to select
// the region of interest
cv::Mat1b fg_seed = mask(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                         cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
// mark it as foreground
fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows/2 - m_rows/2, input.rows/2 + m_rows/2),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);


// next marking
m_rows = 0.3 * input.rows;
m_cols = 0.8 * input.cols;
fg_seed = mask(cv::Range(input.rows * 0.1, m_rows),
               cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));

fg_seed.setTo(cv::GC_FGD);

fg_seed = marked_area(cv::Range(input.rows * 0.1, m_rows),
                      cv::Range(input.cols/2 - m_cols/2, input.cols/2 + m_cols/2));
fg_seed.setTo(0);

// Glasses
cv::ellipse(mask, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::GC_FGD, CV_FILLED);
cv::ellipse(marked_area, cv::Point(input.cols / 2, input.rows / 2.0 - input.rows * 0.07), cv::Size(input.cols * 0.5, input.rows * 0.1),
            0.0, 0.0, 360.0, cv::Scalar(0), CV_FILLED);

// select first 5 rows of the image as background
cv::Mat1b bg_seed = mask(cv::Range(mask.rows-3, mask.rows-1),cv::Range::all());
bg_seed.setTo(cv::GC_BGD);

cv::Mat colour_input;
cv::cvtColor(input , colour_input , CV_GRAY2RGB);

cv::grabCut(colour_input, mask, cv::Rect(), bgModel, fgModel, 1, cv::GC_INIT_WITH_MASK);
// let's get all foreground and possible foreground pixels
cv::Mat1b mask_fgpf = ( mask == cv::GC_FGD) | ( mask == cv::GC_PR_FGD);
// and copy all the foreground-pixels to a temporary image
cv::Mat1b output = cv::Mat1b::zeros(input.rows, input.cols);
input.copyTo(output, mask_fgpf);

Is there any other algorithm in OpenCV which would allow me the image background segmentation? I am doing it based on the single image, so something like BackgroundSubtractorMOG2will not be of much use here.

Here is the unit test which gives me described behaviour (filterAndCutImage runs grabCut as described above, returns me ROI and preprocessingHaarWithAlignmentEye runs Haar cascade, detects face, eyes, makes the alignment)

cv::Mat hidden_mouth_image = cv::imread("hidden_mouth.png", cv::IMREAD_GRAYSCALE);
EXPECT_TRUE(hidden_mouth_image.data);
cv::Mat whole_face_image = cv::imread("whole_face.png", cv::IMREAD_GRAYSCALE);
EXPECT_TRUE(whole_face_image.data);

std::vector<cv::Mat> original_images;
original_images.emplace_back(hidden_mouth_image);
original_images.emplace_back(whole_face_image);

cv::Size first_size(200,260);
cv::Size cut_size(180,230);

std::vector<cv::Mat> previous_filtered_images;
for (int run = 0; run < 2; run++)
{
    std::vector<cv::Mat> run_images = cloneMatVector(original_images);
    for (auto& image : run_images)
    {
        image = preprocessingHaarWithAlignmentEye(image, first_size);
        EXPECT_TRUE(image.data);
        filterAndCutImage(image, image, cut_size);
        EXPECT_EQ(cut_size, image.size());
    }
    if (!previous_filtered_images.empty())
    {
        EXPECT_TRUE(areTheSame(run_images, previous_filtered_images));
    }
    previous_filtered_images = run_images;
}

This code fails on areTheSame.