0

我的任务是交换图像的颜色。将蓝色变为红色,将红色变为绿色,将绿色变为蓝色。我尝试了 3 种方法,但都失败了。以下是三种方法。

拆分 RGB 以获取它们的值并交换它们。我得到的只是一张 BW 图像。

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

2,直接交换RGB值,只需输出[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);

}

最后,创建一个大小相同的蓝色、红色和绿色滤镜图像,在感兴趣的通道中填充 255,在其他通道中填充为零,以获得 RGB 值并将它们相互连接。我得到的只是 3 张填充蓝色、红色和绿色的图像。并制作输入图像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;
}

我知道所有这些方法都是错误的。请指导我如何交换输入图像的颜色。

谢谢。

4

2 回答 2

0

在第一个程序中, cvThreshold() 按预期返回一个黑白图像。

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

于 2013-03-11T06:12:55.673 回答
0
        pic[j][i].r = outpic[j][i].g;
        pic[j][i].g = outpic[j][i].b;
        pic[j][i].b = outpic[j][i].r;

这在我看来是倒退的。 pic是加载的图像。你不应该分配给outpic吗?

于 2013-03-11T05:49:29.167 回答