How do you convert an image (PNG) to a 2D Array (binary image)?

后端 未结 2 794
野趣味
野趣味 2021-01-16 08:50

I need a 2D binary representation of the following PNG image:

Can anyone provide a method or a source to convert PNG image above to a 2D array where 1 = lan

2条回答
  •  囚心锁ツ
    2021-01-16 09:03

    Here's the relevant part if you want to access individual pixels as a 2D array

    var w = canvas.width * 4;
    var r,g,b;
    
    for (var i=0; i= 164 && r <= 255) && 
            (g >= 191 && g <= 229) && 
            (b >= 220 && b <= 255)) {
    
          // blue  = water
          imdata[j*w+i] = 0;
          imdata[j*w+i+1] = 0;
          imdata[j*w+i+2] = 150;
    
        } else {
    
          // green = land
          imdata[j*w+i] = 0;
          imdata[j*w+i+1] = 120;
          imdata[j*w+i+2] = 0;                      
        }                   
      }
    }
    

提交回复
热议问题