#include <QUrl>
#include <QList>
#include <QtGui/QPixmap>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDropEvent>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <math.h>
#include <iostream>
#include<sstream>
#include "binarymorphology.h"
#include"graymorphology.h"
#include<QString>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setWidget(ui->label_2);
ui->scrollArea_2->setWidget(ui->label_12);
QObject::connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
ui->spinBox, SLOT(setValue(int)));
QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)),
ui->horizontalSlider, SLOT(setValue(int)));
QObject::connect(ui->horizontalSlider_2, SIGNAL(valueChanged(int)),
ui->spinBox_13, SLOT(setValue(int)));
QObject::connect(ui->spinBox_13, SIGNAL(valueChanged(int)),
ui->horizontalSlider_2, SLOT(setValue(int)));
QObject::connect(ui->horizontalSlider_3, SIGNAL(valueChanged(int)),
ui->spinBox_14, SLOT(setValue(int)));
QObject::connect(ui->spinBox_14, SIGNAL(valueChanged(int)),
ui->horizontalSlider_3, SLOT(setValue(int)));
QObject::connect(ui->horizontalSlider_4, SIGNAL(valueChanged(int)),
ui->spinBox_15, SLOT(setValue(int)));
QObject::connect(ui->spinBox_15, SIGNAL(valueChanged(int)),
ui->horizontalSlider_4, SLOT(setValue(int)));
initialHistogram();
// connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openImage()));
ui->label_2->installEventFilter(this);
ui->label_2->setAcceptDrops(true); // [[1]]: 使label可接受拖放操作
ui->label_2->setMouseTracking(true);
ui->label_12->installEventFilter(this);
ui->label_12->setAcceptDrops(true); // [[1]]: 使label可接受拖放操作
// parent->setMouseTracking(true);
//me();
this->setMouseTracking(true);
mw=new MouseWindow();
// m_pStatus = new QStatusBar();
// setStatusBar(m_pStatus);
// m_pStatus->showMessage("application init ok!");
// this->ui->label_19->setText("ccccc");
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
if (watched == ui->label_2) {
if (event->type() == QEvent::DragEnter) {
// [[2]]: 当拖放时鼠标进入label时, label接受拖放的动作
QDragEnterEvent *dee = dynamic_cast<QDragEnterEvent *>(event);
dee->acceptProposedAction();
return true;
} else if (event->type() == QEvent::Drop) {
// [[3]]: 当放操作发生后, 取得拖放的数据
QDropEvent *de = dynamic_cast<QDropEvent *>(event);
QList<QUrl> urls = de->mimeData()->urls();
if (urls.isEmpty()) { return true; }
QString path = urls.first().toLocalFile();
// [[4]]: 在label上显示拖放的图片
QImage loadedImage(path); // QImage对I/O优化过, QPixmap对显示优化
if (!loadedImage.isNull()) {
this->ui->label_2->setPixmap(QPixmap::fromImage(loadedImage));
this->sourceimage=loadedImage;
this->currentimage=loadedImage;
this->tempimage=loadedImage;
//QString s="";
//s=QString::number(currentimage.width())+"×"+QString::number(currentimage.height());
//this->ui->label_20->setText(s);
drawHistogram();
}
//else if(event->type()==QEvent::MouseMove)
//{
// QMouseEvent *de2 = dynamic_cast<QMouseMoveEvent *>(event);
// }
// else if(event->type()==QEvent::MouseButtonPress)
// {
// QMouseEvent *d=dynamic_cast<QMouseEvent *>(event);
//QPoint pos = d->pos();
// this->ui->label_19->setText("bbb");
// this->ui->label_19->setText("aaa"+QString("x:%1,y:%2").arg(pos.x()).arg(pos.y()));
// }
return true;
}
}
if (watched == ui->label_12) {
if (event->type() == QEvent::DragEnter) {
// [[2]]: 当拖放时鼠标进入label时, label接受拖放的动作
QDragEnterEvent *dee = dynamic_cast<QDragEnterEvent *>(event);
dee->acceptProposedAction();
return true;
} else if (event->type() == QEvent::Drop) {
// [[3]]: 当放操作发生后, 取得拖放的数据
QDropEvent *de = dynamic_cast<QDropEvent *>(event);
QList<QUrl> urls = de->mimeData()->urls();
if (urls.isEmpty()) { return true; }
QString path = urls.first().toLocalFile();
// [[4]]: 在label上显示拖放的图片
QImage loadedImage(path); // QImage对I/O优化过, QPixmap对显示优化
if (!loadedImage.isNull()) {
this->addedimage=loadedImage;
this->ui->label_12->setPixmap(QPixmap::fromImage(loadedImage));
this->addedtempimage=loadedImage;
}
return true;
}
}
return QWidget::eventFilter(watched, event);
}
void MainWindow::openImage()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::currentPath(),"Images(*.jpg *.png *.bmp *.gif)");
if (!fileName.isEmpty()){
QImage loadedImage;
if (!loadedImage.load(fileName))
return;
this->ui->label_2->setPixmap(QPixmap::fromImage(loadedImage));
this->sourceimage=loadedImage;
this->currentimage=loadedImage;
this->tempimage=loadedImage;
drawHistogram();
}
}
void MainWindow::saveImage()
{
QString fileName = QFileDialog::getSaveFileName(this,tr("Save Image"), QDir::currentPath(),"Images(*.jpg *.png *.bmp *.gif)");
if(fileName.isEmpty())
{
return;
}
else{
currentimage.save(fileName);
}
}
void MainWindow::on_actionOpen_triggered()
{
openImage();
}
void MainWindow::toGrayLevel(){
int m_H=this->currentimage.height();
int m_W=this->currentimage.width();
for(int y=0;y<m_H;y++)
for(int x=0;x<m_W;x++)
{
int gray=qGray(this->currentimage.pixel(x,y));
QColor color(gray, gray, gray);
currentimage.setPixel(x,y,color.rgb());
}
this->ui->label_2->setPixmap(QPixmap::fromImage(currentimage));
}
void MainWindow::on_pushButton_4_clicked()
{
toGrayLevel();
drawHistogram();
}
void MainWindow::toBinary(int value)
{
int m_H=this->currentimage.height();
int m_W=this->currentimage.width();
for(int y=0;y<m_H;y++)
for(int x=0;x<m_W;x++)
{
int gray=qGray(this->currentimage.pixel(x,y));
// sourceimage.pixel(x,y);
if(gray>=value)
gray=255;
if(gray<value)
gray=0;
QColor color(gray, gray, gray);
tempimage.setPixel(x,y,color.rgb());
}
this->ui->label_2->setPixmap(QPixmap::fromImage(tempimage));
this->ui->pushButton_5->setEnabled(true);
}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
toBinary(value);
}
void MainWindow::on_pushButton_3_clicked()
{
this->ui->label_2->setPixmap(QPixmap::fromImage(sourceimage));
currentimage=sourceimage;
drawHistogram();
}
void MainWindow::initialHistogram()
{
QImage* histogram=new QImage(260,200,QImage::Format_ARGB32);
QColor color(255, 255, 255);
int m_H=histogram->height();
int m_W=histogram->width();
for(int y=0;y<m_H;y++)
for(int x=0;x<m_W;x++)
{
histogram->setPixel(x,y,color.rgb());
[qt]图像处理程序
5星 · 超过95%的资源 需积分: 15 46 浏览量
2013-03-12
15:56:34
上传
评论 3
收藏 280KB ZIP 举报
林檎ringo
- 粉丝: 2
- 资源: 3
最新资源
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
- 基于Node.js的Express框架与MySQL的后台管理系统设计源码
- 基于Java的Flink流批一体数据处理快速集成开发框架设计源码
- FirstFilterOrderCompare
- Screenshot_2024-03-28-19-17-25-020_com.ss.android.lark.jpg
- 基于Java的车辆违章信息查询系统设计源码
- wqeAFSDADWDAESD
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页