Order pixels in an Image
in
Programming Questions
•
10 months ago
Hi all, I am having some difficulty with a quick program I'm trying to make. My idea is to take all of the pixels in an image and order them based on the intensity of their colors (from here I want to try experimenting with Perlin Noise, using the colors that I collect from the image).
I can't tell if I am making any serious errors or not with my code; it seems to compile, but it freezes. Maybe it is too computationally intensive? If there is a way around using the draw function, that would be nice, but I'm not sure how else to update the pixels.
Here is my current code for reference:
- PImage sunset;
- int orderedRedColors[] = new int[1280*960];
- void setup(){
- size(1280,960,P2D);
- sunset = loadImage("Sunset.jpg");
- int redColorArray[] = new int[orderedRedColors.length];
- for(int i=0;i<redColorArray.length;i++){
- redColorArray[i] = (int)red(sunset.pixels[i]);
- }
- orderRedColors(redColorArray);
- }
- void draw(){
- loadPixels();
- for(int i=0;i<pixels.length;i++){
- pixels[i] = color(orderedRedColors[i],0,0);
- }
- updatePixels();
- }
- void orderRedColors(int[] redColorArray){
- int i, j, temp;
- for(i=0;i<orderedRedColors.length;i++){
- orderedRedColors[i] = redColorArray[i];
- }
- for(i=0;i<orderedRedColors.length;i++){
- for(j=1;j<orderedRedColors.length - 1;j++){
- if(orderedRedColors[i]>orderedRedColors[j]){
- temp = orderedRedColors[i];
- orderedRedColors[i] = orderedRedColors[j];
- orderedRedColors[j] = temp;
- }
- }
- }
- }
1