liuliu / ccv

C-based/Cached/Core Computer Vision Library, A Modern Computer Vision Library
http://libccv.org
Other
7.08k stars 1.72k forks source link

ccv_sobel() bug on jpg file? #162

Closed dodoma closed 8 years ago

dodoma commented 8 years ago

The ccv_sobel() on a ccv_read() dense matrix don't act with expect on my rasperberry pi.

here is my code:

#include <ccv.h>

int main()
{
    ccv_dense_matrix_t *mta, *mtb;

    mta = mtb = NULL;

    ccv_read("x.jpg", &mta, CCV_IO_ANY_FILE);

    ccv_sobel(mta, &mtb, 0, 0, 1);
    ccv_write(mtb, "y.jpg", NULL, CCV_IO_JPEG_FILE, NULL);
}

The opencv's version woks ok:

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

int main( int argc, char** argv )
{
    cv::Mat src = cv::imread("x.jpg", 0);
    cv::Mat dst;

    cv::Sobel(src, dst, src.depth(), 0, 1);

    cv::imwrite("z.jpg", dst);

    return 0;
}

$file x.jpg output:

x.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 2400x1745, frames 3

y.jpg is about 4 times larger than x.jpg while z.jpg almost same as x.jpg.

seem's mtb->step = 4 * mta->step.

dodoma commented 8 years ago

Here is my x.jpg https://www.dropbox.com/s/cjgdr3iworurk12/x.jpg?dl=0

dodoma commented 8 years ago

And, this is y.jpg, z.jpg

liuliu commented 8 years ago

sobel operator's default data type is 32bit signed integer: https://github.com/liuliu/ccv/blob/stable/lib/ccv_basic.c#L14

Which is more correct considering the range will be between -255 to 255. You can force it to use CCV_8U by calling:

ccv_sobel(mta, &mtb, CCV_8U, 0, 1);

On the other hand, you can also call ccv_visualize: http://libccv.org/lib/ccv-util/#ccvvisualize to get the desired image.

dodoma commented 8 years ago

It's ok now. Seems I need some basic concept learning.