当前位置: 首页 > 知识库问答 >
问题:

处理错误:无法在原始类型int上调用模糊图像(int, int, int)

明利
2023-03-14

我最近做了一个程序在处理Java加载图像和模糊它,但现在我把这个函数作为一个方法到一个类,得到了这个错误:

无法在原始类型int上调用blurImage(int,int,int)

我的班级:

//create class called ImageProcessing where all image editing methonds will be
class ImageProcessing {
  //create function "blurImage" which takes in an xOffset, a yOffset, and a blur amount
  //xOffset and yOffset is changing where the grid starts so the make the blurring algorithm less pixelated using other methods when calling this function
  //the blur amount is how big each box in the grid is
  void blurImage(int xOffset, int yOffset, int blurAmount) {
    //loop through x in the image starting at the negative xOffset and incrementing by the blurAmouint / 2
    //the negative xOffset's purpose is to start the offset off the grid so as to blur the whole image no matter the offset
    //incrementing by the blurAmount / 2 is done because it needs to go in both directions
    for (int x = -xOffset; x < pic.width; x += blurAmount / 2) {
      //loop through the same way for the y so that we can got though in 2 dimentions instead of just 1
      for (int y = -yOffset; y < pic.height; y += blurAmount / 2) {
        //create a 2D array called p that is the size of the blur amount, the size of the box; this is the box
        color[][] p = new color[blurAmount][blurAmount];
        //initialize variables that are the averages of each color channel
        int redAverage = 0;
        int greenAverage = 0;
        int blueAverage = 0;

        //loop through the blurAmount in both dimentions to refrence all the pixels in the p array
        for (int a = 0; a < blurAmount; a++) {
          for (int b = 0; b < blurAmount; b++) {
            //fill the array with the colors of the current box on the grid so i can refrence them easily
            p[a][b] = pic.get(x + a, y + b);
            //these lines add up the Red Green and Blue channels into their individual variables
            //the "(p[a][b] >> 16) & 0xFF" is first shifting the hexidecimal number over into the correct position and then chopping off the extra bits to the right
            redAverage += (p[a][b] >> 16) & 0xFF;
            greenAverage += (p[a][b] >> 8) & 0xFF;
            blueAverage += p[a][b] & 0xFF;
          }
        }

        //dividing the red averages by the square of the blurAmount
        //the squaring is needed to find the area in pixels, this makes the average
        redAverage /= (int)(blurAmount * blurAmount);
        greenAverage /= (int)(blurAmount * blurAmount);
        blueAverage /= (int)(blurAmount * blurAmount);

        //create color from the average values
        color blur = color(redAverage, greenAverage, blueAverage);

        //loop through the current box on the grid the same way as before
        for (int a = 0; a < blurAmount; a++) {
          for (int b = 0; b < blurAmount; b++) {
            //setting all the pixels in the box to the color of the averages
            //the reason for the "x + a" and the "y + b":
            //find the location of the pixels in the box relative to the top left corner of it
            pic.set(x + a, y + b, blur);
          }
        }
      }  
    }
  }
}

我的代码调用它:

/*
//The purpose of this program is to create a blurring algorithm that can
//adjust to pixelate or blur the image at different levels.
//
//We first need to split th image into a grid and work with each box on the grid individually.
//After this we get all the color values from each pixel in the box and store it in our code.
//We can then add all the reds together, the greens together, and the blues together and divide each by the number of pixels.
//Next, we turn all the three average into a color variable and set all the pixels in the current box the that color
//
//If we run this multiple times, we can change the blur amount each time and offset the grid to get less pixelated results, we can get a smoother image.
//
//Kwown Issues:
//none so far
//
//Created by Matthew Teta
*/

//create instance of my ImageProcessing claa
ImageProcessing i = new ImageProcessing();

//Make sketch full screen
boolean sketchFullScreen() {
  return true;
}

//initialize image
PImage pic;

void setup() {
  //load image from file and save it into pic PImage
  pic = loadImage("image.jpg");
  //create window at size of picture
  size(pic.width, pic.height);
  background(255);
  frameRate(1);
}
//maxBlur is the starting variable for the blur; TL;DR sets the first size of the grid boxes; also used for the temperary blur value running later in my code
float maxBlur = 16;
//initialize the blurDecrement
int blurDecrement = 0;
//create variable that is the amount of times the blurring algorithm is run
//RECOMENDATION: make the blur half of the maxBlur
//the lower the value the more pixelated
int blur = 1;
//simple boolean so that my code for running the blur is only run once at the beginning
boolean first = true;

void draw() {
  //calling "first()" if this is the first iteration
  if (first) {
    first();
  }

  //save image in file called "blurred-img" as a jpg
  pic.save("blurred-img.jpg");

  //setting the variable for runniong the code once to false so that is won't be run again
  first = false;
}

//create function first that is only run one time when the program is run
void first() {  
  //first find the blurDecrement so that we will get a smooth transition for the number of iterations of the blur algorithm we have
  blurDecrement = (int)(maxBlur / blur);

  //iterate through blur, the number of times to blur
  for (int i = 0; i < blur; i++) {
    //run blur algorithm at the blurDecrement for the x and y offsets using the maxBlur
    //remember: the maxBlur is also used to store the value after being decremented in the loop
    i.blurImage(blurDecrement, blurDecrement, round(maxBlur));
    //draw the image on the canvas at the top left point, (0, 0)
    image(pic, 0, 0);

    //decrement maxBlur by the blurDecrement value
    maxBlur -= blurDecrement;
  }
}

错误就在这条线上

i、 blurImage(blurDecrement,blurDecrement,round(maxBlur));

我已经在互联网上搜索了这个问题,但没有得到我想要的东西。提前感谢您的回复!

共有1个答案

孙思源
2023-03-14

提示在下面的循环中,您在< code>INT i = 0中将< code>i声明为int。记住,局部变量优先于全局变量。尝试将对象< code>i重命名为类似于< code>object_i的名称。即< code > image processing object _ I = new image processing();

for (int i = 0; i < blur; i++) {

    i.Func(foo);// the problem = object name is same as int variable name.

//stuff
}
 类似资料:
  • 想改进这个问题吗 通过编辑此帖子,添加详细信息并澄清问题。 我对下面的if语句块有问题:

  • 我一直在尝试移植一段JavaScript代码,现在,我唯一尚未开始工作的部分出现了此错误:无法在原始类型int上调用padLz(int)。原始JavaScript代码在下面,同样是我的移植版本,我还无法工作的部分还突出显示。 错误: 无法在基元类型 int 上调用 padLz(int) 原件 移植版本 string grid ref = let pair ' ' e . padlz(digits/

  • 不知道如何让不出错。 非常迷茫。已尝试更改多行代码。 我希望它能够与< code > compare to(new number)没有问题。 错误消息:“无法对基本类型int调用< code>compareTo(int)

  • 我目前正在学习如何使用遗传算法。然而,我在使用我的方法时遇到了困难。这种方法应该比较两个人之间的适应值。我一直试图调用我的

  • 我有这样一个班级: 我想将数组迭代分解为一个单独的方法,以便在整个程序中重复使用或重新调用它,如下所示: 然后我想在我的公共方法中调用它,例如: 我在这里尝试了解决方案:无法通过添加但它仍然不工作。我有所有可用变量的setter和getter。我相信有一个简单的解决办法,但请让我知道,如果有办法绕过它。 谢谢你

  • 我们必须制作一个程序,使用GUI将二进制数转换成十进制数。首先,我在不使用图形用户界面的情况下制作了这个程序,它运行得很好。然后我试着使用GUI,在编写程序的时候,我一直得到一个错误消息,说“不能在原始类型int上调用length()” 任何输入都非常感谢。谢谢! 这是我在没有尝试做GUI的情况下工作时拥有的