Ask Your Question

problem in applying median filtering using cv::mat

asked 2013-03-14 03:58:50 -0600

disha gravatar image

updated 2013-03-14 04:04:51 -0600

Guanta gravatar image
// Usage: ./app input.jpg
#include "opencv2/opencv.hpp"
#include <string>

using namespace cv;
using namespace std;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
cv::Mat result;

class WatershedSegmenter{
    cv::Mat markers;
    void setMarkers(cv::Mat& markerImage)
        markerImage.convertTo(markers, CV_32S);

    cv::Mat process(cv::Mat &image)
        cv::watershed(image, markers);
        return markers;
    cv::Mat smther(cv::Mat &rfd)

medianBlur( rfd, rfd, 9);


cv::Mat thresh_callback(cv::Mat &dst)
  Mat canny_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  /// Detect edges using canny
  Canny( result, canny_output, thresh, thresh*2, 3 );
  /// Find contours
  findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

  /// Draw contours
  Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  for( int i = 0; i< contours.size(); i++ )
       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
       drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );

  /// Show in a window
  namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
  imshow( "Contours", drawing );

int main(int argc, char* argv[])
    cv::Mat image = cv::imread(argv[1]);
    cv::Mat binary;// = cv::imread(argv[2], 0);
    cv::cvtColor(image, binary, CV_BGR2GRAY);
    cv::threshold(binary, binary, 100, 255, THRESH_BINARY);

    imshow("originalimage", image);
    imshow("originalbinary", binary);

    // Eliminate noise and smaller objects
    cv::Mat fg;
    imshow("fg", fg);

    // Identify image pixels without objects
    cv::Mat bg;
    cv::threshold(bg,bg,1, 128,cv::THRESH_BINARY_INV);
    imshow("bg", bg);

    // Create markers image
    cv::Mat markers(binary.size(),CV_8U,cv::Scalar(0));
    markers= fg+bg;
    imshow("markers", markers);

    // Create watershed segmentation object
    WatershedSegmenter segmenter;

    cv::Mat result = segmenter.process(image);
 cv::Mat final=segmenter.smther(result);
    cv::Mat endres= segmenter.thresh_callback(final);

    imshow("final_result", endres);


    return 0;

when i am using this code it is compiling all right but when i run it, it gives following error. init done opengl support available OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/disha/OpenCV-2.4.1/modules/core/src/array.cpp, line 2482 terminate called after throwing an instance of 'cv::Exception' what(): /home/disha/OpenCV-2.4.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat

Aborted (core dumped) can anybody solve my problem?.Basically i am having problem in using the function smther. thanks and regards.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-03-25 12:09:53 -0600

Haris gravatar image

It seems some bugs in your code


The function cv::Mat smther(cv::Mat &rfd) should return the resultant Mat like,

cv::Mat smther(cv::Mat &rfd){

 medianBlur( rfd, rfd, 9);
 return rfd;


And secondly,

in function cv::Mat thresh_callback(cv::Mat &dst) you should pass your function argument to Canny(), also you should return the resultant Mat, as your using it in the function call.

So change your code to

cv::Mat thresh_callback(cv::Mat &dst){
Canny( dst, canny_output, thresh, thresh*2, 3 );
 return drawing;

Hope this will solve your problem,

edit flag offensive delete link more

Question Tools


Asked: 2013-03-14 03:58:50 -0600

Seen: 1,746 times

Last updated: Mar 25 '14