Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:58:13+00:00 2026-06-07T09:58:13+00:00

I am now learning a code from the opencv codebook ( OpenCV 2 Computer

  • 0

I am now learning a code from the opencv codebook (OpenCV 2 Computer Vision Application Programming Cookbook): Chapter 5, Segmenting images using watersheds, page 131.

Here is my main code:

#include "opencv2/opencv.hpp"
#include <string>

using namespace cv;
using namespace std;

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

    cv::Mat process(const cv::Mat &image){
        cv::watershed(image,markers);
        return markers;
    }
};

int main ()
{
    cv::Mat image = cv::imread("/Users/yaozhongsong/Pictures/IMG_1648.JPG");

    // Eliminate noise and smaller objects
    cv::Mat fg;
    cv::erode(binary,fg,cv::Mat(),cv::Point(-1,-1),6);

    // Identify image pixels without objects
    cv::Mat bg;
    cv::dilate(binary,bg,cv::Mat(),cv::Point(-1,-1),6);
    cv::threshold(bg,bg,1,128,cv::THRESH_BINARY_INV);

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

    // Create watershed segmentation object
    WatershedSegmenter segmenter;
    // Set markers and process
    segmenter.setMarkers(markers);
    segmenter.process(image);

    imshow("a",image);
    std::cout<<".";
    cv::waitKey(0);
}

However, it doesn’t work. How could I initialize a binary image? And how could I make this segmentation code work?

I am not very clear about this part of the book.
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:58:15+00:00Added an answer on June 7, 2026 at 9:58 am

    There’s a couple of things that should be mentioned about your code:

    • Watershed expects the input and the output image to have the same size;
    • You probably want to get rid of the const parameters in the methods;
    • Notice that the result of watershed is actually markers and not image as your code suggests; About that, you need to grab the return of process()!

    This is your code, with the fixes above:

    // Usage: ./app input.jpg
    #include "opencv2/opencv.hpp"
    #include <string>
    
    using namespace cv;
    using namespace std;
    
    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;
        }
    };
    
    
    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;
    }
    

    I took the liberty of using Abid’s input image for testing and this is what I got:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm now learning how to extract exif from images.I wrote the simple code like
I'm just now learning to programming at age 17. It's hard for me to
I came from PHP language(codeigniter), but now I learning ASP.Net MVC :) In PHP
I'm learning scheme and until now have been using guile. I'm really just learning
I'm learning how to code in Java after after coming from C. In C
I'm coming from Android and now I'm learning for Blackberry. In Android to access
I am learning json now and have run into an issue: I am using
Newbie still learning, been trying to search and hack code together for hours now,
I'm learning about deadlocks in Java, and there's this sample code from Sun's official
I have been in at the deep end for a week now learning from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.