极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12064|回复: 2

初学processing。做的官网上的images and pixels里面的例子,遇到问题求大神!!

[复制链接]
发表于 2012-8-11 14:13:03 | 显示全部楼层 |阅读模式
本帖最后由 liang647 于 2012-8-11 14:14 编辑

求教大神!!请问这段代码有什么问题?为什么不能出现完整的图像,而全是条纹???
附上使用的图片。[pre lang="processing" line="1"]PImage img;
void setup() {
  size(400, 400);
  img = loadImage("bird.jpg");
}

void draw() {
  loadPixels();
  
  img.loadPixels();
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int loc = x + y*width;
      
      float r = red(img.pixels[loc]);
      float g = green(img.pixels[loc]);
      float b = blue(img.pixels[loc]);
      
      pixels[loc] =color(r,g,b);
      //上四行代码改为pixels[loc]=img.pixels[loc];后也没有解决
    }
  }
  updatePixels();
}[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2012-8-11 14:30:31 | 显示全部楼层
T T。。以自己解决。。。弱智贴就不删了。。给其他人个警示。。
出现错误的原因是图片和画布区域大小不一样。。对于画布和图片的像素位置应该分别计算。附上改后的代码:
  1. PImage img;
  2. void setup() {
  3.   size(500,500);
  4.   img = loadImage("bird.jpg");
  5. }

  6. void draw() {
  7.   loadPixels();
  8.   
  9.   img.loadPixels();
  10.   for (int y = 0; y < height; y++) {
  11.     for (int x = 0; x < width; x++) {
  12.       int imageLoc = x + y*img.width;
  13.       int displayLoc = x + y*width;
  14.       
  15.       float r = red(img.pixels[imageLoc]);
  16.       float g = green(img.pixels[imageLoc]);
  17.       float b = blue(img.pixels[imageLoc]);
  18.       
  19.       pixels[displayLoc] =color(r,g,b);
  20.       
  21.     }
  22.   }
  23.   updatePixels();
  24. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2012-8-14 20:49:32 | 显示全部楼层
多做多学习就好了,有空多发贴,交流一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-17 06:29 , Processed in 0.048158 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表