如何将图像转换为Redscale?

时间:2011-05-12 09:45:51

标签: c opencv

我正在使用OpenCV中图像的光线和颜色。我遇到了红点效应。如何在OpenCV中实现它?我已经尝试了几种方法来操纵像素RGB值。但我觉得我可能需要使用HSV色彩空间......请帮忙。

5 个答案:

答案 0 :(得分:2)

我设法创建了将图片转换为类似于redscale effect的模式的Javascript。

修改
我简化了红色效果并使其更接近“真实”效果。

<html>
<body>

<script type='text/javascript'> 
function AppyRedscale(img) {
  width = 200;
  height = 150;
  canvas = document.getElementById("canvas");
  cnt = canvas.getContext("2d");
  cnt.drawImage(img,0,0);
  imageData = cnt.getImageData(0,0,width, height);
  imageDataNew = cnt.createImageData(width, height);

  for (x = 0; x < width; x++) {
    for (y = 0; y < height; y++) {
      rgb = getPixel(imageData,x,y);
      k = rgb[2]/255;
      rgb[0] = Mix(k,rgb[0],255);
      rgb[1] = Mix((1-k)*(1-k),rgb[1],0);
      rgb[2] = Mix(k,rgb[2],0);
      setPixel(imageDataNew, x, y, rgb, 0xff);
    }
  }

  cnt.putImageData(imageDataNew, 0, 0);

 };

function Mix(k,x,y) {
  return (1-k)*x + k*y;
};

function setPixel(imageData, x, y, rgb, a) {
    index = (x + y * imageData.width) * 4;
    imageData.data[index+0] = rgb[0];
    imageData.data[index+1] = rgb[1];
    imageData.data[index+2] = rgb[2];
    imageData.data[index+3] = a;
};

function getPixel(imageData, x, y) {
    index = (x + y * imageData.width) * 4;
    return [imageData.data[index+0],
            imageData.data[index+1],
            imageData.data[index+2]
            ];
};
</script> 

<canvas id="canvas" width="400" height="300"
          style="position: absolute; left: 8; top: 170; z-index: 1;"><br /> 
  Sorry, canvas not supported!<br /> 
</canvas>

<img id="img" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gODUK/9sAQwADAgIDAgIDAwMDBAMDBAUIBQUEBAUKBwcGCAwK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sRCS0ohNhYnfqf73OBgYbZXq5SkRQHFmySU7i4t6WxR3EmmNQahNdZbQhzXzVEAD5khW23+9+nU4GBjQxD5lTyfgCx9xV4UTJ/ESZUlVRKETOWsNFJVy9WlFgdtrp+wt1tiNV3gzU4cDmqqrC0ggAaFXHpgYGCBpIAQ9KLepfwfpLlLyxm6Mt0PqfhOoKvlA/Z77b+uKXy9mJdEqrsptlC5AaUhCz+6dt/wCf54GBh0fO5Nk/oUroFXq+ZkSZJmaSt5MdQKin0O1ug6YvB3hNUGIhJqpAUkEpDqiL/cYGBi9GxpaeFRe9wd1ULzTS28t0evMBKXlhhKAs9iXEG/8A0/rgYGBhdjaHCif8RX//2Q%3D%3D" width="200" height="150" onload="AppyRedscale(this)"/>

</body>
</html>

JsFiddle Demo

答案 1 :(得分:2)

处理它很容易。

void redscale() {

  loadPixels();      

  for (int i = 0; i < pixels.length; i++) {
  int r = (int) red(pixels[i]);
  int g = (int) red(pixels[i]);
  int b = (int) red(pixels[i]);    
  int average= (r+g+b)/3;

  pixels[i]= color (255, average, 0);

  updatePixels()

 }
}

答案 2 :(得分:0)

在0x69的笔记上需要信用的答案。我非常想说他确实没有回答这个问题,这个问题涉及OpenCV。

Anyhoot ...我第一次尝试在StackOverFlow上找到答案,对我来说很光鲜。是的,我知道问题是“旧的”,但我发现我可能会对这个话题有所了解。 我的答案包含的是如何隔离通道,并相互减去通道的方法。

