0
votes

I have this task in interchanging the colors of an image. Changing Blue to Red, Red to Green, and Green to Blue. I have tried 3 approaches, but all of them failed. Below are the three approach.

Splitting RGB to get their values and interchange them. All I got is an BW images.

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "rgb.h"

using namespace std;

int main()
{
  char infname[256];

  cout << "Enter input image  : ";
  cin >> infname;

  IplImage *img = cvLoadImage(infname, 0);
  RgbImage pic(img);
  int H = img->height;
  int W = img->width;

IplImage* channelRed = cvCreateImage(cvGetSize(img), 8, 1);
IplImage* channelGreen = cvCreateImage(cvGetSize(img), 8, 1);
IplImage* channelBlue = cvCreateImage(cvGetSize(img), 8, 1);

IplImage* blue = cvCreateImage(cvGetSize(img), 8, 1);
IplImage* green = cvCreateImage(cvGetSize(img), 8, 1);
IplImage* red= cvCreateImage(cvGetSize(img), 8, 1);

cvSplit(img, channelBlue, channelGreen, channelRed, NULL);

cvThreshold(channelBlue, blue, 20, 255, CV_THRESH_BINARY);
cvThreshold(channelGreen, green, 20, 255, CV_THRESH_BINARY);
cvThreshold(channelRed, red, 20, 255, CV_THRESH_BINARY);

cvShowImage("original", img);

cvShowImage("blue", blue);
cvShowImage("green", green);
cvShowImage("red", red);
cvWaitKey(0);
return 0;
}

2nd, directly interchanging the RGB values by just making output[j][i].r = pic[j][i].g

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "rgb.h"

using namespace std;

int main(){
    IplImage *img = NULL, *out = NULL;
    img = cvLoadImage("rgb.jpg");
    RgbImage pic(img);
    RgbImage outpic(out);
    int H = img -> height;
    int W = img -> width;

    for(int j=0;j<H;j++){
        for(int i=0;i<W;i++){
            pic[j][i].r = outpic[j][i].g;
            pic[j][i].g = outpic[j][i].b;
            pic[j][i].b = outpic[j][i].r;
        }
    }

    cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
    cvShowImage("image", img);
    cvNamedWindow("Output", CV_WINDOW_AUTOSIZE);
    cvShowImage("Output", out);

    cvWaitKey(0);
    cvReleaseImage(&img);

}

lastly, creating a blue, red, and green filter images with the same size, filled with 255's in the channel of interest, and all zeroes in the other channels to get the RGB values and interhange them. All I got are 3 images with filled blue, red and green. and made the inputed image BW.

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "rgb.h"
#include <cmath>
using namespace std;


int main()
{
  char infname[256];

  cout << "Enter input image  : ";
  cin >> infname;

  IplImage *img = cvLoadImage(infname, 0);
  RgbImage pic(img);
  int H = img->height;
  int W = img->width;

IplImage* redfilter = cvCreateImage(cvGetSize(img),8,3);
IplImage* greenfilter = cvCreateImage(cvGetSize(img),8,3);
IplImage* bluefilter = cvCreateImage(cvGetSize(img),8,3);

for (int y=0;y<H;y++)
for (int x=0;x<W;x++) {
   CV_IMAGE_ELEM(redfilter,unsigned char, y, 3*x + 0) = 0;
   CV_IMAGE_ELEM(redfilter,unsigned char, y, 3*x + 1) = 0;
   CV_IMAGE_ELEM(redfilter,unsigned char, y, 3*x + 2) = 255;
 }


for (int y=0;y<H;y++)
for (int x=0;x<W;x++) {
   CV_IMAGE_ELEM(greenfilter,unsigned char, y, 3*x + 0) = 0;
   CV_IMAGE_ELEM(greenfilter,unsigned char, y, 3*x + 1) = 255;
   CV_IMAGE_ELEM(greenfilter,unsigned char, y, 3*x + 2) = 0;
 }

for (int y=0;y<H;y++)
for (int x=0;x<W;x++) {
   CV_IMAGE_ELEM(bluefilter,unsigned char, y, 3*x + 0) = 255;
   CV_IMAGE_ELEM(bluefilter,unsigned char, y, 3*x + 1) = 0;
   CV_IMAGE_ELEM(bluefilter,unsigned char, y, 3*x + 2) = 0;
 }

cvShowImage("original", img);
cvShowImage("red", redfilter);
cvShowImage("green", greenfilter);
cvShowImage("blue", bluefilter);

cvWaitKey(0);


return 0;
}

I know all of these approaches are wrong. Please instruct me what to do to interchange the colors of an inputed image.

Thanks.

2
You can use cvMixChannels to swap image channels. - sgarizvi

2 Answers

0
votes
        pic[j][i].r = outpic[j][i].g;
        pic[j][i].g = outpic[j][i].b;
        pic[j][i].b = outpic[j][i].r;

That looks backwards to me. pic is the loaded image. Shouldn't you assign to outpic?

0
votes

In the first program, cvThreshold() is returning a black and white image as it is supposed to.

CV_THRESH_BINARY: dst(x,y) = maxValue, if src(x,y)>threshold 0, otherwise