Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取java中一组图像的平均图像_Java_Image Processing_Bufferedimage_Argb - Fatal编程技术网

获取java中一组图像的平均图像

获取java中一组图像的平均图像,java,image-processing,bufferedimage,argb,Java,Image Processing,Bufferedimage,Argb,我有一套气象RGB类型的缓冲图像。我想得到他们的平均图像。我的意思是得到每个像素的平均值,然后用这些值生成一个新的图像。我尝试的是: public void getWaveImage(BufferedImage input1, BufferedImage input2){ // images are of same size that's why i'll use first one's width and height int width = input1.getWidth(),

我有一套气象RGB类型的缓冲图像。我想得到他们的平均图像。我的意思是得到每个像素的平均值,然后用这些值生成一个新的图像。我尝试的是:

public void getWaveImage(BufferedImage input1, BufferedImage input2){
   // images are of same size that's why i'll use first one's width and height
   int width = input1.getWidth(), height = input1.getHeight();

   BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

   int[] rgb1 = input1.getRGB(0, 0, width, height, new int[width * height], 0, width);
   int[] rgb2 = input2.getRGB(0, 0, width, height, new int[width * height], 0, width);
   for(int i=0; i<width; i++){
      for(int j=0; j<height; j++){
         int rgbIndex = i * width + j;
         rgb1[rgbIndex] = (rgb1[rgbIndex] + rgb2[rgbIndex]) / 2;
      }
   }

   output.setRGB(0, 0, width, height, rgb1, 0, width);
   return output;
}
我做错了什么?先谢谢你

输入1:

输入2:

输出:


您需要颜色的每个组成部分的平均值,平均红色、平均绿色、平均蓝色

相反,你要平均整个整数

Color c1 = new Color(rgb1[rgbIndex]);
Color c2 = new Color(rgb2[rgbIndex]);
Color cA = new Color((c1.getRed()   + c2.getRed())/2,
                     (c1.getGreen() + c2.getGreen())/2,
                     (c1.getBlue()  + c2.getBlue())/2);
rgb1[rgbIndex] = cA.getRGB();
由于创建的对象太多,这可能不是最有效的方法,因此更直接的方法如下:

public static int average(int argb1, int argb2){
   return (((argb1       & 0xFF) + (argb2       & 0xFF)) >> 1)       | //b
          (((argb1 >>  8 & 0xFF) + (argb2 >>  8 & 0xFF)) >> 1) << 8  | //g
          (((argb1 >> 16 & 0xFF) + (argb2 >> 16 & 0xFF)) >> 1) << 16 | //r
          (((argb1 >> 24 & 0xFF) + (argb2 >> 24 & 0xFF)) >> 1) << 24;  //a
}

您需要颜色的每个组成部分的平均值,平均红色、平均绿色、平均蓝色

相反,你要平均整个整数

Color c1 = new Color(rgb1[rgbIndex]);
Color c2 = new Color(rgb2[rgbIndex]);
Color cA = new Color((c1.getRed()   + c2.getRed())/2,
                     (c1.getGreen() + c2.getGreen())/2,
                     (c1.getBlue()  + c2.getBlue())/2);
rgb1[rgbIndex] = cA.getRGB();
由于创建的对象太多,这可能不是最有效的方法,因此更直接的方法如下:

public static int average(int argb1, int argb2){
   return (((argb1       & 0xFF) + (argb2       & 0xFF)) >> 1)       | //b
          (((argb1 >>  8 & 0xFF) + (argb2 >>  8 & 0xFF)) >> 1) << 8  | //g
          (((argb1 >> 16 & 0xFF) + (argb2 >> 16 & 0xFF)) >> 1) << 16 | //r
          (((argb1 >> 24 & 0xFF) + (argb2 >> 24 & 0xFF)) >> 1) << 24;  //a
}
如果您有:

int rgb1, rgb2; //the rgb value of a pixel in image 1 and 2 respectively
平均颜色为:

int r = (r(rgb1) + r(rgb2)) / 2;
int g = (g(rgb1) + g(rgb2)) / 2;
int b = (b(rgb1) + b(rgb2)) / 2;

int rgb = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
或者,如果您不想处理逐位操作,可以使用Color类。

如果您有:

int rgb1, rgb2; //the rgb value of a pixel in image 1 and 2 respectively
平均颜色为:

int r = (r(rgb1) + r(rgb2)) / 2;
int g = (g(rgb1) + g(rgb2)) / 2;
int b = (b(rgb1) + b(rgb2)) / 2;

int rgb = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);

或者,如果不想处理逐位操作,可以使用Color类。

另一种解决方案是替换

rgb1[rgbIndex] = (rgb1[rgbIndex] + rgb2[rgbIndex]) / 2;


二进制右移除以2,处理两个奇数情况的和的最后一个成员。

另一种解决方案是替换

rgb1[rgbIndex] = (rgb1[rgbIndex] + rgb2[rgbIndex]) / 2;


二进制右移除以2,是处理两个奇数情况的和的最后一个成员。

首先定义平均值的含义…我不认为使用rgb1[rgbIndex]+rgb2[rgbIndex]/2会给出两种输入颜色之间的颜色。@tschallaka对此表示抱歉,我现在添加了。首先定义你所说的平均值……我不认为使用rgb1[rgbIndex]+rgb2[rgbIndex]/2会让你得到两种输入颜色之间的颜色。@Tschallacka很抱歉,我现在添加了。很有趣!好好玩!