Ask Your Question

Revision history [back]

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;

}

click to hide/show revision 2
No.2 Revision

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>

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

using namespace cv;

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

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;
}

}