开发者

How to convert an Image to Redscale?

I was playing with the light and color of pixels of an image in OpenCV. I came across the redsca开发者_如何学Gole effect. How to implement it in OpenCV? i have tried several ways to manipulating the pixel RGB values. But i have a feeling that i might need to use HSV color space... pls help.


I managed to create Javascript which converts image into mode similar to redscale effect.

EDIT:
I simplified redscale effect and made it closer to the 'real' one.

<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/9sAQwADAgIDAgIDAwMDBAMDBAUIBQUEBAUKBwcGCAwKDAwLCgsLDQ4SEA0OEQ4LCxAWEBETFBUVF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i4t6WxR3EmmNQahNdZbQhzXzVEAD5khW23+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


in Processing it's so easy.

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()

 }
}


I would do it like this (pixel by pixel):

  1. convert to grayscale (ignoring transparency for now)
  2. set the RGB values to the color you want to replace black with (red in this case)
  3. use the grayscale as transparency (255 - grayscale as opacity)

Pseudocode:

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

Note: all inputs including alpha are considered between 0 and 255 and so outputAlpha will be too. You'll need to adapt the code if you work with alpha between 0 and 1.


On the note of 0x69 wanted credit for the answer. I'd very much say that he did in fact not answer the question, which refers to OpenCV.

I know that the question is 'old', but I found I might shed some light on the topic. That which my answer contains, is the approach on how to isolate channels, and subtract these from each other.

My project group and I are in the midst of a project involving c++ and OpenCV 2.3 (version is very important, since OpenCV 2.1 is coded in normal c. The algorithm however does not differ)

What we needed to do was isolate green, and then subtract the red and blue channels, this was how we did it:

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);

}

That which I was thinking you could use was these parts of it

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;
}

The essential part here is the mixChannels function, read more on http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html?highlight=mixchannels#mixchannels

It took some time to understand. And the result will give one or more grayscale images representing individual channels. (You can always do a similar conversion to have the channel you want, mixed to a single channel in an RGB picture, if you want to be able to see that it is red. It's all about intensities, eh :3 )


I have converted this code to opencv in a very simple way. since opencv considers the color as bgr and not rgb, i made some changes. the code works fine ...please edit if there is any mistake.

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