opencv – IplImage Pixel Access JavaCV

我正在尝试访问Ipl Image的Pixel by Pixel.我使用 Java和Processing,有时我需要逐像素访问.到目前为止我已经这样做了,但我不知道出了什么问题:
public IplImage PImagetoIplImage(PImage imageSrc)
    {
        IplImage imageDst;
        if(imageSrc.format==RGB)
        {
            imageDst = IplImage.create(imageSrc.width,imageSrc.height,IPL_DEPTH_8U,3);
            ByteBuffer imagePixels=imageDst.getByteBuffer();
            int locPImage,LociplImage,x,y;
            for(y=0; y<imageSrc.height; y++)
                for(x=0; x<imageSrc.width; x++)
                {
                    locPImage = x + y * width;
                    LociplImage=y*imageDst.widthStep()+3*x;
                    imagePixels.put(LociplImage+2,(byte)(red(imageSrc.pixels[locPImage])));
                    imagePixels.put(LociplImage+1,(byte)(green(imageSrc.pixels[locPImage])));
                    imagePixels.put(LociplImage,(byte)(blue(imageSrc.pixels[locPImage])));
                }
        }
}

在Karlphilip消化之后,我来到这里,仍然没有工作.当我尝试显示时,它给了我一个nullPointer异常:

imageDst = IplImage.create(imageSrc.width,3);
CvMat imagePixels = CvMat.createHeader(imageDst.height(),imageDst.width(),CV_32FC1);  
cvGetMat(imageDst,imagePixels,null,0); 
int locPImage,y;
for(y=0; y<imageSrc.height; y++)
   for(x=0; x<imageSrc.width; x++)
   {
       locPImage = x + y * width;
       CvScalar scalar = new CvScalar();
       scalar.setVal(0,red(imageSrc.pixels[locPImage]));
       scalar.setVal(1,green(imageSrc.pixels[locPImage]));
       scalar.setVal(2,blue(imageSrc.pixels[locPImage]));
       cvSet2D(imagePixels,y,scalar);
   }
   imageDst = new IplImage(imagePixels);

解决方法

迭代JavaCV中每个像素的最快方法是:
ByteBuffer buffer = image.getByteBuffer();

for(int y = 0; y < image.height(); y++) {
    for(int x = 0; x < image.width(); x++) {
        int index = y * image.widthStep() + x * image.nChannels();

        // Used to read the pixel value - the 0xFF is needed to cast from
        // an unsigned byte to an int.
        int value = buffer.get(index) & 0xFF;

        // Sets the pixel to a value (greyscale).
        buffer.put(index,value);

        // Sets the pixel to a value (RGB,stored in BGR order).
        buffer.put(index,blue);
        buffer.put(index + 1,green);
        buffer.put(index + 2,red);
    }
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...