Ask Your Question

Siddharth Agrawal's profile - activity

2013-06-03 00:44:42 -0600 commented question findContours() error with cairo

As it turns out there was nothing wrong with my code. It was giving a problem just because my terminal always loads ROS bindings.

2013-05-31 09:13:48 -0600 asked a question findContours() error with cairo

Hi, I'm using Kinect with OpenCV 2.4.5 using freenect. Below is my code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <libfreenect.h>
#include <pthread.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <iostream>

using namespace cv;
using namespace std;

Mat img_depth;
Mat img_rgb;
Mat img_temp;
Mat img_canny;
Mat img_canny_temp;

pthread_mutex_t mutex_depth = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_rgb = PTHREAD_MUTEX_INITIALIZER;
pthread_t cv_thread;

//callback for depthimage, called by libfreenect
void depthCallback(freenect_device *dev, void *depth, uint32_t timestamp)
{
    Mat depth8;
    Mat mydepth = Mat(Size(640, 480), CV_16UC1, depth);

    mydepth.convertTo(depth8, CV_8UC1, 1.0/4.0);
    pthread_mutex_lock(&mutex_depth);
    memcpy(img_depth.data, depth8.data, 640*480);

    //unlock mutex
    pthread_mutex_unlock(&mutex_depth);

}

//callback for rgbimage, called by libfreenect
void RGBCallback(freenect_device *dev, void *rgb, uint32_t timestamp)
{
    //lock mutex for opencv rgb image
    pthread_mutex_lock(&mutex_rgb);

    memcpy(img_rgb.data, rgb, FREENECT_VIDEO_RGB_SIZE);

    //unlock mutex
    pthread_mutex_unlock(&mutex_rgb);
}

/*
 * thread for displaying the opencv content
 */
void *cv_threadfunc(void *ptr)
{
    img_depth = Mat(Size(640, 480), CV_8UC1);
    img_rgb = Mat(Size(640, 480), CV_8UC3);
    img_temp = Mat(Size(640, 480), CV_8UC3);
    img_canny = Mat(Size(640, 480), CV_8UC1);
    img_canny_temp = Mat(Size(640, 480), CV_8UC1);

    //use image polling
    while(1)
    {
        //lock mutex for depth image
        pthread_mutex_lock(&mutex_depth);

        vector< vector<Point> > contours;

        Canny(img_depth, img_canny, 60, 110);

        findContours(img_canny, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

        imshow("First",img_thresh);
        imshow("Canny", img_canny);

        //unlock mutex for depth image
        pthread_mutex_unlock(&mutex_depth);

        //lock mutex for rgb image
        pthread_mutex_lock(&mutex_rgb);

        //show image to window
        cvtColor(img_rgb,img_temp,CV_BGR2RGB);

        imshow("Second", img_temp);

        //unlock mutex
        pthread_mutex_unlock(&mutex_rgb);

        //wait for quit key
        if(cvWaitKey(15) == 27)
        {
            break;
        }
    }

    pthread_exit(NULL);

    return NULL;

}

int main(int argc, char **argv)
{
    freenect_context *f_ctx;
    freenect_device *f_dev;

    int res = 0;
    int die = 0;

    printf("Kinect camera test\n");

    if(freenect_init(&f_ctx, NULL) < 0)
    {
        printf("freenect_init() failed\n");
        return 1;
    }

    if(freenect_open_device(f_ctx, &f_dev, 0) < 0)
    {
        printf("Could not open device\n");
        return 1;
    }

    freenect_set_depth_callback(f_dev, depthCallback);
    freenect_set_video_callback(f_dev, RGBCallback);
    freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);

    //create opencv display thread
    res = pthread_create(&cv_thread, NULL, cv_threadfunc, NULL);
    if(res)
    {
        printf("pthread_create failed\n");
        return 1;
    }

    printf("init done\n");

    freenect_start_depth(f_dev);
    freenect_start_video(f_dev);

    while(!die && freenect_process_events(f_ctx) >= 0);
}

The error I'm getting is as follows:

/build/buildd/cairo-1.10.2/src/cairo-surface.c:1287: cairo_surface_set_device_offset: Assertion `status == CAIRO_STATUS_SUCCESS' failed. Aborted (core dumped)

I spent 5 hours on the internet searching for this, but still am clueless about this. Please help.

2013-01-03 04:58:09 -0600 received badge  Student (source)
2012-12-30 06:04:10 -0600 asked a question 16-bit conversion problem

While printing values from a 16-bit Mat I'm getting only the lower 8-bit part of the number. For example if the number is 324, I'm getting the printed value as 68 which is 324-256. In the code below Ix_Ix is the element-wise multiplication of Ix, but it does not print values greater than 255. Instead it gives the lower 8 bits. Is there a problem with my code? How do I get the correct values?

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

int main()
{
    Mat orig_img, img_blur, img_gray;

    orig_img = imread("Lenna.png");

    GaussianBlur(orig_img, img_blur, Size(3,3), 0, 0, BORDER_DEFAULT);

    cvtColor(img_blur, img_gray, CV_RGB2GRAY);

    namedWindow("features", CV_WINDOW_AUTOSIZE);

    Mat Ix, Iy;

    Sobel(img_gray, Ix, CV_16U, 1, 0, 3, 1, 0, BORDER_DEFAULT);
    Sobel(img_gray, Iy, CV_16U, 0, 1, 3, 1, 0, BORDER_DEFAULT);

    Mat Ix_Ix, Iy_Iy, Ix_Iy;

    Ix_Ix = Ix.mul(Ix);
    Iy_Iy = Iy.mul(Iy);
    Ix_Iy = Ix.mul(Iy);

    int i, j;

    for(i = 0; i < Ix_Ix.rows; i++)
    {
        for(j = 0; j < Ix_Ix.cols; j++)
        {
            printf("%d\n", Ix.data[Ix.step[0]*i + Ix.step[1]*j]);
            printf("%d\n", Ix_Ix.data[Ix_Ix.step[0]*i + Ix_Ix.step[1]*j]);
            printf("\n");
        }
    }

    Mat Ix_Ix_blur, Iy_Iy_blur, Ix_Iy_blur;

    GaussianBlur(Ix_Ix, Ix_Ix_blur, Size(5,5), 0, 0, BORDER_DEFAULT);
    GaussianBlur(Iy_Iy, Iy_Iy_blur, Size(5,5), 0, 0, BORDER_DEFAULT);
    GaussianBlur(Ix_Iy, Ix_Iy_blur, Size(5,5), 0, 0, BORDER_DEFAULT);

    imshow("feature", Ix_Ix);

    waitKey(0);

    return 0;
}