user1322801
user1322801

Reputation: 849

CPP OpenCV - Trying to crop image according to its edges

I need to crop an image according to the edges i find(with a given epsilon)

I have following the following instructions: Opencv c++ detect and crop white region on image

with one change - i am trying to find the rectangle according to the edges image i receive from the canny edge detector:

cv::Mat in = cv::imread("c:/cropping/myImage.png");
cv::Mat _imgGray;
cv::Mat edges;
std::vector<cv::Point> nonBlackList;

cv::cvtColor(in, _imgGray, CV_BGR2GRAY);
cv::Canny(_imgGray, edges, 5, 50, 3);
for(int j=0; j<edges.rows; ++j)
for(int i=0; i<edges.cols; ++i)
{
    // if not black: add to the list
    if(edges.at<cv::Vec3b>(j,i) != cv::Vec3b(0,0,0))
    {
        nonBlackList.push_back(cv::Point(i,j));
    }
}

The problem i encounter is that when I get to column #139 it crashes over the following exception:

Microsoft C++ exception: cv::Exception at memory location 0x000000EB35DEF170.

but when i use the original image(not the edges image) it works as expected.

any ideas? What i would like is to crop according to the edges of the image and not the actual image

Thanks

Upvotes: 0

Views: 501

Answers (1)

zindarod
zindarod

Reputation: 6468

The Canny result is an 8-bit single channel binary image. You're accessing pixel values using Vec3i; it means you're assuming the image is an 8-bit 3 channel image, which is incorrect. Try:

if(edges.at<uchar>(j,i) != 0)
{
    nonBlackList.push_back(cv::Point(i,j));
}

Upvotes: 1

Related Questions