使用p5.js实现RGB转CMYK时图像变黑、像素值被重置为0的问题求助
p5.js实现RGB转CMYK时图像变黑、像素值被重置为0的问题求助
我正在做一个图像滤镜项目,其中两个滤镜需要转换色彩空间。其他功能类似的函数都运行得很正常,但这个RGB转CMYK的函数却出了问题——当alpha值为255时,整个图像会变成纯黑色,本质上就是根据alpha值把图像变成单一的深色。
补充说明:我使用的是p5.min.js库
经过调试和查看打印的像素颜色信息,我发现这个函数似乎被循环执行了两次:第一次运行时一切正常,但第二次会把所有RGB值都设为0。我绞尽脑汁也找不到问题所在,目前只明确了这个现象。
下面是我的代码:
//CMYK Colour space conversion function rgbToCMYK(imgIn) { var imgOut = createImage(imgIn.width, imgIn.height); imgOut.loadPixels(); imgIn.loadPixels(); //Loop through pixels for (var x=0; x<imgIn.width; x++){ for (var y=0; y<imgIn.height; y++){ var index = (x + (y * imgIn.width)) * 4; var r = imgIn.pixels[index +0]; var g = imgIn.pixels[index +1]; var b = imgIn.pixels[index +2]; var a = imgIn.pixels[index +3]; //Just checking that the program is reading the pixel info correctly console.log(r, " ", g, " ", b, " ", a); //Convert rgb to CMY var computedC = 1 - (r/255); var computedM = 1 - (g/255); var computedY = 1 - (b/255); console.log(computedC, " ", computedM, " ", computedY); //Compute min CMY var minCMY = Math.min(computedC, Math.min(computedM, computedY)); //Complete Conversion computedC = (computedC - minCMY) / (1 - minCMY); computedM = (computedM - minCMY) / (1 - minCMY); computedY = (computedY - minCMY) / (1 - minCMY); console.log(computedC, " ", computedM, " ", computedY); //K is not used yet var k = minCMY; imgOut.pixels[index + 0]] = computedC; imgOut.pixels[index + 1] = computedM; imgOut.pixels[index + 2] = computedY; imgOut.pixels[index + 3] = 125; } } imgOut.updatePixels(); return imgOut; }
我只调用了这个函数一次,它接收一张图像作为参数,本应完成色彩空间转换后返回处理后的图像。我对色彩空间转换完全是新手,甚至不确定自己的实现思路是否正确。
备注:内容来源于stack exchange,提问作者Luke Clark
相关产品推荐
相关产品推荐

