// 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{ private: cv::Mat markers; public: void setMarkers(cv::Mat& markerImage) { markerImage.convertTo(markers, CV_32S); }
cv::Mat process(cv::Mat &image)
{
cv::watershed(image, markers);
markers.convertTo(markers,CV_8U);
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;
cv::erode(binary,fg,cv::Mat(),cv::Point(-1,-1),2);
imshow("fg", fg);
// Identify image pixels without objects
cv::Mat bg;
cv::dilate(binary,bg,cv::Mat(),cv::Point(-1,-1),3);
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;
segmenter.setMarkers(markers);
cv::Mat result = segmenter.process(image);
result.convertTo(result,CV_8U);
cv::Mat final=segmenter.smther(result); cv::Mat endres= segmenter.thresh_callback(final);
imshow("final_result", endres);
cv::waitKey(0);
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.