Loading the SED model in android

asked 2018-04-09 08:38:43 -0600

charara gravatar image

Hi everyone ,

I used this code to implement an Structured Edge Detection model in android but I ran to an error.

Code Snippet :

private Bitmap StructuredDetectEdges (Bitmap bitmap){
    Mat rgba = new Mat();
    Mat edges = new Mat(rgba.size(), CvType.CV_8UC1);
    StructuredEdgeDetection pDollar = createStructuredEdgeDetection("file:///android_asset/SEDmodel.yml");
    Bitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(edges, resultBitmap);
    return resultBitmap; }

Error that I got :

E/cv::error(): OpenCV(3.4.0-dev) Error: Assertion failed (modelFile.isOpened()) in cv::ximgproc::StructuredEdgeDetectionImpl::StructuredEdgeDetectionImpl(const cv::String&, cv::Ptr<const cv::ximgproc::rffeaturegetter="">), file /Users/Chao/opencv_contrib/modules/ximgproc/src/structured_edge_detection.cpp, line 432

Any help on how can I solve this problem is much appreciated.

edit retag flag offensive close merge delete


you can't read from the assets folder directly (those are inside a zip).

you'll have to copy it to your sdcard first, and read from there.

berak gravatar imageberak ( 2018-04-09 08:50:56 -0600 )edit

@berak thank you for the tip !! I managed to call the model correctly , but now I'm having a new error :

 Caused by: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.0-dev) /Users/Chao/opencv_contrib/modules/ximgproc/src/structured_edge_detection.cpp:529: error: (-215) _src.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) in function virtual void cv::ximgproc::StructuredEdgeDetectionImpl::detectEdges(cv::InputArray, cv::OutputArray) const

I'm using the same code above. It seems that there is a problem with the type of the source matrix. Any idea how can this be fixed ?

charara gravatar imagecharara ( 2018-04-10 04:06:29 -0600 )edit

you have to convert your img to float (also scale to [0..1]) before using it, see docs, please

berak gravatar imageberak ( 2018-04-10 04:13:51 -0600 )edit

Thank you again for the information, the documentation you linked is for c++ ! since I'm working with java I should consult the javadoc link . The detectEdges method takes Mat type for both source and destination. And that's what I did in the code above ! I converted my bitmap (img) to a Mat (matrix) and fed it to the detectEdges method. Correct me please if I'm wrong or missing something. Much appreciated

charara gravatar imagecharara ( 2018-04-10 04:39:52 -0600 )edit

Hi there @charara, I have exactly same problem as you has originally with modelFile.isOpened(), but I haven't been able to fix the problem even though I've put model.yml.gz into the downloads folder on my android device. Would you mind sharing what you did? ie: Where you put the model file and whether you opened it in any way before passing the file name to Ximgproc.createStructuredEdgeDetection. and whether you just put in "model.yml.gz" or whether you included the path? (I assume you did?) Thanks in advance.

kayjay gravatar imagekayjay ( 2018-05-30 09:46:27 -0600 )edit

@kayjay , try with an absolute path to the file.

berak gravatar imageberak ( 2018-05-30 09:54:26 -0600 )edit

@berak if I look on my device, the path to the model file is /sdcard/Download/model.yml. But unfortunately my code (below) still crashes with the same model.isOpened() error. String path = "/sdcard/Download/model.yml"; StructuredEdgeDetection edge_detection = Ximgproc.createStructuredEdgeDetection(path);

Do you perhaps have any other ideas? My read external storage permission is in my manifest file.

kayjay gravatar imagekayjay ( 2018-05-31 01:53:24 -0600 )edit

I just solved the problem! Although I'd enabled the permission in my manifest file, I hadn't clicked that I wasn't asked to allow permissions at runtime...so I went to Settings -> Apps -> {Your App} -> Permissions and granted storage access there. Solved. Thanks for your help!

kayjay gravatar imagekayjay ( 2018-05-31 01:57:45 -0600 )edit

you also probably should leave it as model.yml.gz , like it was, originally

berak gravatar imageberak ( 2018-05-31 02:06:23 -0600 )edit