c++ - OpenCV Active Contour doesn't work well for Biomed data compared it's Matlab version -


i working on translating matlab version active contour opencv. seems opencv has own version of active contour deprecated then. curious how different between version matlab version. did comparison experiment. given mri brain image , initial boundary , applied both matlab , opencv version active contour segment white matter in brain.

for both matlab , opencv, choose gradient(edge) method of active contour. labeled results on image. red curve initial boundary while green curve final boundary.

the matlab code is:

clear clc = imread('brain.png'); imasks = imread('shrinkmask.png');  figure(1) imshow(i) hold on visboundaries( imasks, 'color', 'r');  bwsnake = activecontour(i,  imasks, 250, 'edge','contractionbias',-0.45,'smoothfactor',0.7); finalboundary = bwboundaries(bwsnake); visboundaries(finalboundary, 'color', 'g'); 

enter image description here

it seems matlab job when choosing appropriate parameters. "contractionbias" parameter (refer here) useful controlling curve shrink or expand.

however, opencv version active contour seems doesn't work @ all. use slider bar change parameters alpha, beta , gamma cannot descend result. seems curve doesn't expand no matter how set parameters. use opencv 2.4.13.3 , active contour can found in legacy folder snakes.cpp. chek opencv github or more snakes.cpp. opencv test code is:

#include <iostream> #include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/legacy/legacy.hpp"   using namespace std; using namespace cv;  iplimage *image = 0;  iplimage *image2 = 0;  iplimage *imagemask = 0; using namespace std;  int ialpha = 10; int ibeta = 40; int igamma = 50;   void onchange(int pos) {      if (image2) cvreleaseimage(&image2);     if (image) cvreleaseimage(&image);     if (imagemask) cvreleaseimage(&imagemask);      image2 = cvloadimage("brain.png", 1);      image = cvloadimage("brain.png", 0);     imagemask = cvloadimage("shrinkmask.png", 0);        cvmemstorage* storage = cvcreatememstorage(0);     cvseq* contours = 0;      cvfindcontours(imagemask, storage, &contours, sizeof(cvcontour),         cv_retr_external, cv_chain_approx_simple);       if (!contours) return;     int length = contours->total;     if (length<10) return;     cvpoint* point = new cvpoint[length];       cvseqreader reader;     cvpoint pt = cvpoint(0, 0);;     cvseq *contour2 = contours;      cvstartreadseq(contour2, &reader);     (int = 0; < length; i++)     {         cv_read_seq_elem(pt, reader);         point[i] = pt;     }     cvreleasememstorage(&storage);       (int = 0; i<length; i++)     {         int j = (i + 1) % length;         cvline(image2, point[i], point[j], cv_rgb(255, 0, 0), 1, 8, 0);     }      float alpha = ialpha / 100.0f;     float beta = ibeta / 100.0f;     float gamma = igamma / 100.0f;      cvsize size;     size.width = 3;     size.height = 3;     cvtermcriteria criteria;     criteria.type = cv_termcrit_iter;     criteria.max_iter = 500;     criteria.epsilon = 0.1;     cvsnakeimage(image, point, length, &alpha, &beta, &gamma, cv_value, size, criteria, 1);       (int = 0; i<length; i++)     {         int j = (i + 1) % length;         cvline(image2, point[i], point[j], cv_rgb(0, 255, 0), 1, 8, 0);     }     delete[]point;  }  int main(int argc, char* argv[]) {       cvnamedwindow("win1", 0);     cvcreatetrackbar("alpha", "win1", &ialpha, 100, onchange);     cvcreatetrackbar("beta", "win1", &ibeta, 100, onchange);     cvcreatetrackbar("gamma", "win1", &igamma, 100, onchange);     //cvresizewindow("win1", 696, 520);     cvresizewindow("win1", 256, 256);     onchange(0);      (;;)     {         if (cvwaitkey(40) == 27) break;         cvshowimage("win1", image2);     }      return 0; } 

enter image description here

is there did wrong or version of active contour in opencv useless? checked snake.cpp, source code opencv version active contour, seems implemented based on theory in paper. because of file protection, cannot access matlab version active contour. therefore, cannot compare them directly. since eventually, use opencv implement stuffs, suggestion improving or changing opencv code appreciated.

i attached original image , mask in case wants repeat test.

thanks!

brain image mask


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -