qt+opencv设置图片通道颜色

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.

源代码:

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); cvNamedWindow("jdh1",CV_WINDOW_AUTOSIZE); cvNamedWindow("jdh2",CV_WINDOW_AUTOSIZE); img1 = cvLoadImage("test.jpg"); img2 = cvLoadImage("test.jpg"); cvShowImage("jdh1",img1); cvShowImage("jdh2",img2); } Widget::~Widget() { delete ui; cvReleaseImage(&img1); cvReleaseImage(&img2); cvDestroyAllWindows(); } void Widget::on_pushButton_clicked() { QString red = ui->lineEdit->text(); QString green = ui->lineEdit_2->text(); QString blue = ui->lineEdit_3->text(); int temp = 0; for (int y = 0;y < img2->height;y++) { unsigned char *ptr = (unsigned char *)(img2->imageData + y * img2->widthStep); unsigned char *ptr1 = (unsigned char *)(img1->imageData + y * img1->widthStep); for (int x = 0;x < img2->width;x++) { bool ok; //蓝色设置 temp = blue.toInt(&ok,10); if (ok) { ptr[3 * x + 0] = temp; } else { ptr[3 * x + 0] = ptr1[3 * x + 0]; } //绿色设置 temp = green.toInt(&ok,10); if (ok) { ptr[3 * x + 1] = temp; } else { ptr[3 * x + 1] = ptr1[3 * x + 1]; } //红色设置 temp = red.toInt(&ok,10); if (ok) { ptr[3 * x + 2] = temp; } else { ptr[3 * x + 2] = ptr1[3 * x + 2]; } } } cvShowImage("jdh2",img2); }  

效果:

qt+opencv设置图片通道颜色_第1张图片

 

 

qt+opencv设置图片通道颜色_第2张图片

 

你可能感兴趣的:(qt+opencv设置图片通道颜色)