简体   繁体   English

通过互换RGB进行色彩处理

[英]Color Manipulation by Interchanging RGB

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. 我尝试了3种方法,但是所有方法都失败了。 Below are the three approach. 以下是三种方法。

Splitting RGB to get their values and interchange them. 拆分RGB以获取它们的值并交换它们。 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 第二,仅通过使output [j] [i] .r = pic [j] [i] .g直接交换RGB值

#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. 最后,创建大小相同的蓝色,红色和绿色滤镜图像,在感兴趣的通道中填充255,在其他通道中填充全零,以获取RGB值并将其交错。 All I got are 3 images with filled blue, red and green. 我所得到的是3张充满蓝色,红色和绿色的图像。 and made the inputed image BW. 并制作了输入图像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. 谢谢。

        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. pic是加载的图像。 Shouldn't you assign to outpic ? 您不应该分配给outpic吗?

In the first program, cvThreshold() is returning a black and white image as it is supposed to. 在第一个程序中,cvThreshold()返回了原本应该的黑白图像。

CV_THRESH_BINARY: dst(x,y) = maxValue, if src(x,y)>threshold 0, otherwise CV_THRESH_BINARY:dst(x,y)= maxValue,如果src(x,y)>阈值0,否则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM