Как я могу исправить эту ошибку: «Не удается вызвать loadPixels () для массива типа PImage []»? - PullRequest
0 голосов
/ 18 января 2019

Я новичок в обработке, и у меня возникли проблемы с массивом пикселей. У меня есть 10 изображений, пронумерованных от 0 до 9, которые показываются одно за другим ... Вдобавок ко всему, я пытаюсь сделать каждое изображение и изменить его уровень яркости, превратив его в белый или черный.

Я уже пытался просто изменить яркость, используя одно изображение, а не массив изображений, который работает отлично! Но когда я объединяю их вместе, это не работает для меня.

int maxImages = 10;  // The number of frames in the animation
int imageIndex = 00; //initial image to be displayed first
PImage[] picture = new PImage[maxImages]; //the image array

void setup() {
  size(500, 500); //size of sketch
  frameRate(1); //frames processed per second

  //loading images with numbered files into the array
  for (int i = 0; i< picture.length; i++) {
    picture[i] = loadImage("spast_" + i + ".jpg");
  }
} 

void draw() { 

  image(picture[imageIndex], 0, 0); //dispaying one image
  loadPixels(); //accessing pixels

  picture.loadPixels();  //accessing the image pixels too
 //THIS is where it stops me and gives me 'Cannot invoke loadPixels() on the array type PImage[]'

  for (int x = 0; x < width; x++) { //loops through every single x value
    for (int y = 0; y < height; y++) { //loops through every single y value
      int loc = x + y*width; // declare integer loc

      float b = brightness(picture.pixels[loc]); //give me the brightness of pixels
      if (b < 150) { //if the pixel is lower than 150
        pixels[loc] = color(0); //then make those pixels black
      } else { //otherwise
        pixels[loc] = color(255); //make pixels white
      }
    }
  }
  imageIndex = (imageIndex + 1) % picture.length; //increment image index by one each cycle
  updatePixels(); //when finished with the pixel array update the pixels
}

Я ожидаю, что при отображении каждого изображения значение яркости изменяется, а затем оно переходит к изображению 2 и т. Д. *

Ответы [ 2 ]

0 голосов
/ 18 января 2019

Это обновленный и рабочий код:

int maxImages = 10;  // The number of frames in the animation
int imageIndex = 0; //initial image to be displayed first
PImage[] picture = new PImage[maxImages]; //the image array

void setup() {
  size(500, 500); //size of sketch
  frameRate(1); //frames processed per second

  //loading images with numbered files into the array
  for (int i = 0; i< picture.length; i++) {
    picture[i] = loadImage("spast_" + i + ".jpg"); 
  }
} 

void draw() { 

  loadPixels(); //accessing pixels
  image(picture[imageIndex], 0, 0); //dispaying one image
  imageIndex = (imageIndex + 1) % picture.length; //increment image index by one each cycle

  picture[imageIndex].loadPixels();  //accessing the image pixels in the array
  for (int x = 0; x < width; x++) { //loops through every single x value
    for (int y = 0; y < height; y++) { //loops through every single y value
      int loc = x + y*width; // declare integer loc

      float b = brightness(picture[imageIndex].pixels[loc]); //give me the brightness of pixels
      if (b < 150) { //if the pixel is lower than 150
        pixels[loc] = color(0); //then make those pixels black
      } else { //otherwise
        pixels[loc] = color(255); //make pixels white
      }
    }
  }
  updatePixels(); //when finished with the pixel array update the pixels
}
0 голосов
/ 18 января 2019

Ваша переменная picture представляет собой массив из PImage экземпляров. Вы не можете вызвать loadPixels() для самого массива. Вы должны получить PImage по определенному индексу массива, а затем вызвать функцию loadPixels() для этого экземпляра.

Вот пример:

picture[0].loadPixels();

Эта строка кода получает PImage по индексу 0 и вызывает для него функцию loadPixels().

Обратите внимание, что вы уже делаете нечто похожее с этой строкой:

image(picture[imageIndex], 0, 0);

Бесстыдная самореклама: здесь - учебник по массивам в обработке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...