基于Qt OpenCV怎么实现图像灰度化像素
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了基于Qt OpenCV怎么实现图像灰度化像素的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇基于Qt OpenCV怎么实现图像灰度化像素文章都会有所收获,下面我们一起来看看吧。
效果图
实现代码
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<opencv2/opencv.hpp>using namespace cv;QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACEclass Widget : public QWidget{ Q_OBJECTpublic: Widget(QWidget *parent = nullptr); ~Widget();private slots: void TheMax(Mat Input_iamge); void TheMean(Mat Input_iamge); void TheMeightedMean(Mat Input_iamge); void UI_init();private: Ui::Widget *ui;};#endif // WIDGET_H
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget){ ui->setupUi(this); UI_init(); //init ui Mat image_1=imread("D:/program/Qt/Qt5+opencv/108homework/me.png"); ui->image1->setPixmap(QPixmap("D:/program/Qt/Qt5+opencv/108homework/me.png")); TheMax(image_1); ui->image2->setPixmap(QPixmap("D:/program/Qt/Qt5+opencv/108homework/image_2.png")); TheMean(image_1); ui->image3->setPixmap(QPixmap("D:/program/Qt/Qt5+opencv/108homework/image_3.png")); TheMeightedMean(image_1); ui->image4->setPixmap(QPixmap("D:/program/Qt/Qt5+opencv/108homework/image_4.png"));}Widget::~Widget(){ delete ui;}void Widget::TheMax(Mat Input_iamge){ int width = Input_iamge.cols; int height = Input_iamge.rows; Mat image_2(height,width,CV_8UC1); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { int b = Input_iamge.at<Vec3b>(j, i)[0]; int g = Input_iamge.at<Vec3b>(j, i)[1]; int r = Input_iamge.at<Vec3b>(j, i)[2]; image_2.at<uchar>(j, i) = saturate_cast<uchar>(max(r,max(b,g))); } } imwrite("D:/program/Qt/Qt5+opencv/108homework/image_2.png", image_2);}void Widget::TheMean(Mat Input_iamge){ int width = Input_iamge.cols; int height = Input_iamge.rows; Mat image_3(height,width,CV_8UC1); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { int b = Input_iamge.at<Vec3b>(j, i)[0]; int g = Input_iamge.at<Vec3b>(j, i)[1]; int r = Input_iamge.at<Vec3b>(j, i)[2]; image_3.at<uchar>(j, i) = saturate_cast<uchar>((r+g+b)/3); } } imwrite("D:/program/Qt/Qt5+opencv/108homework/image_3.png", image_3);}void Widget::TheMeightedMean(Mat Input_iamge){ int width = Input_iamge.cols; int height = Input_iamge.rows; Mat image_4(height,width,CV_8UC1); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { int b = Input_iamge.at<Vec3b>(j, i)[0]; int g = Input_iamge.at<Vec3b>(j, i)[1]; int r = Input_iamge.at<Vec3b>(j, i)[2]; image_4.at<uchar>(j, i) = saturate_cast<uchar>(0.299*r + 0.587*g + 0.114*b); } } imwrite("D:/program/Qt/Qt5+opencv/108homework/image_4.png", image_4);}void Widget::UI_init(){ ui->image1->setFrameShape(QFrame::Box); ui->image1->setFrameShadow(QFrame::Sunken); ui->image1->setLineWidth(5); ui->image1->setLineWidth(10); ui->image2->setFrameShape(QFrame::Box); ui->image2->setFrameShadow(QFrame::Sunken); ui->image2->setLineWidth(5); ui->image2->setLineWidth(10); ui->image3->setFrameShape(QFrame::Box); ui->image3->setFrameShadow(QFrame::Sunken); ui->image3->setLineWidth(5); ui->image3->setLineWidth(10); ui->image4->setFrameShape(QFrame::Box); ui->image4->setFrameShadow(QFrame::Sunken); ui->image4->setLineWidth(5); ui->image4->setLineWidth(10);}
关于“基于Qt OpenCV怎么实现图像灰度化像素”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“基于Qt OpenCV怎么实现图像灰度化像素”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341