我的项目组和我正处于涉及c ++和OpenCV 2.3的项目中 (版本非常重要,因为OpenCV 2.1在正常c中编码。但算法没有区别)

我们需要做的是分离绿色,然后减去红色和蓝色通道,这就是我们做到的:

void IsolateGreen(Mat mIn, Mat& mOut) 

{

Mat inImg (mIn.rows, mIn.cols, CV_8UC3, Scalar(1,2,3));
inImg.data = mIn.data;
Mat channelRed (inImg.rows, inImg.cols, CV_8UC1);
Mat channelGreen (inImg.rows, inImg.cols, CV_8UC1);
Mat channelBlue (inImg.rows, inImg.cols, CV_8UC1);
Mat outImg[] = {channelRed, channelGreen, channelBlue};

int fromTo[] = { 0,2, 1,1, 2,0};
mixChannels( &inImg, 1, outImg, 3, fromTo, 3);

mOut = (channelGreen) - (channelRed + channelBlue);

threshold(mOut, mOut, 10, 255, THRESH_BINARY);

erode(mOut, mOut, Mat(), Point (-1,-1), 1);
dilate(mOut, mOut, Mat(), Point(-1,-1), 3);

}

我认为你可以使用的是它的这些部分

void IsolateGreen(Mat mIn, Mat& mOut) 
{
    Mat inImg (mIn.rows, mIn.cols, CV_8UC3, Scalar(1,2,3));
inImg.data = mIn.data;
Mat channelRed (inImg.rows, inImg.cols, CV_8UC1);
Mat channelGreen (inImg.rows, inImg.cols, CV_8UC1);
Mat channelBlue (inImg.rows, inImg.cols, CV_8UC1);
Mat outImg[] = {channelRed, channelGreen, channelBlue};

int fromTo[] = { 0,2, 1,1, 2,0};
mixChannels( &inImg, 1, outImg, 3, fromTo, 3);

mOut = channelGreen;
}

这里的关键部分是mixChannels功能, 了解更多 http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html?highlight=mixchannels#mixchannels

花了一些时间才明白。结果将给出一个或多个代表各个通道的灰度图像。 (你总是可以进行类似的转换以获得你想要的频道,混合到RGB画面中的单个频道,如果你想能够看到它是红色的。这都是关于强度的,呃:3)

答案 3 :(得分:0)

我会这样做(逐像素):

  1. 转换为灰度(暂时忽略透明度)
  2. 将RGB值设置为您要替换为黑色的颜色(在这种情况下为红色)
  3. 使用灰度作为透明度(255 - grayscale作为不透明度)

伪代码:

const grayscale = inputRed * 0.2126 + inputGreen * 0.7152 +
    inputBlue * 0.0722
const outputRed = 255
const outputGreen = 0
const outputBlue = 0
const outputAlpha = (255 - grayscale) * inputAlpha / 255

注意:包括alpha在内的所有输入都被认为在0到255之间,因此outputAlpha也是如此。 如果您使用0到1之间的alpha,则需要修改代码。

答案 4 :(得分:-1)

我已经以非常简单的方式将此代码转换为opencv。因为opencv认为颜色是bgr而不是rgb,所以我做了一些改动。代码工作正常......如果有任何错误,请编辑。

int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "D:/test.jpg" );
    cvShowImage("Input", img );
    int x,y;
    double k;
    CvScalar s;

    for (x = 0; x < img->width;  x++ )
    {
        for(y = 0; y < img->height;  y++)
        {

            s = cvGet2D(img, y, x);
            k = s.val[0]/255;

            s.val[0] = (1-k) * (1-k) * s.val[0];
            s.val[1] = k * s.val[1];
            s.val[2] = (1-k) * s.val[2] + k * 255;

            cvSet2D(img, y, x, s);

        }
    }
    cvShowImage("Output", img );
    cvWaitKey(0);

    cvReleaseImage(&img);   
    return 0;
}