Ask Your Question

codemonkey's profile - activity

2013-05-29 17:40:22 -0600 asked a question findcontours is not finding all the contours

image description image description

I'm trying to get outlines of all of the color blobs in this image but between Canny and findContours, I'm not getting all of the contours. I attached the original image and one with the resulting contours drawn in white.

Here's my code:

// pad the image with a border of black pixels in case a hotspot is on the edge
cv::Mat matPadded = cv::Mat::zeros(cv::Size(matImage.size().width + 4, matImage.size().height + 4), CV_8UC3);
cv::Rect roi = cv::Rect(cv::Point(2, 2), matImage.size());
cv::Mat matROI = matPadded(roi);
matImage.copyTo(matROI);

cv::Mat matCanny;
cv::Canny(matPadded, matCanny, 50, 150);

// find contours
std::vector<std::vector<cv::Point>> aContours;
cv::findContours(matCanny, aContours, aHierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

Any tips on how I can get better results? I tried converting the image to grayscale first but it was even worse.

2013-04-26 13:32:08 -0600 received badge  Editor (source)
2013-04-26 13:30:06 -0600 asked a question Throwing an error when scanning pixels in an image

I'm getting this error:

error: (-215) dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()

I'm scanning through an image to determine what color the pixels are inside a polygon. I'm completely stumped as to what is causing the error. Any assistance would be great.

This is the code that is throwing the error (CVGColor is just a structure to hold RGB values):

cv::Rect boundingRect = cv::boundingRect(aPolygon);
cv::Mat matCropped = matImage(boundingRect);
std::vector<cv::Point> aPolygonTranslated;
for(size_t i=0; i<aPolygon.size(); i++)
{
  aPolygonTranslated.push_back(cv::Point(aPolygon[i].x - boundingRect.x, aPolygon[i].y - boundingRect.y));
}
for(int i=0; i<matCropped.cols; i++)
{
  for(int j=0; j<matCropped.rows; j++)
  {
    if(cv::pointPolygonTest(aPolygonTranslated, cv::Point(i, j), false) > 0)  // point is inside
    {
      CVGColor cvgPixel(matCropped.at<cv::Vec3b>(i, j)[2], matCropped.at<cv::Vec3b>(i, j)[1], matCropped.at<cv::Vec3b>(i, j)[0]);
      }
    }
}