Qt+OpenCV设置图片通道颜色
日期:2020-06-08 栏目:程序人生 浏览:次
Qt+OpenCV设置图片通道颜色源代码:
#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);
}
效果: