Ask Your Question
0

Debug assertion fail when using grayscale

asked 2016-03-02 05:23:28 -0600

Lily gravatar image

I am trying to make a hand recognition system but when i used grayscale for cvtColor, i get debug assertion fail but when i use HSV the code works fine. Can you resolve this ? I am a newbie in opencv.

#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect.hpp"
#include < opencv2\opencv.hpp>    
#include < stdio.h>  
#include <iostream>


using namespace std;
using namespace cv;

int thresh = 100;


int findBiggestContour(vector<vector<Point> > contours){
    int indexOfBiggestContour = -1;
    int sizeOfBiggestContour = 0;
    for (int i = 0; i < contours.size(); i++){
        if (contours[i].size() > sizeOfBiggestContour){
            sizeOfBiggestContour = contours[i].size();
            indexOfBiggestContour = i;
        }
    }
    return indexOfBiggestContour;
}

void shifcontour(vector<Point>& contour, int x, int y)
{
    for (size_t i = 0; i<contour.size(); i++)
    {
        contour[i].x += x;
        contour[i].y += y;
    }
}
int main()
{
    cout << "beginning";

    VideoCapture cap("pathaka.MP4");

    if (!cap.isOpened())  // check if we succeeded
        return -1;

Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
    for (;;)
    {
        Mat original, img;
        cap >> img;
        imshow("Source", img);

        Mat hsv;
        cvtColor(img, hsv, CV_BGR2GRAY);

        Mat bw;
        inRange(hsv, Scalar(0, 30, 80), Scalar(20, 150, 255), bw);
        GaussianBlur(bw, bw, Size(7, 7), 1.5, 1.5);
        Canny(bw, bw, 0, 30, 3);


        vector<vector<Point> > contours;
        vector<vector<Point> > convex_hull;
        vector<Vec4i> hierarchy;


        int erosion_type = MORPH_ELLIPSE;
        int erosion_size = 0;
        Mat element = getStructuringElement(erosion_type,
            Size(2 * erosion_size + 1, 2 * erosion_size + 1),
            Point(erosion_size, erosion_size));

        dilate(bw, bw, element);

        findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

        int s = findBiggestContour(contours);

        Mat drawing = Mat::zeros(img.size(), CV_8UC1);

        dilate(drawing, drawing, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(drawing, drawing, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        std::vector<cv::Point> cnt;
        cnt = contours[s];

        Moments M;
        M = cv::moments(cnt);

        cv::Point result;
        result = cv::Point(M.m10 / M.m00, M.m01 / M.m00);

        Point center(drawing.cols / 2, drawing.rows / 2);

        cv::circle(drawing, center, 3, Scalar(255, 255, 255), -1, 8, 0);


        int x;
        if (result.x > center.x)
        {
            x = result.x - center.x;
            x = -x;
        }
        else
        {
            x = result.x - center.x;
        }

        int y;

        if (result.y < center.y)
        {
            y = center.y - result.y;
        }
        else
        {
            y = center.y - result.y;
        }

        cout << "x:" << x << endl;
        cout << "y: " << y << endl;


        shifcontour(contours[s], x, y);

        drawContours(drawing, contours, s, Scalar(255), -1, 8, hierarchy, 0, Point());

        imshow("Hsv", drawing);
        if (waitKey(30) >= 0) break;
    }

    return 0;
}
edit retag flag offensive close merge delete

Comments

Debug Assertion Failed! Program: C:\Windows\system32\MSVCP120D.dll File: c:\program files\microsoft visual studio12.0\vc\include\vector Line:1201 Expression:vector subscript out of range

Lily gravatar imageLily ( 2016-03-02 05:36:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-03-02 05:45:46 -0600

berak gravatar image

updated 2016-03-02 06:04:58 -0600

if you apply inRange on a grayscale image, it will get thresholded in the [0..20] range (the scalar values you're using there, are for hsv, and not really suitable for grayscale), and probably give you a total black binary image. then, findContours won't find any, and there things start to break apart.

even with hsv as input, you have to take into account, that findContours might return an empty array, and adjust your code to that.

    findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    Mat drawing = Mat::zeros(img.size(), CV_8UC1);
    if (contours.size() > 0)
    {
         ... do your drawing stuff
    }
    imshow("Hsv", drawing);
    if (waitKey(30) >= 0) break;
edit flag offensive delete link more

Comments

I am now getting a total black image.

Lily gravatar imageLily ( 2016-03-02 05:57:44 -0600 )edit

yes, that is expected.

try a imshow("binary", bw);before calling findContours(), and adjust your inRange values until you get a nice binary image, then findContours() will work again.

berak gravatar imageberak ( 2016-03-02 06:03:39 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-02 05:23:28 -0600

Seen: 264 times

Last updated: Mar 02 '16