Ask Your Question
1

16-bit conversion problem

asked 2012-12-30 06:04:10 -0500

updated 2013-01-01 07:36:07 -0500

Vladislav Vinogradov gravatar image

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;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-01-01 07:39:02 -0500

Vladislav Vinogradov gravatar image

Use this code to iterate over Mat's elements:

for (int i = 0; i < Ix.rows; ++i)
{
    unsigned short* Ix_row = Ix.ptr<unsigned short>(i);
    unsigned short* Ix_Ix_row = Ix_Ix.ptr<unsigned short>(i);
    for (int j = 0; j < Ix.cols; ++j)
    {
        printf("%d, %d \n", (int) Ix_row[j], (int) Ix_Ix_row[j])
    }
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2012-12-30 06:04:10 -0500

Seen: 135 times

Last updated: Jan 01 '13