Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I don't know about your images. So maybe my answer will be wrong, but if these fishes don't consist solely of an outer contour, then you use the wrong parameter for findContours: CV_RETR_LIST will give you all contours, also those of e.g. the eyes of the fish. So either you search then for your largest contours in your contours-list or you use CV_RETR_EXTERNAL which gives you just the extremal outer contours.

I don't know about your images. So maybe my answer will be wrong, but if these fishes don't consist solely of an outer contour, then you use the wrong parameter for findContours: CV_RETR_LIST will give you all contours, also those of e.g. the eyes of the fish. So either you search then for your largest contours in your contours-list or you use CV_RETR_EXTERNAL which gives you just the extremal outer contours.


EDIT

I find exactly the 5 contours using your black and white image from above:

image description

with the following code:

cv::Mat immat = cv::imread(argv[1], 1); 
CV_Assert(!immat.empty());

cv::Mat img;
cv::cvtColor(immat, img, CV_BGR2GRAY);

cv::Mat imcanny;
cv::Canny (img,imcanny,75,150,3); 

std::vector<std::vector<cv::Point> > contours; 
cv::findContours(imcanny,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
cv::drawContours(immat,contours,-1,CV_RGB(255,0,0),2);

std::cout << "found " << contours.size() << " contours\n";
cv::imwrite("contours.png", immat);

One of your conversions must be wrong, hope you'll figure it out!

I don't know about your images. So maybe my answer will be wrong, but if these fishes don't consist solely of an outer contour, then you use the wrong parameter for findContours: CV_RETR_LIST will give you all contours, also those of e.g. the eyes of the fish. So either you search then for your largest contours in your contours-list or you use CV_RETR_EXTERNAL which gives you just the extremal outer contours.


EDIT

I find exactly the 5 contours using your black and white image from above:

image description

with the following code:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main( int argc, char** argv )
{
    cv::Mat immat = cv::imread(argv[1], 1); 
1);
    CV_Assert(!immat.empty());

 cv::Mat img;
 cv::cvtColor(immat, img, CV_BGR2GRAY);

 cv::Mat imcanny;
 cv::Canny (img,imcanny,75,150,3); 

(img,imcanny,75,150,3);

    std::vector<std::vector<cv::Point> > contours; 
contours;
    cv::findContours(imcanny,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
 cv::drawContours(immat,contours,-1,CV_RGB(255,0,0),2);

 std::cout << "found " << contours.size() << " contours\n";
 cv::imwrite("contours.png", immat);
}

One of your conversions must be wrong, hope you'll figure it out!