opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.42k stars 5.76k forks source link

Unable to detect edges with modelBsds.mat #1559

Open hoonkai opened 6 years ago

hoonkai commented 6 years ago
System information (version)
Detailed description

I'm unable to detect edges using structured forest training's original BSDS model (https://github.com/pdollar/edges/blob/master/models/forest/modelBsds.mat). When I try loading the model and running edge detection, I get this:

screen shot 2018-02-15 at 01 05 32

for the input image:

screen shot 2018-02-15 at 01 07 20

modelBsds.mat works fine within Matlab, but after converting to .yaml, detection fails. I believe something might be wrong with the mat-to-yaml conversion script.

Steps to reproduce

(Based on https://docs.opencv.org/master/d2/d59/tutorial_ximgproc_training.html#gsc.tab=0)

  1. Download and load in Matlab (R2017b) https://github.com/pdollar/edges/blob/master/models/forest/modelBsds.mat
  2. Download https://github.com/opencv/opencv_contrib/blob/master/modules/ximgproc/tutorials/scripts/modelConvert.m
  3. Convert trained model from Matlab binary format to YAML by running modelConvert(model, "model.yml") in Matlab
  4. Run
    import cv2
    img = cv2.imread('./kermit.jpg')
    edgedetector = cv2.ximgproc.createStructuredEdgeDetection('./model.yml')
    src = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    edges = edgedetector.detectEdges(np.float32(src) / 255.0)
    cv2.imshow("edges", edges)
hoonkai commented 6 years ago

@alalek How come this is invalid? The steps I described are essentially those outlined here, which is a tutorial for contrib modules.

hoonkai commented 6 years ago

Hi @mshabunin Can I ask if the conversion script https://github.com/opencv/opencv_contrib/blob/master/modules/ximgproc/tutorials/scripts/modelConvert.m is supposed to work on the "stock" .mat model as well? That is, the model generated without removing lines 26–41 in edgesChns.m.

gds101054108 commented 6 years ago

@alalek I use mexopencv3.4.0 and train the model using the following code. the trained model works fine within Matlab, but after converting to .yaml, detection fails. I believe the training process might be different between matlab and opencv. Looking forward to your reply。

function [chnsReg,chnsSim] = edgesChns( I, opts )
% Compute features for structured edge detection.
%
% For an introductory tutorial please see edgesDemo.m.
%
% USAGE
%  [chnsReg,chnsSim] = edgesChns( I, opts )
%
% INPUTS
%  I          - [h x w x 3] color input image
%  opts       - structured edge model options
%
% OUTPUTS
%  chnsReg    - [h x w x nChannel] regular output channels
%  chnsSim    - [h x w x nChannel] self-similarity output channels
%
% EXAMPLE
%
% See also edgesDemo, edgesTrain, edgesDetect, gradientMag
%
% Structured Edge Detection Toolbox      Version 3.01
% Code written by Piotr Dollar, 2014.
% Licensed under the MSR-LA Full Rights License [see license.txt]

shrink=opts.shrink; nTypes=1; chns=cell(1,opts.nChns); k=0;
% if(size(I,3)>3), nTypes=2; Is={I(:,:,1:3),I(:,:,4:end)}; end
% for t=1:nTypes
%   if(nTypes>1), I=Is{t}; end
%   if(size(I,3)==1), cs='gray'; else cs='luv'; end; I=rgbConvert(I,cs);
%   Ishrink=imResample(I,1/shrink); k=k+1; chns{k}=Ishrink;
%   for i = 1:2, s=2^(i-1);
%     if(s==shrink), I1=Ishrink; else I1=imResample(I,1/s); end
%     I1 = convTri( I1, opts.grdSmooth );
%     [M,O] = gradientMag( I1, 0, opts.normRad, .01 );
%     H = gradientHist( M, O, max(1,shrink/s), opts.nOrients, 0 );
%     k=k+1; chns{k}=imResample(M,s/shrink);
%     k=k+1; chns{k}=imResample(H,max(1,s/shrink));
%   end
% end
% chns=cat(3,chns{1:k}); assert(size(chns,3)==opts.nChns);
chns=cv.StructuredEdgeDetection.getFeatures(im2single(I),struct('normRad',4,'grdSmooth',0,'shrink',2,'nChns',13,'nOrients',4));
chnSm=opts.chnSmooth/shrink; if(chnSm>1), chnSm=round(chnSm); end
simSm=opts.simSmooth/shrink; if(simSm>1), simSm=round(simSm); end
chnsReg=convTri(chns,chnSm); chnsSim=convTri(chns,simSm);

end
alalek commented 6 years ago

@gds101054108 Did you have a change to check the patch #1579 ?

gds101054108 commented 6 years ago

@alalek , after check the patch #1579,change the param sharpen to 0 and retrain the model, it works now.