Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a couple of things that should be mentioned about your code:</p> <ul> <li>Watershed expects the input and the output image to have the same size;</li> <li>You probably want to get rid of the <code>const</code> parameters in the methods;</li> <li>Notice that the result of watershed is actually <code>markers</code> and not <code>image</code> as your code suggests; About that, you need to grab the return of <code>process()</code>!</li> </ul> <p>This is your code, with the fixes above:</p> <pre><code>// Usage: ./app input.jpg #include "opencv2/opencv.hpp" #include &lt;string&gt; using namespace cv; using namespace std; class WatershedSegmenter{ private: cv::Mat markers; public: void setMarkers(cv::Mat&amp; markerImage) { markerImage.convertTo(markers, CV_32S); } cv::Mat process(cv::Mat &amp;image) { cv::watershed(image, markers); markers.convertTo(markers,CV_8U); return markers; } }; 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); imshow("final_result", result); cv::waitKey(0); return 0; } </code></pre> <p>I took the liberty of using Abid's input image for testing and this is what I got:</p> <p><img src="https://i.stack.imgur.com/j9AHh.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload