OpenCV:应用adaptiveThreshold函数后,最大值为-1

时间:2013-04-04 15:46:47

标签: java android opencv image-processing

我尝试使用adaptiveThreshold方法对RGB图像进行二值化。我的代码如下:

public byte[] filter(byte[] buff, int width, int height) {

    Mat img_rgb = new Mat(width, height, CvType.CV_8UC3);
    Mat img_gray = new Mat(width, height, CvType.CV_8U);
    Mat img_bin = new Mat(width, height, CvType.CV_8U);     

    img_rgb.put(0, 0, buff);        
    Imgproc.cvtColor(img_rgb, img_gray, Imgproc.COLOR_RGB2GRAY);                
    Imgproc.adaptiveThreshold(img_gray, img_bin, 255, 
               Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 5, 2);

    int size = (int) img_bin.total() * img_bin.channels();
    byte[] bin_buff = new byte[size];
    img_bin.get(0, 0, bin_buff);

    return bin_buff;

}

应用adaptiveThreshold后img_bin数据的最大值应为255,但它为-1。为什么会这样?我是OpenCV的新手,我找不到任何解释。

提前致谢。

1 个答案:

答案 0 :(得分:1)

有符号字节中的二进制补码表示为-1,等于无符号字节中的二进制补码表示。可能是您在代码中某处丢失了一个强制转换符。