Skip to content

Instantly share code, notes, and snippets.

@aavogt
Created May 1, 2014 19:29
Show Gist options
  • Save aavogt/88bb0f5be06a951e545f to your computer and use it in GitHub Desktop.
Save aavogt/88bb0f5be06a951e545f to your computer and use it in GitHub Desktop.
HOpenCV contours crash
{-# LANGUAGE DataKinds #-}
module Main where
import OpenCV.ArrayOps
import OpenCV.ColorConversion
import OpenCV.Contours ( withContourList, cvMoments, cvGetHuMoments, cvDrawContours, cvContourArea )
import OpenCV.Core.CVOp ( cv )
import OpenCV.Core.CxCore
import OpenCV.Core.ImageUtil
import OpenCV.HighCV
import Control.Applicative
import Control.Monad
method = 1 -- 1 crashes, 0 does not
mkFiles = False
main = do
-- http://i.imgur.com/ZrFTk42.jpg is circ.jpg
f <- fromFileColor "circ.jpg"
let ed n = dilate n . erode n
f' = ed 5 $ thresholdBinaryOtsuInv 100 (convertRGBToGray f)
when mkFiles (toFile "tresh.jpg" f')
withContourList f' $ \c -> do
let dc im = cvDrawContours im c (CvScalar 255 0 0 0) (CvScalar 0 0 0 0) 0 (-1) 8 (CvPoint 0 0)
im <- case method of
0 -> fst <$> withDuplicateImage f dc
1 -> return . cv dc =<< duplicateImage f
a <- cvContourArea c
let selected = cvAnd im f
selected' = cvAnd im (convertGrayToRGB f')
when mkFiles $ do
toFile ("gen/c"++show a++".jpg") im
toFile ("gen/d"++show a++".jpg") selected
let getHu img = unsafeWithHIplImage img (cvGetHuMoments <=< cvMoments)
print =<< getHu (convertRGBToGray selected)
print =<< getHu (convertRGBToGray selected')
return ()
@aavogt
Copy link
Author

aavogt commented May 1, 2014

This crashes more often with ghc-7.8. In gdb it gets to:

CvHuMoments 9.484409732066301e-4 1.5982856271309009e-7 1.488383002113723e-12 3.019515183982149e-12 (-1.2364324671298664e-24) (-8.245547866924004e-16) 6.280679283870537e-24

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b44fe0 in void cv::vBinOp8<unsigned char, cv::OpAnd<unsigned char>, cv::_VAnd8u>(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, cv::Size_<int>) () from /usr/lib/libopencv_core.so.2.4

@aavogt
Copy link
Author

aavogt commented May 2, 2014

valgrind of crash

==23730== Invalid read of size 8
==23730==    at 0x4FB3FE0: void cv::vBinOp8<unsigned char, cv::OpAnd<unsigned char>, cv::_VAnd8u>(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, cv::Size_<int>) (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FB4175: ??? (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FC0CC2: ??? (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FC4E5F: cv::bitwise_and(cv::_InputArray const&, cv::_InputArray const&, cv::_OutputArray const&, cv::_InputArray const&) (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FC5B68: cvAnd (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x408859: ??? (in /home/aavogt/checkContour/main)
==23730==  Address 0x12c77680 is 16 bytes inside a block of size 768,024 free'd
==23730==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23730==    by 0x4EC0C2E: cvReleaseData (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4ECF6B5: cvReleaseImage (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x433952: release_image (in /home/aavogt/checkContour/main)
==23730==    by 0x87AF35: runCFinalizers (in /home/aavogt/checkContour/main)
==23730==    by 0x87AFE7: scheduleFinalizers (in /home/aavogt/checkContour/main)
==23730==    by 0x87CD38: GarbageCollect (in /home/aavogt/checkContour/main)
==23730==    by 0x875572: scheduleDoGC.isra.21 (in /home/aavogt/checkContour/main)
==23730==    by 0x87608E: scheduleWaitThread (in /home/aavogt/checkContour/main)
==23730==    by 0x874146: hs_main (in /home/aavogt/checkContour/main)
==23730==    by 0x409106: main (in /home/aavogt/checkContour/main)
==23730== 
==23730== Invalid read of size 8
==23730==    at 0x4FB3FE4: void cv::vBinOp8<unsigned char, cv::OpAnd<unsigned char>, cv::_VAnd8u>(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, cv::Size_<int>) (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FB4175: ??? (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FC0CC2: ??? (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FC4E5F: cv::bitwise_and(cv::_InputArray const&, cv::_InputArray const&, cv::_OutputArray const&, cv::_InputArray const&) (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4FC5B68: cvAnd (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x408859: ??? (in /home/aavogt/checkContour/main)
==23730==  Address 0x12c77690 is 32 bytes inside a block of size 768,024 free'd
==23730==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23730==    by 0x4EC0C2E: cvReleaseData (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x4ECF6B5: cvReleaseImage (in /usr/lib/libopencv_core.so.2.4.5)
==23730==    by 0x433952: release_image (in /home/aavogt/checkContour/main)
==23730==    by 0x87AF35: runCFinalizers (in /home/aavogt/checkContour/main)
==23730==    by 0x87AFE7: scheduleFinalizers (in /home/aavogt/checkContour/main)
==23730==    by 0x87CD38: GarbageCollect (in /home/aavogt/checkContour/main)
==23730==    by 0x875572: scheduleDoGC.isra.21 (in /home/aavogt/checkContour/main)
==23730==    by 0x87608E: scheduleWaitThread (in /home/aavogt/checkContour/main)
==23730==    by 0x874146: hs_main (in /home/aavogt/checkContour/main)
==23730==    by 0x409106: main (in /home/aavogt/checkContour/main)
==23730== More than 10000000 total errors detected.  I'm not reporting any more.
==23730== Final error counts will be inaccurate.  Go fix your program!
==23730== Rerun with --error-limit=no to disable this cutoff.  Note
==23730== that errors may occur in your program without prior warning from
==23730== Valgrind, because errors are no longer being displayed.

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